Solved

Powershell Error Checking

Posted on 2013-01-08
3
327 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

628 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