Hi Experts,
Need some powershell help.
Issue Descrition: I work as SQL Server DBA and we monitor servers using powershell scripts. So, we have one such script to monitor disk free space of multiple servers. We are running powershell 4.0 and using below powershell script which takes Servers_test.txt file as input file. i.e. We put the list of servers and read the content inside the powershell script to loop thru each server , get all disk/drive space details and if the freespacepercent< 10% we show the drive entry in RED color indicating it is CRITICAL and we are running of space and if freespacepercent $PSVersionTable
Name Value
PSVersion 5.1.14393.1358
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.14393.1358
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
SQL Server Details :
SQL server 2016 Enterprise Edition SP
Sql agent Job as only 1 step and choosen Powershell subsystem and then invoking powershell script as below
powershell.exe E:\diskspace\DiskspaceV2.ps1
Script source code (E:\diskspace\DiskspaceV2.ps1)
Continue even if there are errors-
$ErrorActionPreference = “Continue”;
Set your warning and critical thresholds
$percentWarning = 15;
$percentCritcal = 10;
EMAIL PROPERTIES i.e Tolist
# Set the recipients Emails
$users = "testguy@cnz.com" #---- change this later
REPORT PROPERTIES
# Path to the report
$reportPath = "E:\DiskSpace\";
# Report name
#$reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html";
$reportName = “DiskSpaceRpt_$(get-date -format ddMMyyyyHHmmss).html”;
Path and Report name together
$diskReport = $reportPath + $reportName
#Set colors for table cell backgrounds
$redColor = “#FF0000”
$orangeColor = “#FBB917”
$whiteColor = “#FFFFFF”
Count if any computers have low disk space. Do not send report if less than 1.
$i = 0;
$computers = Get-Content “E:\DiskSpace\servers_test.txt”; #–testing
$datetime = Get-Date -Format “MM-dd-yyyy_HHmmss”;
Write-host $diskReport
Create and write HTML Header of report
$titleDate = get-date -uformat “%m-%d-%Y - %A”
$header = "
DiskSpace Report
<!--
td {
font-family: Tahoma;
font-size: 11px;
border-top: 1px solid #999999;
border-right: 1px solid #999999;
border-bottom: 1px solid #999999;
border-left: 1px solid #999999;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 10px;
table {
border: thin solid #000000;
}
-->
<strong>Disk Usage Report for $titledate</strong>
"
Add-Content $diskReport $header
Create and write Table header for report
$tableHeader = "
Server
Drive
Drive Label
Total Capacity(GB)
Used Capacity(GB)
Free Space(GB)
Freespace %
"
Add-Content $diskReport $tableHeader
Start processing disk space reports against a list of servers
foreach($computer in $computers)
{
$disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter “DriveType = 3”
$computer = $computer.toupper()
Write-host "Computer : "$computer
$dataRow=“”;
foreach($disk in $disks)
{
$deviceID = $disk.DeviceID;
$volName = $disk.VolumeName;
[float]$size = $disk.Size;
[float]$freespace = $disk.FreeSpace;
$percentFree = [Math]::Round(($freespace / $size) * 100, 2);
$sizeGB = [Math]::Round($size / 1073741824, 2);
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
$usedSpaceGB = $sizeGB - $freeSpaceGB;
$color = $whiteColor;
Write-host "Computer : "$computer
Write-host "Deviceid : "$deviceID
Write-host "Volumne name : "$volName
Write-host "Size : "$size
Write-host "FreeSpace : "$freespace
Write-host "PercentFree : "$percentFree
Write-host "SizeGB : "$sizeGB
Write-host "UsedGb : "$usedSpaceGB
Write-host "FreespaceGB : "$freeSpaceGB
Write-host " "
Set background color to Orange if just a warning
if($percentFree -lt $percentWarning)
{
$color = $orangeColor
Set background color to Orange if space is Critical
if($percentFree -lt $percentCritcal)
{
$color = $redColor
}
Create table data rows
$dataRow = "
$computer
$deviceID
$deviceID
$sizeGB
$usedSpaceGB
$freeSpaceGB
$percentFree
"
Add-Content $diskReport $dataRow;
#Write-Host -ForegroundColor DarkYellow “$computer $deviceID percentage free space = $percentFree”;
$i++
}
}
}
Create table at end of report showing legend of colors for the critical and warning
$tableDescription = "
Warning less than 15% free space
Critical less than 10% free space
"
Add-Content $diskReport $tableDescription
Add-Content $diskReport “”
Send Notification if alert $i is greater then 0
if ($i -gt 0)
{
#Write-host “*** 1 "
foreach ($user in $users)
{
#Write-host " 2 ***”
Write-Host “Sending Email notification to $user”
$smtpServer = "smtphost.xxxxx.xxx.xxxx.com" #---- change this later
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From = "abc@hall.com" #---- change this later
$msg.Subject = "Disk Usage Report for $titledate"
$msg.IsBodyHTML = $true
$msg.Body = get-content $diskReport
Invoke-Expression $diskReport
Write-Host $msg.Body
$smtp.UseDefaultCredentials = $true
$smtp.Send($msg)
#$body = ""
}
}
I am unable to attach screenshots here. not sure how to do it.