我运行此代码以从 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();
但我收到错误:
无法加载 .ps1,因为禁用了脚本的执行
这个系统。请参阅“get-help about_signing”了解更多详细信息。
相同的代码可以在命令提示符或 Windows(Windows 窗体)应用程序中正常运行。
由于以下原因,您的脚本被阻止执行执行政策 http://technet.microsoft.com/en-us/library/ee176961.aspx.
您需要以管理员身份运行PowerShell,并将其在客户端PC上设置为无限制。您可以通过调用 Invoke 来做到这一点:
Set-ExecutionPolicy Unrestricted
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)