Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell Error Checking

Posted on 2013-01-08
3
Medium Priority
?
335 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 71

Accepted Solution

by:
Qlemo earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Loops Section Overview
Screencast - Getting to Know the Pipeline

782 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