PS Script to prompt for a computer name to obtain up time information

Good morning everyone and please excuse the interruption.

I found this particular script online that when executed can tell me the uptime of a computer (from the looks of it, it only works on the computer it is run on)

Is there a way to modify the script so it can do the following:

  1. prompt me to enter a computer name
  2. display the uptime of said computer entered

This script says I would need to insert a line with all the computer names i want it to look for, which to me, is too much info.

#############################################################################
# Get-Uptime.ps1
# This script will report uptime of given computer since last reboot.
# 
# Pre-Requisites: Requires PowerShell 2.0 and WMI access to target computers (admin access).
#
# Usage syntax:
# For local computer where script is being run: .\Get-Uptime.ps1.
# For list of remote computers: .\Get-Uptime.ps1 -ComputerList "c:\temp\computerlist.txt"
#
# Usage Examples:
#
# .\Get-Uptime.ps1 -Computer ComputerName
# .\Get-Uptime.ps1 -ComputerList "c:\temp\computerlist.txt" | Export-Csv uptime-report.csv -NoTypeInformation
#
# Last Modified: 3/20/2012
#
# Created by 
# Bhargav Shukla
# http://blogs.technet.com/bshukla
# http://www.bhargavs.com
# 
# DISCLAIMER
# ==========
# THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE 
# RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#############################################################################
#Requires -Version 2.0

param 
(
	[Parameter(Position=0,ValuefromPipeline=$true)][string][alias("cn")]$computer,
	[Parameter(Position=1,ValuefromPipeline=$false)][string]$computerlist
)

If (-not ($computer -or $computerlist))
{
	$computers = $Env:COMPUTERNAME
}

If ($computer)
{
	$computers = $computer
}

If ($computerlist)
{
	$computers = Get-Content $computerlist
}

foreach ($computer in $computers) 
{
	$Computerobj = "" | select ComputerName, Uptime, LastReboot
	$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
	$now = Get-Date
	$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
	$uptime = $now - $boottime
	$d =$uptime.days
	$h =$uptime.hours
	$m =$uptime.Minutes
	$s = $uptime.Seconds
	$Computerobj.ComputerName = $computer
	$Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
	$Computerobj.LastReboot = $boottime
	$Computerobj	
}

April 22nd, 2015 10:53am

You can use Invoke-Command and pass the script as a script block to it. This will let you run it across multiple computers using PS Session

https://technet.microsoft.com/en-us/library/hh849719.aspx

  • Proposed as answer by Aditi Satam 16 hours 13 minutes ago
Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 10:56am

Thank you for your response

I am still learning this so perhaps you can show me an example how to do that?

April 22nd, 2015 11:00am

Is there a way to modify the script so it can do the following:
  1. prompt me to enter a computer name
  2. display the uptime of said computer entered

1 - use Read-Host to prompt for input.

2 - use a calculated property:

https://technet.microsoft.com/en-us/library/ff730948.aspx

You can use Invoke-Command and pass the script as a script block to it. This will let you run it across multiple computers using PS Session

Completely unnecessary. Script is already querying remote machines via WMI.

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 11:01am

For the Read-Host, do I place that before or after the parameters?

and for the calcuated properties, wouldnt that require a local txt file to read from to get that info?

April 22nd, 2015 11:03am

For the Read-Host, do I place that before or after the parameters?

After, but before the if tests.

and for the calcuated properties, wouldnt that require a local txt file to read from to get that info?

No, it doesn't. Just use the current $computer variable as the expression.

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 11:07am

i think i understand so instead of the parameter where its reading from a computer list i should remove that and use the calculated properties as a param?

The get-childitem command doesnt show me anything other than how to list a directory structure or read from a file on a specific location so i am stumped here
  • Edited by Storo1975 15 hours 34 minutes ago
April 22nd, 2015 11:21am

i think i understand so instead of the parameter where its reading from a computer list i should remove that and use the calculated properties as a param?

No, the calculated property is only for output. Here's a quick example:

$computerList = Read-Host 'Enter computer name'

foreach ($computer in $computerList) {

    Get-WmiObject Win32_OperatingSystem -ComputerName $computer |
        Select @{N='ComputerName';E={$computer}},
               @{N='LastBootupTime';E={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime)}},
               @{N='Uptime';E={(Get-Date) - ([System.Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime))}}
}

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 11:37am

ahh that makes much more sense now. I will play around with it.

Thank you

April 22nd, 2015 12:54pm

Cheers, you're very welcome.
Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 1:06pm

You can use Invoke-Command and pass the script as a script block to it. This will let you run it across multiple computers using PS Session

https://technet.microsoft.com/en-us/library/hh849719.aspx

  • Proposed as answer by Aditi Satam Wednesday, April 22, 2015 2:55 PM
April 22nd, 2015 2:55pm

i think i understand so instead of the parameter where its reading from a computer list i should remove that and use the calculated properties as a param?

The get-childitem command doesnt show me anything other than how to list a directory structure or read from a file on a specific location so i am stumped here
  • Edited by Storo1975 Wednesday, April 22, 2015 3:34 PM
Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 3:19pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics