Solved

Powershell help

Posted on 2013-12-03
5
301 Views
Last Modified: 2013-12-11
Hi all,

Can someone explains line by line...i know it just try to get the uptime but need explanation and i want to knw what it does...


Program:


$computer = read-host "Please type in computer name you would like to check uptime on"

$lastboottime = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime

$sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime)
 
Write-Host "$computer has been up for: " $sysuptime.days "days" $sysuptime.hours "hours" $sysuptime.minutes "minutes" $sysuptime.seconds "seconds"
0
Comment
Question by:sekar_raj32
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
Radek Baranowski earned 250 total points
Comment Utility
here you go:

make user input computer name to check uptime (like hostname or domainname)
$computer = read-host "Please type in computer name you would like to check uptime on"

Open in new window


fetch LastBootUpTime variable from $Computername given in previous step, using Win32_OperatingSystem object class
$lastboottime = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime

Open in new window

calculate uptime by checking current timestamp and subtracting from it timestamp of the last bootup time (got in the prev step) converted to comparable format
$sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime) 
 

Open in new window

output the uptime result, using appropriate format options
Write-Host "$computer has been up for: " $sysuptime.days "days" $sysuptime.hours "hours" $sysuptime.minutes "minutes" $sysuptime.seconds "seconds"

Open in new window

0
 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
Comment Utility
Some additional explanation for the 2nd and 3rd commands.
The LastBootupTime returned from the WMI query is a string in a format like "20101019201512.000000-360".  This needs to be converted to a datetime object to be able to perform useful operations with it.  That's the reason for the [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime) part.  "ToDateTime" is a static method for the "[System.Management.ManagementDateTimeconverter]" .Net class.
Next, when you subtract one datetime object from another you will get a timespan object (that is what $sysuptime is), which has the properties which are referenced by the Write-Host command.

Another way to do the same thing
$wmi = Get-WmiObject -Class Win32_OperatingSystem -computername $computer -Property LastBootUpTime
$sysuptime = (Get-Date) – $wmi.ConvertToDateTime($wmi.LastBootUpTime)

Open in new window


See here for some more detail.
http://technet.microsoft.com/en-us/magazine/2008.12.heyscriptingguy.aspx?pr=PuzzleAnswer
0
 
LVL 7

Author Closing Comment

by:sekar_raj32
Comment Utility
d
0
 
LVL 7

Author Comment

by:sekar_raj32
Comment Utility
mn
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
q
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
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 …
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

6 Experts available now in Live!

Get 1:1 Help Now