How to invoke MSBuild from PowerShell using & operator?

后端 未结 4 635
陌清茗
陌清茗 2020-12-15 16:42

I\'ve just tested this on PowerShell v1.0. Setup is as follows:

 Id CommandLine
 -- -----------
  1 $msbuild = \"C:\\Windows\\Microsoft.NET\\Framework\\v3.5\         


        
相关标签:
4条回答
  • 2020-12-15 16:58

    Ugh.

    $collectionOfArgs = @("C:\some\project\or\other\src\Solution.sln", 
        "/target:Clean", "/target:Build")
    & $msbuild $collectionOfArgs
    

    This works. & takes a collection of arguments, so you must split up strings containing multiple arguments into a collection of string arguments.

    0 讨论(0)
  • 2020-12-15 17:08

    The issues you are seeing results from PowerShell parsing arguments. In the first example, when PowerShell sees $a it passes it as a single parameter msbuild. We can see this using the echoargs utility from PSCX:.

    PS> $a = "C:\some\project\or\other\src\Solution.sln /target:Clean /target:Build"
    PS> & echoargs $a
    Arg 0 is <C:\some\project\or\other\src\Solution.sln /target:Clean /target:Build>
    

    The second example is even worse because you are telling powershell to invoke "$echoargs $a" as the command name and it isn't a valid command name.

    The third line works because CMD.exe gets the expanded form of "$echoargs $a" as a single argument which is parses and executes:

    You have a couple of options here. First I do it this way:

    PS> & $msbuild C:\some\project\or\other\src\Solution.sln `
        /target:Clean /target:Build
    

    The other option is to use Invoke-Expression like so:

    PS> Invoke-Expression "$msbuild $a"
    

    In general I try to be very careful with Invoke-Expression particularly if any part of the string that gets invoked is provided by the user.

    0 讨论(0)
  • 2020-12-15 17:09

    You can also try using the free Invoke-MsBuild powershell script/module. This essentially gives you an Invoke-MsBuild PowerShell cmdlet that you can call instead of trying to invoke the msbuild.exe manually yourself.

    0 讨论(0)
  • 2020-12-15 17:10

    That works well for me:

    PS> cmd.exe /c 'C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe' /target:Clean /target:Build 'C:\some\project\or\other\src\Solution.sln'
    
    0 讨论(0)
提交回复
热议问题