Solved

Handling Warnings in Powershell

Posted on 2012-03-22
12
4,753 Views
Last Modified: 2013-04-05
I have been attempting to do some maintenance on thousands of exchange mailboxes and would like to base a statement on one of the warnings returned by the get-mailboxstatistics cmdlet.  I'm running get-mailboxstatistics -warningvariable X -identity <alias> | select lastlogontime,storagelimitstatus.  I'm getting the warning "The user hasn't logged on to mailbox '<alias>' (big long SID), so ther is no data to return.  After the user logs on, this warning will no longer appear."  I would like to trap this warning and use it for decision making in my script but I can't seem to get it into the warning variable.   Any suggestions?

thanks
0
Comment
Question by:spicer411
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 8

Expert Comment

by:Brent Challis
ID: 37755193
The preferred way to deal with problems occurring in your code at run time is to use the try/catch construct:
try
{
  your code goes here
}
catch
{
  you deal with the problem here.  The variable $_ contains the exception that occurred
}

You can put as much code as you want to in the try block and if one of the lines fails control goes to the catch block.

Here is the on-line reference:
http://technet.microsoft.com/en-us/library/dd315350.aspx
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 37755312
You can also use Transcript, which would capture all the warnings.

"Start-Transcript filepath" before executing command and "Stop-Transcript" after finishing
0
 
LVL 8

Expert Comment

by:Brent Challis
ID: 37755749
The down side of using Transcript is that it is simply providing information for review rather than getting your code to respond.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:spicer411
ID: 37757731
Is there any reason why the -warningvariable doesn't work at all?  I receive a warning when executing the command but it doesn't terminate the script.  The script keeps processing but I want to collect any warning information so I can do something a little different with the accounts that have never been logged onto.

I've seen the -warningvariable switch all over the place but it seems to not work at all.  Anyone else experience this?
0
 
LVL 9

Expert Comment

by:chrismerritt
ID: 37763632
As above, does the Try Catch statement not work? I use this all the time (granted sometimes more success than others) but even so, worth at least trying it :)

Try
{
	Get-MailboxStatistics -Identity $Mailbox
}
Catch [Exception]
{
	Write-Host -ForeGroundColor "Magenta" "$_"
}
Finally
{
	#Do something here, even if an error occurs if you want to.
}

Open in new window

0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 37848776
A warning is a warning - not an error. It will neither stop the script, nor be captured by a trap or try/catch. You need to use -WarningAction Stop for both to happen (default value is Continue), or set the preference variable $WarningPreference to Stop. In my tests only changing the preference var is working, the -WarningAction seems to be ignored. However, you won't get the warning text, only the error message that the script is stopped because of the preference setting.

The warning variable should nevertheless contain something whenever a warning is printed out, and hence evaluated for checking if there were warnings. This does not seem to work and is reported as a bug, according to http://jdhitsolutions.com/blog/2011/05/warning-signs/, as it seems the setting is not propagated to the underlying Write-Warning commandlet. The -errorVariable works, btw.
Probably that is fixed in PS3, but that isn't released yet.

Sorry, there is no workaround different than to scan the command output, or check for $null in variables/attributes you expect to get back.
0
 

Author Comment

by:spicer411
ID: 39048636
I've requested that this question be deleted for the following reason:

It's been a year since I've been on Experts-Exchange.  I have forgotten about that script.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39048637
Objection: Since this thread contains valuable information about bugs/misbehaviour of PowerShell, and direct advice how to work with warnings arising when executing a script, I reckon it should not get just disposed, but posts accepted. In particular mine ;-).
0
 

Author Comment

by:spicer411
ID: 39048675
So Qlemo,

Are you telling me that you were able to actually capture the warning and examine the text of the warning to make a determination as to which direction you wanted your script to go?  If so, that's what I needed.  I can capture errors and examine their text all day but warnings seem to just disappear into the bitbucket.  It has been a year since I worked with that script.  I must've found another way to handle the situation and moved on.  I quit using Experts Exchange because I've rarely gotten an answer to my questions that could be implemented in my particular situations.  I'm back again now looking to see if I can find an answer to a problem I have with DPM 2010 backing up VM's in a 2012 cluster.  Wish me luck. ;-)
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39048744
This part

Sorry, there is no workaround different than to scan the command output, or check for $null in variables/attributes you expect to get back.
is what I refer to. The warning variable just does not work yet with many cmdlets.
0
 

Author Comment

by:spicer411
ID: 39048753
No problem.  This thread can be a source to help someone else realize that there's a problem with powershell capturing warning text....as you mentioned.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

691 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