Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C创建PowerShell远程会话#_C#_Powershell - Fatal编程技术网

C# 使用C创建PowerShell远程会话#

C# 使用C创建PowerShell远程会话#,c#,powershell,C#,Powershell,我一直在尝试使用C#从.Net应用程序创建与PowerShell的连接。当我尝试创建会话时,连接完成后,它返回空集合 string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell"; PSCredential remoteCredential = new PSCredential("userID", StringToSecureString("Password")); WSManConnecti

我一直在尝试使用C#从.Net应用程序创建与PowerShell的连接。当我尝试创建会话时,连接完成后,它返回空集合

string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";

PSCredential remoteCredential = new PSCredential("userID", StringToSecureString("Password"));

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "Ip Address of server", 5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);

runspace = RunspaceFactory.CreateRunspace(connectionInfo);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

runspace.Open();

using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;

ps.AddScript(@"$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://servername/poweshell -Credential " + remoteCredential);

//result count returned is 0 
var result = ps.Invoke();

ps.Commands.Clear();

ps.AddCommand("Import-PSSession $Session");

ps.Invoke();
}

我无法对此进行测试,但它可能会让您走上正确的道路:

        string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
        PSCredential remoteCredential = new PSCredential("userID", StringToSecureString("Password"));
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "Ip Address of server", 5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);

        string scriptPath = $@"
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://servername/poweshell -Credential {remoteCredential} | Out-String
        Import-PSSession $Session";

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
        runspace.Open();
        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        Pipeline pipeline = runspace.CreatePipeline();
        string scriptfile = scriptPath;
        Command myCommand = new Command(scriptfile, false);
        pipeline.Commands.Add(myCommand);
        pipeline.Invoke();
        runspace.Close();

我无法对此进行测试,但它可能会让您走上正确的道路:

        string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
        PSCredential remoteCredential = new PSCredential("userID", StringToSecureString("Password"));
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "Ip Address of server", 5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);

        string scriptPath = $@"
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://servername/poweshell -Credential {remoteCredential} | Out-String
        Import-PSSession $Session";

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
        runspace.Open();
        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        Pipeline pipeline = runspace.CreatePipeline();
        string scriptfile = scriptPath;
        Command myCommand = new Command(scriptfile, false);
        pipeline.Commands.Add(myCommand);
        pipeline.Invoke();
        runspace.Close();

我有一篇文章介绍了一种通过WinRM从.NET在运行Powershell的简单方法,或者您可以直接在获取代码

多年来,我一直在处理任何参数编码问题,它大大简化了这些操作的运行

如果您只想复制代码,那么代码将位于单个文件中,并且它也是一个NuGet包,其中包含对System.Management.Automation的引用

// this is the entrypoint to interact with the system (interfaced for testing).
var machineManager = new MachineManager(
    "10.0.0.1",
    "Administrator",
    MachineManager.ConvertStringToSecureString("xxx"),
    true);

// can run random script blocks WITH parameters.
var fileObjects = machineManager.RunScript(
    "{ param($path) ls $path }",
    new[] { @"C:\PathToList" });

希望这能有所帮助,我已经在自动化部署中使用了一段时间了。如果您发现问题,请留下评论。

我有一篇文章介绍了一种通过WinRM从.NET运行Powershell的简便方法,您也可以直接从.NET获取代码

多年来,我一直在处理任何参数编码问题,它大大简化了这些操作的运行

如果您只想复制代码,那么代码将位于单个文件中,并且它也是一个NuGet包,其中包含对System.Management.Automation的引用

// this is the entrypoint to interact with the system (interfaced for testing).
var machineManager = new MachineManager(
    "10.0.0.1",
    "Administrator",
    MachineManager.ConvertStringToSecureString("xxx"),
    true);

// can run random script blocks WITH parameters.
var fileObjects = machineManager.RunScript(
    "{ param($path) ls $path }",
    new[] { @"C:\PathToList" });

希望这能有所帮助,我已经在自动化部署中使用了一段时间了。如果您发现问题,请留下评论。

我试过了。但它抛出“无法执行操作,因为未实现文件:行:列:0:0”中偏移量78处的操作“NewNotImplementedException”。错误。好的,问题是您是否可以实际建立到“”的远程会话。运行$Session=New PSSession-ConfigurationName Microsoft.Exchange-ConnectionUri-Credential凭证时;直接从PowerShell导入PSSession$Session“是否可以返回任何数据?顺便说一句,这也可能有帮助:我尝试了上述解决方案,但没有效果。这里的问题是我可以从Windows PowerShell命令提示符运行PSSession命令。但在我的.Net应用程序中不起作用。当我调用PowerShell命令时,它返回0计数。”var result=ps.Invoke();“var结果中的计数为0这些解决方案如何?它起作用了。我现在可以连接到powershell并创建会话。但现在的另一个问题是,当我尝试使用“New mailbox”命令创建邮箱时,它返回错误“找不到与参数名称”标识匹配的参数”“。我已经在新的帖子上发布了这个问题。我试过了。但它抛出“无法执行操作,因为未实现文件:行:列:0:0”中偏移量78处的操作“NewNotImplementedException”。错误。好的,问题是您是否可以实际建立到“”的远程会话。运行$Session=New PSSession-ConfigurationName Microsoft.Exchange-ConnectionUri-Credential凭证时;直接从PowerShell导入PSSession$Session“是否可以返回任何数据?顺便说一句,这也可能有帮助:我尝试了上述解决方案,但没有效果。这里的问题是我可以从Windows PowerShell命令提示符运行PSSession命令。但在我的.Net应用程序中不起作用。当我调用PowerShell命令时,它返回0计数。”var result=ps.Invoke();“var结果中的计数为0这些解决方案如何?它起作用了。我现在可以连接到powershell并创建会话。但现在的另一个问题是,当我尝试使用“New mailbox”命令创建邮箱时,它返回错误“找不到与参数名称”标识匹配的参数”“。我已经在新的帖子上发布了这个问题。添加
string
PSCredential
会产生什么结果?添加
string
PSCredential
会产生什么结果?