powershell test/WhatIf question

Posted on 2014-08-14
Last Modified: 2014-08-14
I've written a script that will perform a get-mailbox, then add a new domain alias to each of the results.  However I want to test it.  I've seen google search results saying that this can be accomplished with a -WhatIf such as:

c:\>.\somescriptfile.ps1 -WhatIf

However that generate an error complaining about the whatif part.

Heres the code Im using:

$users = get-mailbox
foreach ($a in $users) {$a.emailaddresses.Add("smtp:$($a.alias)")}
$users | %{set-mailbox $_.Identity -emailaddresses $_.emailaddresses}

Open in new window

Not sure what I could put a -whatif statement in here... I tried on a new line at the bottom but it failed.
Question by:Ben Hart
    LVL 38

    Accepted Solution

    You have to write your script to support parameters like -whatif.  Check out [cmdletbinding()].
    The Set-Mailbox cmdlet supports the -whatif parameter by itself though.
    LVL 14

    Author Comment

    by:Ben Hart
    Figured it out.  The -WhatIf has to be inside the last } on the last line.  Thanks, you pointed me in the right direction.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    "Migrate" an SMTP relay receive connector to a new server using info from an old server.
    Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
    The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now