CmdletInvocationException が処理されませんでした。現在のホストが実装していないため、この関数を呼び出すことができません。
テストとして Export-Mailbox コマンドレットを get-command に置き換えると、エラーは発生しません。
これが私のサンプルコードです。sScript1 を実行すると問題なく動作し、sScript2 を実行するとエラーが発生します。テスト マシンで get-excommand を実行したところ、export-mailbox コマンドレットが見つかりませんでした。
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;
}