ume*_*ape 119 c# asp.net powershell
我运行此代码以从ASP.NET应用程序执行PowerShell代码:
System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
runspace.Open();
System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(@"\\servername\path");
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
无法加载.ps1,因为在此系统上禁用了脚本的执行.有关详细信息,请参阅"get-help about_signing".
从命令提示符或Windows(Windows窗体)应用程序运行相同的代码.
gho*_*ord 148
由于执行策略,您的脚本被阻止执行.
您需要以管理员身份运行PowerShell,并在客户端PC上将其设置为Unrestricted.您可以通过调用Invoke来执行此操作:
Set-ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)
Jon*_*ell 79
在某些情况下,您可以按照其他答案中建议的步骤进行操作,验证是否正确设置了执行策略,并且仍然使脚本失败.如果您遇到这种情况,您可能位于具有32位和64位版本PowerShell的64位计算机上,并且在未设置执行策略的版本上发生故障.该设置不适用于这两个版本,因此您必须明确设置两次.
在Windows目录中查找System32和SysWOW64.
对每个目录重复这些步骤:
检查ExecutionPolicy的当前设置:
Get-ExecutionPolicy -List
为所需的级别和范围设置ExecutionPolicy,例如:
Set-ExecutionPolicy -Scope LocalMachine Unrestricted
请注意,您可能需要以管理员身份运行PowerShell,具体取决于您尝试为其设置策略的范围.
您可以在此处阅读更多内容:运行Windows PowerShell脚本
Nad*_*_MK 24
你需要运行Set-ExecutionPolicy
:
Set-ExecutionPolicy Unrestricted <-- Will allow unsigned PowerShell scripts to run.
Set-ExecutionPolicy Restricted <-- Will not allow unsigned PowerShell scripts to run.
Set-ExecutionPolicy RemoteSigned <-- Will allow only remotely signed PowerShell scripts to run.
Run Code Online (Sandbox Code Playgroud)
Mat*_*les 23
问题是执行策略是基于每个用户设置的.每次运行它时,您都需要在应用程序中运行以下命令以使其工作:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)
可能有一种方法可以为ASP.NET用户设置此项,但这意味着您不会打开整个系统,只打开您的应用程序.
(来源)
小智 13
我有一个类似的问题,并注意到 Windows Server 2012 上的默认 cmd 运行的是 x64 的。
对于 Windows 7、Windows 8、Windows Server 2008 R2 或 Windows Server 2012,以管理员身份运行以下命令:
打开C:\Windows\SysWOW64\cmd.exe
运行命令:powershell Set-ExecutionPolicy RemoteSigned
打开C:\Windows\system32\cmd.exe
运行命令powershell Set-ExecutionPolicy RemoteSigned
您可以使用检查模式
在 CMD 中:echo %PROCESSOR_ARCHITECTURE%
在 Powershell 中:[Environment]::Is64BitProcess
我希望这可以帮助你。
小智 8
尝试这个:
Set-ExecutionPolicy -scope CurrentUser Unrestricted
Run Code Online (Sandbox Code Playgroud)