Solved

powershell scripting

Posted on 2013-01-28
11
421 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
Comment Utility
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
Comment Utility
sorry,third party tools are not allowed.
0
 
LVL 7

Expert Comment

by:Beneford
Comment Utility
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
Comment Utility
I need a sample script which will help me to build more logic into the script.
0
 
LVL 7

Expert Comment

by:Beneford
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
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
Comment Utility
can you please help me to store the data as html file.
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
Comment Utility
excellent Work!!!Subsun Awesome one.

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

Author Comment

by:VIVEKANANDHAN_PERIASAMY
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

11 Experts available now in Live!

Get 1:1 Help Now