从 C# 远程连接到 powershell [英] Remote connect to powershell from C#

查看:88
本文介绍了从 C# 远程连接到 powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ASP.NET 将 Windows 7 中的 powershell 远程连接到 Windows Server 2008 R2(安装在 VMware 中).

I want to remote connect powershell from windows 7 to windows server 2008 R2 (that's installed in VMware) with ASP.NET.

这是我的代码:

    string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
    var target = new Uri("http://win-qkheb9s51i8/wsman");

    Pipeline p = runSpace.CreatePipeline();
    SecureString passing = new SecureString();
    string password = "A123456a";

    foreach (char c in password)
    {
        passing.AppendChar(c);
    }
    passing.MakeReadOnly();

    var cred = new PSCredential(@"win-qkheb9s51i8\Administrator", passing);

    var connectionInfo = new WSManConnectionInfo(target, shell, cred);
    connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.
    connectionInfo.OpenTimeout = 1 * 60 * 1000;
   runSpace = RunspaceFactory.CreateRunspace(connectionInfo);
   runSpace.Open();

但在 runSpace.open() 中有时会出现此错误

but in runSpace.open() sometimes give this error

连接到远程服务器失败并显示以下错误消息:访问被拒绝.有关更多信息,请参阅about_Remote_Troubleshooting 帮助主题.

Connecting to remote server failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.

有时会出现此错误:

错误:客户端无法连接到指定的目的地要求.验证目标上的服务是否正在运行并且是接受请求.

ERROR: The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests.

我阅读了about_Remote_Troubleshooting,但我不明白为什么会出现这些错误.有人可以帮我吗?

I read about_Remote_Troubleshooting but I can't see why these errors occur. Can anyone help me?

推荐答案

好吧,你说两台机器都在同一个域中,但我看到你正在尝试使用本地管理员帐户.这可能是一个问题,但不一定.为了复制这一点,我在虚拟机上设置了 Win2k8R2 并按照以下步骤操作:

Ok so you said that both machines are on the SAME domain, but I see you are trying to use the local admin account. That may be an issue but not necessarily. To replicate this, I set up Win2k8R2 on a VM and followed the following steps:

步骤 1 和 2 适用于 Server 2008 R2 核心安装.如果您有完整安装,只需跳到 #3.

  1. 运行以下命令来安装 .NET2 &PowerShell(在 cmd.exe 中)
    • DISM/Online/Enable-Feature/Featurename:NetFx2-ServerCore
    • DISM/Online/Enable-Feature/FeatureName:MicrosoftWindowsPowerShell
  • DISM/online/enable-feature/featurename=ServerManager-PSH-Cmdlets
  • DISM/online/enable-feature/featurename=BestPractices-PSH-Cmdlets
  • Set-Excutionpolicy RemoteSigend
  • Configure-SMRemoting.ps1 -force -enable

然后我拿走了你的示例代码,我并没有对它做任何实质性的改变

And then I took your sample code, I didn't really make any substantial changes to it

        Console.Write("Target: ");
        var target = Console.ReadLine();
        Console.Write("User: ");
        var user = Console.ReadLine();
        user = string.Format("{0}\\{1}", target, user);
        string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
        var targetWsMan = new Uri(string.Format("http://{0}:5985/wsman", target));

        using (var passing = new SecureString())
        {
            Console.Write("Password: ");
            var cki = default(ConsoleKeyInfo);
            do
            {
                cki = Console.ReadKey(true);
                if (cki.Key == ConsoleKey.Enter)
                    Console.Write(cki.KeyChar);
                else
                    passing.AppendChar(cki.KeyChar);
            }
            while (cki.Key != ConsoleKey.Enter);
            passing.MakeReadOnly();

            var cred = new PSCredential(user, passing);

            var connectionInfo = new WSManConnectionInfo(targetWsMan, shell, cred);
            connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.
            connectionInfo.OpenTimeout = 1 * 60 * 1000;
            using (var runSpace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                var p = runSpace.CreatePipeline();
                runSpace.Open();
                Console.WriteLine("Connected to {0}", targetWsMan);
                Console.WriteLine("As {0}", user);
                Console.Write("Command to run: ");
                var cmd = Console.ReadLine();
                p.Commands.Add(cmd);
                var returnValue = p.Invoke();
                foreach (var v in returnValue)
                    Console.WriteLine(v.ToString());
            }
        }

        Console.WriteLine("End...");
        Console.ReadLine();

我确实添加了对 C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll 的 dll 引用,以防万一它不是您使用的.

I did add a dll reference to C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll just in case its not what you are using.

然后它起作用了.所以我的感觉是这不是你的代码,而是你的凭据,或者目标机器上的远程设置.

And then it worked. So my feeling is that its not your code but either your credentials, or the remote setup on the target machine.

这篇关于从 C# 远程连接到 powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