Solved

powershell scripting

Posted on 2013-01-28
11
437 Views
Last Modified: 2013-02-05
Need a sample powershell script which will check for all drive present in the server and will give the output as green if it's 80% or below ,Amber if it's 90 -95 % and red if it's 95 > above.

It should also check for SQL services, if it's running then it should display as green,if it's other than running state then it should be red

It should check both drive and service check, if all drives are below 80% and service is not running then it should display as red and mention the reason for displaying the corresponding state.
Sample op:

Server1  D: 89%    Amber
Server2   mssqlservice is not running Red
server3     All checks are good  green

Version i'm using is 1.0
0
Comment
  • 5
  • 3
  • 3
11 Comments
 
LVL 7

Expert Comment

by:Beneford
ID: 38830198
I'm using Nagios for this. It's not an instant set up, but it monitors the things you're asking for (and more) as well as being configurable etc.
It's also free (but you can save yourself the install time by paying...).
Check out www.nagios.org.

It runs on Linux, but you can run it inside a virtual machine on a Windows server.

The monitoring you're after is easily done with NSClient++ running on the Windows Servers
(which Nagios calls to get the status information - see www.nsclient.org).
0
 
LVL 5

Author Comment

by:VIVEKANANDHAN_PERIASAMY
ID: 38831667
sorry,third party tools are not allowed.
0
 
LVL 7

Expert Comment

by:Beneford
ID: 38833215
No 3P tools makes it a little harder.

You can get the raw information using WMIC

eg:
Free disk space:
wmic /node:Server1 logicaldisk get deviceId, Description, FileSystem, Size, FreeSpace

Running SQLServer:
wmic /node:Server1 service where "Name like '%SQL%'" get name, state, status

There are options for colouring the output:
1. Write something that parses the output and adds the ANSI colouring sequences
2. Create a new output format for WMIC that addes ANSI colouring sequences
3. Load the output into an Excel file and have some conditional formatting that does what you need.
0
 
LVL 5

Author Comment

by:VIVEKANANDHAN_PERIASAMY
ID: 38834144
I need a sample script which will help me to build more logic into the script.
0
 
LVL 7

Expert Comment

by:Beneford
ID: 38834262
Can you give more details about how this will be used?

I think Experts Exchange is more about getting expert input to the solution than getting someone to do all the work for you.

If I were doing this, I'd be using cygwin (programs such as awk and sed) but you don't want any (is that right?) 3P tools, which means sticking with what Microsoft provide, which is a bit more restricting than how I normallly work.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 38834397
I had created this script  for reporting disk/Vol information of servers, I have modified it according to your requirement.. Check and let me know if it works for you..
Script will read server information from server.txt..

$fileName = "C:\Temp\diskReport.htm"
If(Test-Path $FileName -ea SilentlyContinue){Remove-Item $FileName}
New-Item -ItemType file -Path C:\Temp -Name Diskreport.htm -Force
$date = ( Get-Date ).ToString('MM/dd/yyyy h:mm:ss tt')
$diskwarning = 10
$diskcritical = 5
$serverlist = GC C:\server.txt

#Function to write the HTML Header to the report

Function writeHtmlHeader
{
param($fileName)
$date = ( Get-Date ).ToString('MM/dd/yyyy h:mm:ss tt')
Add-Content $fileName "<html width=100%>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName "<title>Server Disk Space Check list- Report</title>"
Add-Content $fileName "<style>"
Add-Content $fileName "td {"
Add-Content $fileName "font-family: Tahoma;"
Add-Content $fileName "font-size: 11px;"
Add-Content $fileName "}"
Add-Content $fileName "</style>"
Add-Content $fileName "<head>"
Add-Content $fileName "<body width=100% >"
Add-Content $fileName "<table width='100%' border='1' bordercolor='black'>"
Add-Content $fileName "<tr width='100%' border=2 >"
Add-Content $fileName "<td width='100%' colspan='6' bgcolor='#C7A317' align='center'>"
Add-Content $fileName "<font face='tahoma' size='4' color='#151B54' size='4'><strong>Server Disk Space - Report as on - $date</strong></font>"
Add-Content $fileName "</td>"
Add-Content $fileName "</tr>"
}

