• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10413
  • Last Modified:

Suppressing warnings in a PowerShell script

I am writing a script that moves Exchange 2010 mailboxes using the new-moverequest cmdlet. Whenever one uses the -baditemlimit parameter, the cmdlet displays a warning in yellow text. I want to suppress the warning. In the code snippet below, I have tried setting the warningpreference value as well as using the -warningaction parameter for the cmdlet itself as you can see. In addition, I have also tried setting the warningpreference value by just typing it into the powershell window prior to running the .ps1 file containing the code below.

Even with all three of those things put together, I still get the yellow warning text for each invocation of the commandment, meaning it appears dozens of times. How do I get rid of it? If someone could please show me how the code below needs to be edited, I would appreciate it.

Thanks.

$WarningPreference = "SilentlyContinue"
$userList = Import-Csv $csvfile 

foreach ($user in $userList)
{
    write-host "Creating move request for" $user.identity "to database" $targetDB
    new-moverequest -identity $user.Identity  -targetdatabase $targetdb -baditemlimit 100 -acceptlargedataloss -warningaction "SilentlyContinue"
}

Open in new window

0
Julian123
Asked:
Julian123
1 Solution
 
Robert SchuttSoftware EngineerCommented:
Are you using PowerShell 1.0? Apparently the warningAction option only comes in v2. But I can't try it out here.
0
 
QlemoC++ DeveloperCommented:
How about piping stderr to null? That is:
$userList = Import-Csv $csvfile 
foreach ($user in $userList)
{
    write-host "Creating move request for" $user.identity "to database" $targetDB
    new-moverequest -identity $user.Identity  -targetdatabase $targetdb -baditemlimit 100 -acceptlargedataloss 2> nul
}

Open in new window

0
 
Julian123Author Commented:
It is powershrll v2 on w2k8 r2.

I can't pipe all error output as I need to see errors, just suppress the warnings.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
soostibiCommented:
I do not exactly know why suppressing warnings does not work as it should, but here is a workaround (hope it woks, I could not test this exact case, but tested a very similar one):
$userList = Import-Csv $csvfile   
$s = Get-PSSession
  
foreach ($user in $userList)  
{  
    write-host "Creating move request for" $user.identity "to database" $targetDB  
    Invoke-Command -Session $s -ScriptBlock {new-moverequest -identity $args[0] -targetdatabase $args[1] -baditemlimit 100 -acceptlargedataloss -warningaction "SilentlyContinue"} -ArgumentList $user.identity, $targetdb
  
}

Open in new window

0
 
omnipower321Commented:
You can use $WarningPreference = "SilentlyContinue".
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now