5

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?

1
  • 1
    There's nothing wrong with the invocation syntax. The error message suggests that - despite what you say in the question - you're running your script from a different directory, where the *.sln file isn't present. Commented Aug 11, 2021 at 20:39

1 Answer 1

9

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"
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Any chance you can explain why the approach being used by the question-asker doesn't work? What's the bug/issue in there
I tried your first approach (aggregating the arguments into an array) but I'm getting the same error... I'd rather not try to create an entire function as this script will eventually be run in an azure pipeline.
Scratch that, there was an issue with a command before this in my script that caused the error after making those adjustments. Your approach works great! Thank you =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.