2

C# コード内で、マシン A からリモート ホスト B とのセッションを確立しようとしています。そのために実行空間 API を使用しています。コードスニペットを以下に示します

            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();

            //constructing the vmname parameter here
            vmname = useralias + DateTime.Now.ToString();


            Pipeline pipeline = runspace.CreatePipeline();
            string scripttext = "$secpasswd = ConvertTo-SecureString '222_bbbb' -AsPlainText –Force";
            string scripttext1 = "$mycreds = New-object -typename System.Management.Automation.PSCredential('TS-TEST-09\\Administrator',$secpasswd)";
            string scripttext2  = "$s = New-PSSession -ComputerName TS-TEST-09 -Credential $mycreds";
            //not accepting session string here, only computername acceptable
            **string scripttext3 = "Enter-PSSession -Session $s";**



            //Command cmd = new Command(@"C:\mypath\helper.ps1", true);
            //cmd.Parameters.Add("local_useralias", useralias);
            //cmd.Parameters.Add("local_vmname", vmname);
            //cmd.Parameters.Add("local_datastore", datastoredropdown.Text.ToString());
            //cmd.Parameters.Add("local_architecture", architecturedropdown.Text.ToString());


            pipeline.Commands.AddScript(scripttext);
            pipeline.Commands.AddScript(scripttext1);
            pipeline.Commands.AddScript(scripttext2);
            pipeline.Commands.AddScript(scripttext3);
            //pipeline.Commands.Add(cmd);


            Collection<PSObject> results = pipeline.Invoke();


            runspace.Close();

このコードは、マシン TS-TEST-09 とのセッションに入り、そのマシンに存在するスクリプト helper.ps1 を呼び出すことが期待されます (現在、リモート ホストとのセッションに入ることができないため、その部分はコードでコメント アウトされています) )。

今問題は、-Session パラメータ (scripttext3 で強調表示) を使用してセッション $s に入ることができないことですが、-Computername パラメータを使用して入ることができます。

scripttext3 で -Session パラメータを使用すると発生するエラーは次のとおりです。

invokedSystem.Management.Automation.Internal.Host.InteralHost.GetIHostSupportsInteractiveSession() で

invokedSystem.Management.Automation で。Internal.Host.InteralHost.PushRunspace(実行空間実行空間)

Microsoft.Powershel.Commands.EnterPSSessionCommand.ProcessRecord() で

System.Management.Automation.Cmdlet.DoProcessRecord() で

System.Management.Automation.CommandProcessor.ProcessRecord() で

内部例外スタック トレースの終わり


カスタム PSHost を作成し、このパラメーターを使用して Enter-PSSession コマンドレットのサポートを追加する必要があるということですか?

このコマンドを機能させる代替手段はありますか?

どんな助けでも大歓迎です。

ありがとう、

マニッシュ

4

3 に答える 3

8

リモート セッションを開く最も簡単な方法は次のとおりです。

    string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
    var target = new Uri("http://myserver/wsman");
    var secured = new SecureString();
    foreach (char letter in "mypassword")
    {
        secured.AppendChar(letter);
    }
    secured.MakeReadOnly();

    var credential = new PSCredential("username", secured);
    var connectionInfo = new WSManConnectionInfo(target, shell, credential);

    Runspace remote = RunspaceFactory.CreateRunspace(connectionInfo);
    remote.Open();

    using (var ps = PowerShell.Create())
    {
        ps.Runspace = remote;
        ps.Commands.AddCommand("'This is running on {0}.' -f (hostname)");

        Collection<PSObject> output = ps.Invoke();
    }

リモート実行空間インスタンスからリモート パイプラインを作成することもできますが、新しいPowerShellオブジェクトはこれを行うためのはるかに管理しやすい方法です (powershell v2 以降)。

PowerShell v3 では、他のプロパティが上記と同じデフォルトを採用しているため、プロパティを新規作成しWSManConnectionInfoて設定するだけです。ComputerName残念ながら、これらのプロパティは v2 では読み取り専用であり、上記のように最小値を渡す必要があります。コンストラクターの他のバリアントでは、認証に kerberos/negotiate/credssp などを使用できます。

-オイシン

于 2012-01-19T02:58:39.303 に答える
1

私も大いに役立ったので、解決策についてコメントを残したかったのですが、

しかし、私が見逃していたのは、5985 である WSMan のポートでした。

したがって、この var target = new Uri(" http://myserver/wsman "); var target = new Uri(" http://myserver:5895/wsman ");である必要があります。私の場合

于 2015-07-10T14:48:24.443 に答える
0

試しましたInvoke-Commandか?Enter-PsSessionインタラクティブな使用のためにあります。( を参照help Enter-PsSession -online)。

于 2012-01-18T16:37:48.883 に答える