[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Powershell help

Posted on 2013-12-03
5
Medium Priority
?
338 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
[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 12

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 41

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:Kamalasekar Parthasarathy
ID: 39713233
d
0
 
LVL 7

Author Comment

by:Kamalasekar Parthasarathy
ID: 39713235
mn
0
 
LVL 41

Expert Comment

by:footech
ID: 39713341
q
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

650 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