Error handling in a Powershell script

Greetings Powershell Experts,

I have written a small Powershell script (with the help of various Experts here on Experts-Exchange) that basically allows me to input a department in our organization and generate a list of the users in that department along with their mailbox size.  The code for the script is as follows:

$Department = Read-Host "What department do you want the mailbox size report for?"
$Users = Get-user -Filter {Department -like $Department} | Select-Object SamAccountName
$results = foreach ($user in $Users) {
Get-MailboxStatistics $User.samaccountname | Select-object @{n="User";e={$_.DisplayName}}, @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}}
$Results | Format-Table -AutoSize

This is providing the information that I want in format that I want.  The only thing wrong is if a user has never logged into their mailbox, they are not included in the output.  In the powershell console I see a warning that states:

WARNING: There is no data to return for the specified mailbox 'routie', because it has not been logged on to.

Is there a way to handle this warning in my script?  

As always, thanks in advance.

Nick
LVL 1
ndalmolin_13Asked:
Who is Participating?
 
slidingfoxCommented:
Give this a whirl. Any users that haven't logged on before will be written to a file in C:\Temp called UsersNotLoggedOnYet.txt. This should supress the warning message using the -WarningAction parameter.

$Department = Read-Host "What department do you want the mailbox size report for?"
$Users = Get-user -Filter {Department -like $Department} | Select-Object SamAccountName
$results = @()
foreach ($user in $Users) {
    $mailboxstats = Get-MailboxStatistics $User.samaccountname -WarningAction SilentlyContinue | Select-object @{n="User";e={$_.DisplayName}}, @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}} 
    
    if ($mailboxstats) {
        $results += $mailboxstats
    } else {
        Add-Content "C:\Temp\UsersNotLoggedOnYet.txt" "$user"
    }
}
$Results | Format-Table -AutoSize

Open in new window

0
 
Rajitha ChimmaniCommented:
If you are using Powershell 2.0 you can add -WarningAction SilentlyContinue to Get-MailboxStatistics command.
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
Not sure but if something like this will help

$warningpreference = "SilentlyContinue"

http://sus42.wordpress.com/2010/12/30/start-the-microsoft-exchange-2010-shell/

Now we look a litte bit further. The variable $warningpreference defines what happens if a script uses the write-warning cmdlet in order to make some output. If you set this variable to the value of silentlycontinue the output is suppressed. The default of this variable is Continue whitch means that warningmessages are displayed in yellow color on the console. If you want to get more details on preference variables you can read the PowerShell help

- Rancy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.