#Function to write the heading of the Disk report

Function writeTableHeader
{
param($fileName)
Add-Content $fileName "<tr bgcolor=#999999>"
Add-Content $fileName "<td width='30%' align='center'>Volume/Drive</td>"
Add-Content $fileName "<td width='30%' align='center'>Volume/Drive Label</td>"
Add-Content $fileName "<td width='10%' align='center'>Total Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Used Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Free Space(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Freespace %</td>"
Add-Content $fileName "</tr>"
}

Function writeServiceTableHeader
{
param($fileName)
Add-Content $fileName "<tr bgcolor=#999999>"
Add-Content $fileName "<td width='40%' align='center'>Service Name</td>"
Add-Content $fileName "<td width='30%' align='center'>Displayname</td>"
Add-Content $fileName "<td width='30%' align='center'>Status</td>"
Add-Content $fileName "</tr>"
}

#Function to write the Footer of the report

Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName "</table>"
Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}

#Function to get the Disk space information

Function getinfo
{
foreach ($server in $serverlist)
 {
 Add-Content $fileName "</table>"
 Add-Content $fileName "<table width='100%' border='1' bordercolor='black'>"
 Add-Content $fileName "<tr >"
 Add-Content $fileName "<td width='100%' align='center' bgcolor='#999999' colSpan=6><font face='tahoma' size='2'><strong> $server </strong></font></td>"
 Add-Content $fileName "</tr>"

 writeTableHeader $fileName
 $dp = Get-WmiObject win32_volume -ComputerName $server | where {(($_.DriveType -like "3") -and ($_.caption -like "*:\*"))} | Sort-Object caption
  foreach ($item in $dp)
  {
 Write-Host $item.Caption $item.Label $item.FreeSpace $item.Capacity
 writediskinfo $FileName $item.Caption $item.Label $item.FreeSpace $item.Capacity
 	}
 Add-Content $fileName "</table>"
 Add-Content $fileName "<table width='100%' border='1' bordercolor='black'>"
 Add-Content $fileName "<tr >"
 Add-Content $fileName "<td width='100%' align='center' bgcolor='#999999' colSpan=3><font face='tahoma' size='2'><strong>$("Service Status")</strong></font></td>"
 Add-Content $fileName "</tr>"
 writeServiceTableHeader $fileName
 $Service = Get-Service -ComputerName $Server | ?{$_.DisplayName -like "*SQL*"} | Select Name,DisplayName,Status
 $Service | % {
  Write-Host $_.Name $_.DisplayName $_.Status
  writeServiceinfo $FileName $_.Name $_.DisplayName $_.Status
  }
 }
}

#Function to write the Disk information

Function writeDiskInfo
{
param($fileName,$Caption,$Label,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace - $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,2)
 if ($freePercent -gt $diskwarning)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$caption</td>"
 if ($Label -ne $null)
 {
 Add-Content $fileName "<td>$Label</td>"
 }
 else
 {
 Add-Content $fileName "<td>Local Disk</td>"
 }
 Add-Content $fileName "<td align=center>$totSpace</td>"
 Add-Content $fileName "<td align=center>$usedSpace</td>"
 Add-Content $fileName "<td align=center>$frSpace</td>"
 Add-Content $fileName "<td align=center>$freePercent</td>"
 Add-Content $fileName "</tr>"
 }
 elseif ($freePercent -le $diskcritical)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$caption</td>"
 if ($Label -ne $null)
 {
 Add-Content $fileName "<td>$Label</td>"
 }
 else
 {
 Add-Content $fileName "<td>Local Disk</td>"
 }
 Add-Content $fileName "<td align=center>$totSpace</td>"
 Add-Content $fileName "<td align=center>$usedSpace</td>"
 Add-Content $fileName "<td align=center>$frSpace</td>"
 Add-Content $fileName "<td bgcolor='#F75D59' align=center>$freePercent</td>"
 Add-Content $fileName "</tr>"
 }
 else
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$caption</td>"
 if ($Label -ne $null)
 {
 Add-Content $fileName "<td>$Label</td>"
 }
 else
 {
 Add-Content $fileName "<td>Local Disk</td>"
 }
 Add-Content $fileName "<td align=center>$totSpace</td>"
 Add-Content $fileName "<td align=center>$usedSpace</td>"
 Add-Content $fileName "<td align=center>$frSpace</td>"
 Add-Content $fileName "<td bgcolor='#FBB917' align=center>$freePercent</td>"
 Add-Content $fileName "</tr>"
 }
}

