Solved

Powershell help

Posted on 2013-12-03
5
305 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
ID: 39694814
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
ID: 39694878
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
ID: 39713233
d
0
 
LVL 7

Author Comment

by:sekar_raj32
ID: 39713235
mn
0
 
LVL 39

Expert Comment

by:footech
ID: 39713341
q
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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

13 Experts available now in Live!

Get 1:1 Help Now