【问题标题】:Using invoke-command from C# throwing exception使用 C# 中的调用命令引发异常
【发布时间】:2014-02-20 19:10:37
【问题描述】:

我已经从 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();

【问题讨论】:

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


    【解决方案1】:

    此方案中的典型问题是,您在 PowerShell 中为 32 位或 64 位版本的 PowerShell 运行了 Set-ExecutionPolicy,但不能同时运行这两种版本。您正在运行的 C# 程序可能是您尚未更改 PowerShell 的默认执行策略的位。尝试同时打开 32 位和 64 位 PowerShell 会话,并确保将执行策略设置为 RemoteSigned 或 Unrestricted。

    【讨论】:

    • 从 32 位 windows powershell 尝试使用执行策略 RemoteSigned 和 Unrestricted。仍然得到同样的错误。
    • 尝试将应用程序编译为 x86。
    • 我直接在powershell 32位终端中尝试了invoke-command,我得到了问题中提到的异常。
    • 如果你在那个shell中运行Get-ExecutionPolicy,它会返回Restricted吗?如果是这样,您需要运行(在提升的 32 位 PowerShell 中)Set-ExecutionPolicy Unrestricted
    【解决方案2】:

    尝试使用 PowerShell 实例设置和获取 Set-ExecutionPolicy Unrestricted。

     public static ICollection<PSObject> RunScriptText(string scriptFullPath, ICollection<CommandParameter> parameters = null)
            {
                var runspace = RunspaceFactory.CreateRunspace();
                runspace.Open();
                var pipeline = runspace.CreatePipeline();
    
                using (PowerShell PowerShellInstance = PowerShell.Create())
                {
                    var initial = InitialSessionState.CreateDefault();
                    Console.WriteLine("Importing ServerManager module");
                    initial.ImportPSModule(new[] { "ServerManager" });
    
                    PowerShellInstance.Runspace = runspace;
    
                    PowerShellInstance.AddScript("Set-ExecutionPolicy Unrestricted");
                    PowerShellInstance.AddScript("Get-ExecutionPolicy");
                    PowerShellInstance.Invoke();
    
                    var cmd = new Command(scriptFullPath);
                    if (parameters != null)
                    {
                        foreach (var p in parameters)
                        {
                            cmd.Parameters.Add(p);
                        }
                    }
                    pipeline.Commands.Add(cmd);
                    var results = pipeline.Invoke();
                    pipeline.Dispose();
                    runspace.Dispose();
                    return results;
                }
                return null;
            }
    

    【讨论】:

      猜你喜欢
      • 2018-12-09
      • 1970-01-01
      • 2019-09-19
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      • 2022-01-16
      相关资源
      最近更新 更多