?
Solved

Powershell help

Posted on 2013-12-03
5
Medium Priority
?
330 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
[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
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
Radek Baranowski earned 1000 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 40

Assisted Solution

by:footech
footech earned 1000 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 40

Expert Comment

by:footech
ID: 39713341
q
0

Featured Post

Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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