Get a Computers Uptime using PowerShell

1 02 2012

Here is a PowerShell script that will prompt you for a computer name and it will output the uptime.

###########################################################################
#
# NAME: Get Computer Uptime
#
# AUTHOR:  randolph.brady
#
# COMMENT:
#
# VERSION HISTORY:
# 1.0 1/23/2012 – Initial release
#
###########################################################################

$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"





Reboot Computer using PowerShell

1 02 2012

Here is a script you can use to schedule a server reboot or manually reboot one.  There are two arguments used, –server (server name) and –to (who to send the email to).

###########################################################################
#
# NAME: Reboot Server
#
# AUTHOR:  randolph.brady
#
# COMMENT: You will supply the server and email To: parameters
#(Ex. reboot-server.ps1 -server mpbrandolph01 -to randolph.brady@company.com
#
# VERSION HISTORY:
# 1.0 2/1/2012 – Initial release
#
###########################################################################
param ($server, $to)
 
$resource = $ENV:COMPUTERNAME

if (($server -eq $null) -or ($to -eq $null)) {

Write-Host "`nThere are attributes missing from the script. `n
Please use -server and -to to identify the server to reboot `r
and to who the email should be sent. `n"
Exit
}
 
## Generate an email.  If you don’t have an attachment, remove the attachment parameter in Send-Mailmessage
$body = "This is a scripted reboot of $server from $resource."
$from = "networkservices@company.com"
$subject = "Rebooting $server"
$smtpServer = "smtp.company.corp"

Send-MailMessage -To $to -From $from -Subject $subject -Body $body -SmtpServer $smtpServer

Restart-Computer -ComputerName $server -Force








Follow

Get every new post delivered to your Inbox.