Function writeServiceinfo
{
param($fileName,$Name,$DisplayName,$Status)
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$name</td>"
 Add-Content $fileName "<td>$DisplayName</td>"
 if ($Status -ne "Running")
 {
 Add-Content $fileName "<td bgcolor='#F75D59' align=center>$Status</td>"
 Add-Content $fileName "</tr>"
 }
 Else
 {
 Add-Content $fileName "<td bgcolor='#0A8A0A' align=center>$Status</td>"
 Add-Content $fileName "</tr>"
 }
}
#Function to Send mail using the html report

Function sendEmail
{ param($from,$to,$subject,$smtphost,$filename)
$body = Get-Content $FileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)
}

#writing the information to HTML file using Function's

writehtmlheader $fileName
getinfo
writehtmlfooter $fileName

#sending mail

sendEmail From@domain.com To@Domain.com "Disk Space Report as on - $date" SMTP.Domain.com $fileName

Open in new window

0
 
LVL 5

Author Comment

by:VIVEKANANDHAN_PERIASAMY
ID: 38840493
can you please help me to store the data as html file.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38840922
It saves report file in html format (.htm file). if you want you can change the file name extn in script from htm to html?
0
 
LVL 5

Author Comment

by:VIVEKANANDHAN_PERIASAMY
ID: 38841356
excellent Work!!!Subsun Awesome one.

And i want to display,only server having issue.
0
 
LVL 5

Author Comment

by:VIVEKANANDHAN_PERIASAMY
ID: 38846524
i have modified the code.It works.
$fileName = "C:\Temp\diskReport.htm"
If(Test-Path $FileName -ea SilentlyContinue){Remove-Item $FileName}
New-Item -ItemType file -Path C:\Temp -Name Diskreport.htm -Force
$date = ( Get-Date ).ToString('MM/dd/yyyy h:mm:ss tt')
$diskwarning = 30
$diskcritical = 25
$serverlist = GC C:\server.txt

#Function to write the HTML Header to the report

Function writeHtmlHeader
{
param($fileName)
$date = ( Get-Date ).ToString('MM/dd/yyyy h:mm:ss tt')
Add-Content $fileName "<html width=100%>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName "<title>Server Disk Space Check list- Report</title>"
Add-Content $fileName "<style>"
Add-Content $fileName "td {"
Add-Content $fileName "font-family: Tahoma;"
Add-Content $fileName "font-size: 11px;"
Add-Content $fileName "}"
Add-Content $fileName "</style>"
Add-Content $fileName "<head>"
Add-Content $fileName "<body width=100% >"
Add-Content $fileName "<table width='100%' border='1' bordercolor='black'>"
Add-Content $fileName "<tr width='100%' border=2 >"
Add-Content $fileName "<td width='100%' colspan='6' bgcolor='#C7A317' align='center'>"
Add-Content $fileName "<font face='tahoma' size='4' color='#151B54' size='4'><strong>Server Disk Space - Report as on - $date</strong></font>"
Add-Content $fileName "</td>"
Add-Content $fileName "</tr>"
}

#Function to write the heading of the Disk report

