Solved

Powershell Error Checking

Posted on 2013-01-08
3
322 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
ID: 38756830
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 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 38757141
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
ID: 38757189
Thanks this works
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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 …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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