[英]how to establish and enter a remote session using runspace in powershell
我正在嘗試在C#代碼中從我的機器A與遠程主機B建立會話。 我正在使用runspace 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參數時得到的錯誤是:
at invokedSystem.Management.Automation.Internal.Host.InteralHost.GetIHostSupportsInteractiveSession()
在invokedSystem.Management.Automation。 Internal.Host.InteralHost.PushRunspace(Runspace runspace)
在Microsoft.Powershel.Commands.EnterPSSessionCommand.ProcessRecord()
在System.Management.Automation.Cmdlet.DoProcessRecord()
在System.Management.Automation.CommandProcessor.ProcessRecord()
內部異常堆棧跟蹤的結束
這是否意味着我必須編寫自定義PSHost並使用此參數添加對Enter-PSSession cmdlet的支持?
有沒有其他方法可以使這個命令工作?
任何幫助都感激不盡。
謝謝,
馬尼什
打開遠程會話的最簡單方法是這樣的:
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等進行身份驗證。
-Oisin
我想對解決方案發表評論,因為它對我有很多幫助,
但我遺漏的一件事是WSMan的端口是5985
所以這個var target = new Uri(“ http:// myserver / wsman ”); 應該是var target = new Uri(“ http:// myserver:5895 / wsman ”); 在我的情況下
你試過Invoke-Command
嗎? Enter-PsSession
可供交互使用。 (請參閱help Enter-PsSession -online
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.