0

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;
    }
4

1 に答える 1

0

これは別の投稿で見つけましたが、これは私のエラーで機能しました。

そのコマンドが通常確認を求めるプロンプトを表示する場合は、次のいずれかを行う必要があります。

1) Set -Confirm:$false as a parameter (and possibly -Force as well)
2) Set $ConfirmPreference = "None" before calling Set-Mailbox (and possibly -Force too)
3) Create a Host and implement the Confirm functionality ;-)

Jaykulによる最初の投稿/回答

于 2012-06-22T15:14:35.973 に答える