Your approach is not working because it seems like it cannot find the project/solution file. I assume from your comment that something has failed before your command. You should check for errors from any other utilities that you are running.
With command line tools in general, I tend to do stuff like this by passing an array of arguments to the executable. An array of arguments seems to work better when the line becomes longer and more complicated in the console.
$DotnetArgs = @()
$DotnetArgs = $DotnetArgs + "build"
$DotnetArgs = $DotnetArgs + ".\MySolution.sln"
$DotnetArgs = $DotnetArgs + "--configuration" + "Release"
& dotnet $DotnetArgs
You can create a usable function like this and save it in your profile, at least that is what I do.
function Invoke-Dotnet {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[System.String]
$Command,
[Parameter(Mandatory = $true)]
[System.String]
$Arguments
)
$DotnetArgs = @()
$DotnetArgs = $DotnetArgs + $Command
$DotnetArgs = $DotnetArgs + ($Arguments -split "\s+")
[void]($Output = & dotnet $DotnetArgs)
# Should throw if the last command failed.
if ($LASTEXITCODE -ne 0) {
Write-Warning -Message ($Output -join "; ")
throw "There was an issue running the specified dotnet command."
}
}
Then you can run it as so:
Invoke-Dotnet -Command build -Arguments ".\MySolution.sln --configuration Release"
*.slnfile isn't present.