Best practice for Invoke other scripts or exe in PowerShell

时间:2023-03-09 18:09:30
Best practice for Invoke other scripts or exe in PowerShell

Recently, I find I used many different type method to invoke other scripts or exe in PowerShell. Maybe by start new process, or just use the call operator &. That's fine. But I want to mention there's one preper way to invoke other scripts or exe in PowerShell I'd like to use.

Problem background:

I want to run msbuild to build something, but I want to sepcify multi folders. e.g. I want to output to both 'C:\output' and 'D:\output'. So, what should I do ?

I may use the call operator & directly like:

Best practice for Invoke other scripts or exe in PowerShell

What's wrong ? Can you find it ?

Actually, The real OutputFolder property value would be: "C:\output, D:\output".

And the whole script will be tranlated to:

& .\msbuild.exe YOUR_PROJECT.csproj /t:$Targets /p:C:\output, D:\output

msbuild.exe will report something like, property is invalid or cannot distinguish and so on. Because the wired comma exists.

Sulotion:

Best practice for Invoke other scripts or exe in PowerShell

Create a argument array, and use double quote to quote the property value to make true the quote will in the property value.

Done.