【发布时间】:2013-05-03 20:00:39
【问题描述】:
我运行此代码以从 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 窗体)应用程序中正常运行。
【问题讨论】:
-
您只需要以管理员身份运行您的 powershell 然后运行以下两个留置权 第一行:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -scope currentuser 输入 Y 表示是,然后第二行:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -范围 LocalMachine 再次输入 Y 表示是这样,享受
-
这不应该是重复的,在这个问题中,我们正在创建我们自己的运行空间。在重复中,我们在普通的 powershell 运行空间中运行,对于那些发现这个的人,当你打电话给
RunspaceFactory.CreateRunspace你可以提供一个InitialSessionState,您将ExecutionPolicy设置为ExecutionPolicy.Unrestricted。
标签: c# asp.net powershell