问题
I have a PowerShell script (ps file) which I'd like to run from a C# WPF app on the clients' machine. Most of the examples I found uses the PowerShell SDK, but I can not install that on the client computers.
If I try to run my script simply with Process.Start like this:
Process.Start("Powershell.exe", "myscript.ps");
I get the following error in the command window:
File myscript.ps cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ myscript.ps
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
I looked at the link but I can not set the ExecutionPolicy on the machines of the clients.
Is there any other way to run a PS script from a C# client app without using the SDK or setting the ExecutionPolicy?
回答1:
I'm sorry, I don't know C#, but there's a command line option for powershell.exe that will help you I'm pretty sure.
powershell.exe -ExecutionPolicy Bypass
来源:https://stackoverflow.com/questions/22790362/how-to-run-powershell-script-file-without-sdk