How to run PowerShell script file without SDK?

耗尽温柔 提交于 2021-01-29 18:01:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!