Solved

Powershell Error Checking

Posted on 2013-01-08
3
318 Views
Last Modified: 2013-01-08
I have this script that was pulled off the web.

It works, but halts on WMI connection errors.
How do I write in some error checking?


  Start of script
##

# Helper Function - convert WMI date to TimeDate object
function WMIDateStringToDate($Bootup) {
      [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup)
}

# Main script
$Computer = Get-Content “Servers.txt”
$computers = Get-WMIObject -class Win32_OperatingSystem -computer $computer

foreach ($system in $computers) {
      $Bootup = $system.LastBootUpTime
      $LastBootUpTime = WMIDateStringToDate($Bootup)
      $now = Get-Date
      $Uptime = $now - $lastBootUpTime
      $d = $Uptime.Days
      $h = $Uptime.Hours
      $m = $uptime.Minutes
      $ms= $uptime.Milliseconds

      "{0} Up for: {1} days, {2} hours, {3}.{4} minutes"  -f $system,$d,$h,$m,$ms
}
# End script
0
Comment
Question by:nate3olm
3 Comments
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
You may refer this question to get details on Error handling..
www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_27918873.html
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
Above EE Q is showing you the general ways how to handle errors.

Since Get-WMIObject ignores the -ErrorAction setting for some of the errors (like Access Denied), it is best to query one machine after another. To speed up processing I would test for a connection first. I've also shortened the code snippet, just for fun.
foreach ($computer in Get-Content “Servers.txt”) {
  if (Test-Connection $computer -Count 1 -EA Silentlycontinue) {
    try {
      $system = Get-WMIObject -class Win32_OperatingSystem -computer $computer
      $LastBootUpTime = [System.Management.ManagementDateTimeconverter]::ToDateTime($system.LastBootUpTime)
      $Uptime = (get-date) - $lastBootUpTime
      $d, $h, $m, $ms = $Uptime.Days, $Uptime.Hours, $uptime.Minutes, $uptime.Milliseconds
      write-output "$Computer Up for: $d days, $h hours, $m.$ms minutes"
    }
    catch {}
  }
}

Open in new window

0
 

Author Closing Comment

by:nate3olm
Comment Utility
Thanks this works
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

11 Experts available now in Live!

Get 1:1 Help Now