使用C#中的invoke命令引发异常

使用C#中的invoke命令引发异常,c#,powershell,powershell-remoting,invoke-command,executionpolicy,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" 这很好用。 但当

我已从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#的invoke命令尝试同样的事情时,它抛出了以下异常:

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会话,并确保执行策略设置为RemoteSigned或Unrestricted。

尝试使用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;
        }
公共静态ICollection RunScriptText(字符串scriptFullPath,ICollection参数=null)
{
var runspace=RunspaceFactory.CreateRunspace();
Open();
var pipeline=runspace.CreatePipeline();
使用(PowerShell PowerShellInstance=PowerShell.Create())
{
var initial=InitialSessionState.CreateDefault();
WriteLine(“导入服务器管理器模块”);
initial.ImportPSModule(新[]{“ServerManager”});
PowerShellInstance.Runspace=运行空间;
PowerShellInstance.AddScript(“设置执行策略不受限制”);
AddScript(“获取执行策略”);
PowerShellInstance.Invoke();
var cmd=新命令(scriptFullPath);
if(参数!=null)
{
foreach(参数中的var p)
{
cmd.Parameters.Add(p);
}
}
pipeline.Commands.Add(cmd);
var results=pipeline.Invoke();
pipeline.Dispose();
Dispose();
返回结果;
}
返回null;
}

尝试使用32位windows powershell的RemoteSigned和Unrestricted执行策略。仍会收到相同的错误。请尝试将应用程序编译为x86。我在powershell 32位终端中直接尝试了invoke命令,但遇到了问题中提到的异常。如果在该shell中运行Get ExecutionPolicy,它是否返回
受限
?如果是这样,您需要运行(在提升的32位PowerShell中)
设置ExecutionPolicy Unrestricted