Reputation: 570
When I run the following line directly within a powershell terminal it works perfectly fine:
dotnet build ./MySolution.sln --configuration Release
However, when I put that exact line inside of a powershell script, and run it from within the same directory I get this error:
MSBUILD : error MSB1009: Project file does not exist.
I've tried passing the parameters differently as mentioned in one of the answers in How to invoke MSBuild from PowerShell using & operator? So I'm now at a loss. What do I need to do to make this work from within a powershell script?
Upvotes: 5
Views: 9989
Reputation: 3246
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"
Upvotes: 9