通过C#交换PowerShell命令

我正在使用C#发送与Exchange交互的PowerShell命令。 我有一个名为initconnection的方法,它设置我与Exchange的连接。

当我单击一个按钮时,我会调用另一种方法,该按钮将在建立连接后向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 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(); } 

这是我在创建连接后单击按钮时调用的方法,但它不起作用。

您必须将Exchange管理单元添加到您的运行空间。 看一下开发人员的Exchange

如果“runspace”不存在,则说明了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); 

创建并导入会话。

 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()等链接:

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

在Get_Mailboxes()中使用它

 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();