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 コマンドレットのサポートを追加する必要があるということですか?
このコマンドを機能させる代替手段はありますか?
どんな助けでも大歓迎です。
ありがとう、
マニッシュ