c# - 使用 C# 中的调用命令抛出异常

标签 c# powershell powershell-remoting invoke-command executionpolicy

我从 powershell 终端尝试了以下情况:

invoke-command -connectionuri "http://FullMachineName/wsman" -configuration "http
://schemas.microsoft.com/powershell/Microsoft.PowerShell" -credential "MachineName\Administrator" -filepath "C:\scripts\get
Users.ps1"

这工作正常。 但是,当我通过 C# 的调用命令尝试相同的操作时,它会抛出以下异常:

System.Management.Automation.CmdletInvocationException was unhandled
  Message=File C:\scripts\getUsers.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
  Source=System.Management.Automation
  WasThrownFromThrowStatement=false
  StackTrace:
       at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
       at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
       at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
  InnerException: System.Management.Automation.PSSecurityException
       Message=File C:\scripts\getUsers.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
       Source=System.Management.Automation
       WasThrownFromThrowStatement=false
       StackTrace:
            at System.Management.Automation.AuthorizationManager.ShouldRunInternal(CommandInfo commandInfo, CommandOrigin origin, PSHost host)
            at Microsoft.PowerShell.Commands.PSExecutionCmdlet.GetScriptBlockFromFile(String filePath)
            at Microsoft.PowerShell.Commands.PSExecutionCmdlet.BeginProcessing()
            at Microsoft.PowerShell.Commands.InvokeCommandCommand.BeginProcessing()
            at System.Management.Automation.Cmdlet.DoBeginProcessing()
            at System.Management.Automation.CommandProcessorBase.DoBegin()
       InnerException: System.UnauthorizedAccessException
            Message=File C:\scripts\getUsers.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
            InnerException: 

我正在尝试使用以下代码:

String file = @"C:\scripts\getUsers.ps1";
PowerShell ps = PowerShell.Create()
PSCommand cmd = new PSCommand();                
cmd.AddCommand("Invoke-Command");
cmd.AddParameter("CONNECTIONURI", "http://FullMachineName/wsman");
cmd.AddParameter("FILEPATH",file);
cmd.AddParameter("CREDENTIAL", Credential);            
cmd.AddParameter("CONFIGURATION","http://schemas.microsoft.com/powershell/Microsoft.PowerShell" );
ps.Commands = cmd;                
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
ps.Runspace = runSpace;
psObjects = ps.Invoke();

最佳答案

此场景中的典型问题是您已在 PowerShell 中为 32 位或 64 位版本的 PowerShell 运行 Set-ExecutionPolicy,但不能同时运行这两者。您正在运行的 C# 程序可能是您没有更改 PowerShell 的默认执行策略的位。尝试打开 32 位和 64 位 PowerShell session ,并确保执行策略设置为 RemoteSigned 或 Unrestricted。

关于c# - 使用 C# 中的调用命令抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21423339/

相关文章:

PowerShell 远程处理 $Using 变量范围

c# - 使用 Entity Framework 的规范模式(按属性排序)

c# - 有谁知道 AsyncPageable 是什么?

powershell - 将哈希表打印为文字

PowerShell PadRight 未添加指定的字符数

powershell - PowerShell 控制台输出中的可点击超链接

Powershell 远程处理的 Python 包装器

c# - Entity Framework 的查询执行速度极慢且效率低下

java - 如何在 Java 中创建自定义事件

powershell - 如何使用Microsoft帐户添加-AzureRmAccount?