Solved

Handling Warnings in Powershell

Posted on 2012-03-22
12
3,929 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
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 8

Expert Comment

by:bchallis
Comment Utility
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
Comment Utility
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:bchallis
Comment Utility
The down side of using Transcript is that it is simply providing information for review rather than getting your code to respond.
0
 

Author Comment

by:spicer411
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

12 Experts available now in Live!

Get 1:1 Help Now