我收到 CmdletInvocationException 未处理,无法调用此函数,因为当前主机未实现它。
当我用 get 命令替换 Export-Mailbox cmdlet 作为测试时,我没有收到任何错误。
这是我的示例代码。当我运行 sScript1 时,它运行得很好,当我运行 sScript2 时,我得到了错误。当我在我的测试机器上执行 get-ex 命令时,我找不到 export-mailbox cmdlet。
public void RunPowerShell()
{
Cursor.Current = Cursors.WaitCursor;
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
Runspace runSpace;
//create the runspace
runSpace = RunspaceFactory.CreateRunspace(rsConfig);
runSpace.Open();
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
Pipeline pipeLine = runSpace.CreatePipeline();
//Add-MailboxPermission -Identity " + FromEmailAccount.Text + " -User Administrator -AccessRights FullAccess
String sScript1 = "Add-MailboxPermission -Identity " + ToEmailAccount.Text + " -User Administrator -AccessRights FullAccess";
String sScript2 = "Export-Mailbox -Identity " + FromEmailAccount.Text + " -TargetMailbox " + ToEmailAccount.Text + " -TargetFolder " + ToEmailAccount.Text + " -Confirm:$false";
//pipeLine.Commands.AddScript(sScript1);
pipeLine.Commands.AddScript(sScript2);
Collection<PSObject> commandResults = pipeLine.Invoke();
//loop through the results of the command and load the SamAccountName into the list
foreach (PSObject results in commandResults)
{
//MessageBox.Show(results.ToString(), @"test");
}
pipeLine.Dispose();
runSpace.Close();
MessageBox.Show(@"complete", @"Done");
Cursor.Current = Cursors.Default;
}