PowerShell - Start-Process and Cmdline Switches

前端 未结 5 618
我寻月下人不归
我寻月下人不归 2020-11-28 08:27

I can run this fine:

$msbuild = \"C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\MSBuild.exe\" 
start-process $msbuild -wait

But when I run t

相关标签:
5条回答
  • 2020-11-28 08:48

    you are going to want to separate your arguments into separate parameter

    $msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe"
    $arguments = "/v:q /nologo"
    start-process $msbuild $arguments 
    
    0 讨论(0)
  • 2020-11-28 08:51

    Using explicit parameters, it would be:

    $msbuild = 'C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe'
    start-Process -FilePath $msbuild -ArgumentList '/v:q','/nologo'
    

    EDIT: quotes.

    0 讨论(0)
  • 2020-11-28 08:55

    Unless the OP is using PowerShell Community Extensions which does provide a Start-Process cmdlet along with a bunch of others. If this the case then Glennular's solution works a treat since it matches the positional parameters of pscx\start-process : -path (position 1) -arguments (positon 2).

    0 讨论(0)
  • 2020-11-28 08:59

    Warning

    If you run PowerShell from a cmd.exe window created by Powershell, the 2nd instance no longer waits for jobs to complete.

    cmd>  PowerShell
    PS> Start-Process cmd.exe -Wait 
    

    Now from the new cmd window, run PowerShell again and within it start a 2nd cmd window: cmd2> PowerShell

    PS> Start-Process cmd.exe -Wait
    PS>   
    

    The 2nd instance of PowerShell no longer honors the -Wait request and ALL background process/jobs return 'Completed' status even thou they are still running !

    I discovered this when my C# Explorer program is used to open a cmd.exe window and PS is run from that window, it also ignores the -Wait request. It appears that any PowerShell which is a 'win32 job' of cmd.exe fails to honor the wait request.

    I ran into this with PowerShell version 3.0 on windows 7/x64

    0 讨论(0)
  • 2020-11-28 09:03

    I've found using cmd works well as an alternative, especially when you need to pipe the output from the called application (espeically when it doesn't have built in logging, unlike msbuild)

    cmd /C "$msbuild $args" >> $outputfile

    0 讨论(0)
提交回复
热议问题