Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell scripting

Posted on 2013-01-28
11
Medium Priority
?
507 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
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
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
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 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

WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Loops Section Overview
Screencast - Getting to Know the Pipeline

972 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