2012-05-12 36 views
3

我在C#中有以下代碼,我用它來連接到通過PowerShell進行交換。PowerShell和在C交換#

以下代碼可以正常工作,但是爲了使用交換cmdlet,還需要一條命令。

這是我現在的代碼。

Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); 
PowerShell powershell = PowerShell.Create(); 

PSCommand command = new PSCommand(); 
command.AddCommand("New-PSSession"); 
command.AddParameter("ConfigurationName", "Microsoft.Exchange"); 
command.AddParameter("ConnectionUri", new Uri("https://ps.outlook.com/powershell/")); 
command.AddParameter("Credential", creds); 
command.AddParameter("Authentication", "Basic"); 
command.AddParameter("AllowRedirection"); 

powershell.Commands = command; 

try 
{ 
    runspace.Open(); 
    powershell.Runspace = runspace; 

    Collection<PSObject> commandResults = powershell.Invoke(); 

    StringBuilder sb = new StringBuilder(); 

    foreach (PSObject ps in commandResults) 
    { 
     sb.AppendLine(ps.ToString()); 
    } 

    sb.AppendLine(); 

    lbl.Text += sb.ToString(); 
} 
finally 
{ 
    // dispose the runspace and enable garbage collection 
    runspace.Dispose(); 
    runspace = null; 
    // Finally dispose the powershell and set all variables to null to free 
    // up any resources. 
    powershell.Dispose(); 
    powershell = null; 
} 

我的問題是,我仍然需要運行命令import-pssession $session其中$session是我的第一個命令的輸出。但是我不知道我怎麼可以聲明輸出變量$會話或喜歡的東西:

PSCommand command = new PSCommand(); 
command.AddCommand("Import-PSSession"); 
command.AddParameter("Session", #Not sure how to put session info which is what the first command produces into here.); 
+0

你有沒有解決過這個問題? 使用powershell__2作爲PowerShell的= PowerShell.Create() command.AddScript( 「進口的PSSession -Session $ RA」) powershell__2.Commands =命令 powershell__2.Runspace =: – Kjensen

回答

1

嘗試以下TechNet博客「使用本地運行空間(腳本編寫遠程類)遠程請求」一節 http://blogs.technet.com/b/exchange/archive/2009/11/02/3408653.aspx

我相信你正在努力實現的是:

// Set the runspace as a local variable on the runspace 
    powershell = PowerShell.Create(); 
    command = new PSCommand(); 
    command.AddCommand("Set-Variable"); 
    command.AddParameter("Name", "ra"); 
    command.AddParameter("Value", result[0]); 
    powershell.Commands = command; 
    powershell.Runspace = runspace; 
    powershell.Invoke(); 

凡結果[0]是創建的第一個遠程會話的結果。讓我知道這是否有幫助。

+0

然後你可以爲了使用它導入會話運行空間 powershell__2.Invoke() End Using End –

3

您可以嘗試使用創建遠程運行空間來代替,下面給出了一個示例。您可以參考以下文章http://msdn.microsoft.com/en-us/library/windows/desktop/ee706560(v=vs.85).aspx

string schemaURI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"; 
Uri connectTo = new Uri("https://ps.outlook.com/powershell/"); 
PSCredential credential = new PSCredential(user,secureStringPassword); // the password must be of type SecureString 
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo,schemaURI, credential); 
connectionInfo.MaximumConnectionRedirectionCount = 5; 
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; 

try 
{ 
    Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo); 
    remoteRunspace.Open(); 
} 
catch(Exception e) 
{ 
    //Handle error 
}