Solved

Powershell Error Checking

Posted on 2013-01-08
3
324 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
[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
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 70

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

Industry Leaders: 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!

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

734 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