【问题标题】:Creating PowerShell remote session using C#使用 C# 创建 PowerShell 远程会话
【发布时间】:2019-01-02 08:14:14
【问题描述】:

我一直在尝试使用 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();
}

【问题讨论】:

  • 您希望添加stringPSCredential 会产生什么结果?

标签: c# powershell


【解决方案1】:

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

        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();

【讨论】:

  • 我试过了。但它抛出“无法执行操作,因为操作“NewNotImplementedException at offset 78 in file:line:column :0:0”未实现。”错误。
  • 好的,问题是你是否真的可以建立一个到“servername/poweshell”的远程会话。当您运行 $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri servername/poweshell -Credential credential; Import-PSSession $Session" 直接从 PowerShell,你能返回任何数据吗?顺便说一句,这也有帮助:msdn.microsoft.com/en-us/library/bb332449(v=exchg.80).aspx
  • 我已经尝试了上述解决方案,但无济于事。这里的问题是我可以从 Windows PowerShell 命令提示符运行 PSSession 命令。但同样不适用于我的 .Net 应用程序。当我调用 PowerShell 命令时,它返回 0 个计数。 “var 结果 = ps.Invoke();” var 结果中的计数为 0
  • 那些解决方案怎么样? stackoverflow.com/questions/36236897/…
  • 成功了。我现在可以连接到 powershell 并创建会话。但是现在的另一个问题是,当我尝试使用“New-Mailbox”命令创建邮箱时,它返回错误“找不到与参数名称'Identity”匹配的参数。我已经在新线程上发布了这个问题。 stackoverflow.com/questions/42136343/…
【解决方案2】:

我有一篇文章描述了一种通过 WinRM 从 .NET 运行 Powershell 的简单方法,地址为 http://getthinktank.com/2015/06/22/naos-winrm-windows-remote-management-through-net/,或者您可以直接在 https://github.com/NaosProject/Naos.WinRM 获取代码。

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

如果您只想复制代码,该代码位于单个文件中,它也是一个 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" });

希望这会有所帮助,我已经在自动化部署中使用了一段时间。如果您发现问题,请离开 cmets。

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 2013-04-10
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 2022-01-16
    相关资源
    最近更新 更多