2

ASP.NET を使用して、Windows 7 から Windows Server 2008 R2 (VMware にインストールされている) に PowerShell をリモート接続したいと考えています。

これは私のコードです:

    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() では、このエラーが発生することがあります

リモート サーバーへの接続に失敗し、次のエラー メッセージが表示されました: アクセスが拒否されました。詳細については、about_Remote_Troubleshooting ヘルプ トピックを参照してください。

そして時々このエラー:

エラー: クライアントは、要求で指定された宛先に接続できません。宛先のサービスが実行中であり、要求を受け入れていることを確認します。

読みabout_Remote_Troubleshootingましたが、これらのエラーが発生する理由がわかりません。誰でも私を助けることができますか?

4

1 に答える 1

1

両方のマシンが同じドメインにあるとおっしゃいましたが、ローカル管理者アカウントを使用しようとしているようです。それは問題かもしれませんが、必ずしもそうではありません。これを複製するために、VM に Win2k8R2 をセットアップし、次の手順に従いました。

手順 1 と 2 は Server 2008 R2 Core インストールに適用されます。完全にインストールしている場合は、#3 に進んでください。

  1. 次のコマンドを実行して、.NET2 と PowerShell をインストールします (cmd.exe 内)。
    • DISM /Online /Enable-Feature /Featurename:NetFx2-ServerCore
    • DISM /Online /Enable-Feature /FeatureName:MicrosoftWindowsPowerShell
  2. いくつかの便利な PS CmdLets をインストールします (cmd.exe 内)。
    • DISM /online /enable-feature /featurename=ServerManager-PSH-Cmdlets
    • DISM /online /enable-feature /featurename=BestPractices-PSH-Cmdlets
  3. リモート シェルを有効にする (powershell.exe 内)
    • Set-Excutionpolicy RemoteSigend
    • 設定-SMRemoting.ps1 -force -enable

そして、私はあなたのサンプルコードを取りましたが、実際には大幅な変更はしていません

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

あなたが使用しているものではない場合に備えて、dll参照を追加しましたC:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll

そして、それはうまくいきました。したがって、それはあなたのコードではなく、資格情報、またはターゲット マシンのリモート セットアップのいずれかであると私は感じています。

于 2012-08-17T13:56:44.087 に答える