2012-05-13 85 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();