Solved

Powershell help

Posted on 2013-12-03
5
309 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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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