2012-05-13 118 views
1

我正在使用C#發送與Exchange交互的PowerShell命令。我有一個名爲initconnection的方法,它建立了與Exchange的連接。通過C#交換PowerShell命令

我有另一種方法,當我點擊一個按鈕後,會在連接建立後向powershell發送命令。但是我無法繼續創建的連接。當我嘗試運行一個命令時說它沒有找到命令。更可能是因爲它沒有交換cmdlet。

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

runspace.Open(); 

Pipeline pipeline = runspace.CreatePipeline(); 
pipeline.Commands.AddScript("Set-ExecutionPolicy Unrestricted -Scope process -Force;$password = ConvertTo-SecureString -AsPlainText -Force " + password + ";$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist " + username + ",$password;$LiveCred = Get-Credential -Credential $mycred; $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection; Import-PSSession $Session"); 
// pipeline.Commands.Add("Out-String"); 

pipeline.Invoke(); 
mpeAdd.Hide(); 

這是創建連接的initconnection方法。

protected void Get_Mailboxes(object sender, EventArgs e) { 

    PowerShell powershell = PowerShell.Create(); 
    PSCommand command = new PSCommand(); 
    command = new PSCommand(); 
    command.AddCommand("Get-Mailbox"); 

    powershell.Commands = command; 
    powershell.Runspace = runspace; //Also it says runsapce doesn't exist in this context. 
    Collection<PSObject> commandResults = powershell.Invoke(); 

    StringBuilder sb = new StringBuilder(); 

    ArrayList boxesarray = new ArrayList(); 

    foreach (PSObject ps in commandResults) 
    { 
     boxesarray.Add(ps.Properties["Alias"].Value.ToString()); 
    } 

    boxes.DataSource = boxesarray; 
    boxes.DataBind(); 
} 

這是我在創建連接後單擊按鈕時調用的方法,但它不起作用。

回答

0

如果「運行空間」不存在,則說明Get-Mailbox命令失敗的原因。您可以在initConnection方法中創建一個PowerShell實例,並在需要的地方使用它,而不是管理運行空間。注意這是用本機代碼而不是腳本顯示的。

ps = PowerShell.Create(); 

設置執行策略。

ps.ClearCommands() 
.AddCommand("Set-ExecutionPolicy") 
.AddParameter("Scope", "Process") 
.AddParameter("ExecutionPolicy", "Unrestricted")  
.AddParameter("Confirm", false) 
.AddParameter("Force", true) 
.Invoke(); 

創建憑證。請注意,您不需要調用Get-Credential。

SecureString pass; 
var creds = new PSCredential(username, pass); 

創建並導入會話。

public static PowerShell ClearCommands(this PowerShell ps) 
{ 
    if (ps.Commands != null) 
     ps.Commands.Clear(); 

    return ps; 
} 

使用它在Get_Mailboxes()

var newSession = ps.ClearCommands() 
.AddCommand("New-PSSession") 
.AddParameter("ConfigurationName", "Microsoft.Exchange") 
.AddParameter("ConnectionUri", "https://ps.outlook.com/powershell/") 
.AddParameter("Credential", creds) 
.AddParameter("Authentication", "Basic") 
.AddParameter("AllowRedirection", true) 
.Invoke(); 

var session = newSession[0]; 
var import = ps.ClearCommands() 
.AddCommand("Import-PSSession") 
.AddParameter("Session", session) 
.Invoke(); 

ps.ClearCommands()是一個擴展方法,添加所以可以用AddCommand(),AddParameter()等鏈

protected void Get_Mailboxes(object sender, EventArgs e) { 

    var commandResults = ps.ClearCommands().AddCommand("Get-Mailbox").Invoke(); 
    StringBuilder sb = new StringBuilder(); 
    ArrayList boxesarray = new ArrayList(); 

    foreach (PSObject ps in commandResults) 
    { 
     boxesarray.Add(ps.Properties["Alias"].Value.ToString()); 
    } 

    boxes.DataSource = boxesarray; 
    boxes.DataBind(); 
} 

當您關閉應用程序,或適當的地方:

ps.ClearCommands() 
.AddCommand("Get-PSSession") 
.AddCommand("Remove-PSSession") 
.Invoke(); 

ps.Dispose();