[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 696
  • Last Modified:

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
0
ndalmolin_13
Asked:
ndalmolin_13
1 Solution
 
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
 
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

Featured Post

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

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