Servers uptime script

Hi Readers,

Today I am sharing a script that you can us to email you the uptime report of servers in your enviornment.

I have found this script on internet but I have modified it to get the report in HTML & email the report.

There are two scripts :-

1. uptime.ps1 (thanks to its creator)

2. runuptime.ps1

3. Computer.txt (contains computer lists)

Place all three to same folder (C:\Patching\uptime)

Now change the email variables & smtp servers accordingly in runuptime.ps1 & see the magic.

 

——————————————————————————————————————————————————————–

#############################################################################
# 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 = 0
    $Computerobj = “” | select ComputerName, Uptime, LastReboot
    $wmi = 0
    $wmi = Get-WmiObject -ComputerName $computer -Query “SELECT LastBootUpTime FROM Win32_OperatingSystem”
    $now = 0
    $now = Get-Date
    $boottime = 0
    $boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
    $uptime = 0
    $uptime = $now – $boottime
    $d = 0
    $d =$uptime.days
    $h = 0
    $h =$uptime.hours
    $m = 0
    $m =$uptime.Minutes
    $s = 0
    $s = $uptime.Seconds
    $Computerobj.ComputerName = $computer
    $Computerobj.Uptime = “$d Days $h Hours $m Min $s Sec”
    $Computerobj.LastReboot = $boottime
    $Computerobj  
}

————————————————————————————————————————————————————————

Runuptime.ps1

————————————————————————————————————————————————————————

###################################################################
##           Script to get uptime of computers                   ##
##           Author: Vikas Sukhija                     ##
##           Date: 10-01-2012                          ##
##  This scripts is used for find update time of servers         ##
###################################################################

.\uptime.ps1 -computerlist C:\Patching\uptime\computer.txt |  Export-Csv uptime.csv
$a = “<style>”
$a = $a + “BODY{background-color:white;}”
$a = $a + “TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;width:40%}”
$a = $a + “TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle;}”
$a = $a + “TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod;text-align:center;}”
$a = $a + “</style>”

 

 
Import-csv C:\Patching\uptime\uptime.csv | ConvertTo-HTML -head $a -body “<H2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Uptime Report &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</H2>”| Out-File C:\Patching\uptime\uptime.html

 
$message = new-object System.Net.Mail.MailMessage(“uptime@xxxxxxxxxxxxxx.com“, “vikas.sukhija@xxxxxxxxxxxxxxx.com“)
$message.IsBodyHtml = $True
$message.Subject = “Uptime Report Messaging ”
$smtp = new-object Net.Mail.SmtpClient(“smtp server“)
$body = get-content “C:\Patching\uptime\uptime.html”
$message.body = $body
$smtp.Send($message)

 

####################################################################################

Regards

Sukhija Vikas

Advertisements

One thought on “Servers uptime script

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s