Function writeTableHeader
{
param($fileName)
Add-Content $fileName "<tr bgcolor=#999999>"
Add-Content $fileName "<td width='30%' align='center'>Volume/Drive</td>"
Add-Content $fileName "<td width='30%' align='center'>Volume/Drive Label</td>"
Add-Content $fileName "<td width='10%' align='center'>Total Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Used Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Free Space(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Freespace %</td>"
Add-Content $fileName "</tr>"
}

Function writeServiceTableHeader
{
param($fileName)
Add-Content $fileName "<tr bgcolor=#999999>"
Add-Content $fileName "<td width='40%' align='center'>Service Name</td>"
Add-Content $fileName "<td width='30%' align='center'>Displayname</td>"
Add-Content $fileName "<td width='30%' align='center'>Status</td>"
Add-Content $fileName "</tr>"
}

#Function to write the Footer of the report

Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName "</table>"
Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}

#Function to get the Disk space information

Function getinfo
{
foreach ($server in $serverlist)
 {
 Add-Content $fileName "</table>"
 Add-Content $fileName "<table width='100%' border='1' bordercolor='black'>"
 Add-Content $fileName "<tr >"
 Add-Content $fileName "<td width='100%' align='center' bgcolor='#999999' colSpan=6><font face='tahoma' size='2'><strong> $server </strong></font></td>"
 Add-Content $fileName "</tr>"

 writeTableHeader $fileName
 $dp = Get-WmiObject win32_volume -ComputerName $server | where {(($_.DriveType -like "3") -and ($_.caption -like "*:\*"))} | Sort-Object caption
  foreach ($item in $dp)
  {
 Write-Host $item.Caption $item.Label $item.FreeSpace $item.Capacity
 writediskinfo $FileName $item.Caption $item.Label $item.FreeSpace $item.Capacity
 	}
 Add-Content $fileName "</table>"
 Add-Content $fileName "<table width='100%' border='1' bordercolor='black'>"
 Add-Content $fileName "<tr >"
 Add-Content $fileName "<td width='100%' align='center' bgcolor='#999999' colSpan=3><font face='tahoma' size='2'><strong>$("Service Status")</strong></font></td>"
 Add-Content $fileName "</tr>"
 writeServiceTableHeader $fileName
 $Service = Get-Service -ComputerName $Server | ?{$_.DisplayName -like "*SQL*"} | Select Name,DisplayName,Status
 $Service | % {
  Write-Host $_.Name $_.DisplayName $_.Status
  writeServiceinfo $FileName $_.Name $_.DisplayName $_.Status
  }
 }
}

#Function to write the Disk information

Function writeDiskInfo
{
param($fileName,$Caption,$Label,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace - $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,2)
 if ($freePercent -le $diskcritical)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$caption</td>"
 if ($Label -ne $null)
 {
 Add-Content $fileName "<td>$Label</td>"
 }
 else
 {
 Add-Content $fileName "<td>Local Disk</td>"
 }
 Add-Content $fileName "<td align=center>$totSpace</td>"
 Add-Content $fileName "<td align=center>$usedSpace</td>"
 Add-Content $fileName "<td align=center>$frSpace</td>"
 Add-Content $fileName "<td bgcolor='red' align=center>$freePercent</td>"
 Add-Content $fileName "</tr>"
}
}
Function writeServiceinfo
{
param($fileName,$Name,$DisplayName,$Status)

 if ($Status -ne "Running")
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$name</td>"
 Add-Content $fileName "<td>$DisplayName</td>"
 Add-Content $fileName "<td bgcolor='#F75D59' align=center>$Status</td>"
 Add-Content $fileName "</tr>"
 }
 Else
 {
 }
}


#writing the information to HTML file using Function's

writehtmlheader $fileName
getinfo
writehtmlfooter $fileName

                                            

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 38855066
Sorry I was not checking my EE mail alerts for last one week due to a personal engagement.. I'm glad you figured it out and were able to fix it.. Let me know if you need any further help..
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Set OWA language and time zone in Exchange for individuals, all users or per database.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now