Author Posts

July 22, 2016 at 9:30 am

Hi,

I want to trigger an email alert when the diskspace for the different servers will be less than 20 percent.
The below given code will generate Excel sheet as an output and provides the diskspace data in it.

So please suggest me on how to get it done.

Output in Excel sheet
———————————————————————
Machine Name Drive Total size (GB) Free Space (GB) Free Space (%)
MyMachine C: 50 11 23%
D: 100 92 92%
———————————————————————-

===================================================================
#Parameter Declareation
param(
[string]$FilePath
)

$erroractionpreference = “SilentlyContinue”
$objExcel = New-Object -comobject Excel.Application
$objExcel.visible = $True
$objWorkbook = $objExcel.Workbooks.Add()
$objWorkSheet = $objWorkbook.Worksheets.Item(1)
$objWorkSheet.Cells.Item(1,1) = “Machine Name”
$objWorkSheet.Cells.Item(1,2) = “Drive”
#$objWorkSheet.cells.item(1,2) = "Volume Name "
$objWorkSheet.Cells.Item(1,3) = “Total size (GB)”
$objWorkSheet.Cells.Item(1,4) = “Free Space (GB)”
$objWorkSheet.Cells.Item(1,5) = “Free Space (%)”
$objWorkSheet.Cells.Item(1,6)

$Header = $objWorkSheet.UsedRange
$Header.Interior.ColorIndex = 09
$Header.Font.ColorIndex = 19
$Header.Font.Bold = $True
$Header.EntireColumn.AutoFit()
$intRow = 2

$objWorkSheetomputers = Get-Content $FilePath
$objWorkSheetomputers = Get-Content E:\Servers.txt

Clear-Host
Write-Host "Input FilePath: " $FilePath
Write-Host "Processing........."

$Date = Get-Date -UFormat "%B_%d_%Y"
$curRow = $intRow
$endRow = $intRow

foreach ($strComputer in $objWorkSheetomputers)
{
try
{
$objWorkSheet.Cells.Item($intRow, 1) = $strComputer.ToUpper()
Write-Host "Server:" $strComputer ": Processing..."

$objWorkSheetolDisks = Get-WmiObject Win32_LogicalDisk -computername $strComputer -Filter “DriveType = 3"

if ($objWorkSheetolDisks -ne $null)
{
$curRow = $intRow
foreach ($objDrive in $objWorkSheetolDisks)
{
$objWorkSheet.Cells.Item($intRow, 2) = $objDrive.DeviceID
#$objWorkSheet.cells.item($introw, 3) = $objDrive.volumename
$objWorkSheet.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDrive.Size/1GB)
$objWorkSheet.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDrive.FreeSpace/1GB)
$objWorkSheet.Cells.Item($intRow, 5) = “{0:P0}” -f ([double]$objDrive.FreeSpace/[double]$objDrive.Size)
$intRow = $intRow + 1
}
$endRow = $intRow – 1
$RowMergeCells = $objWorkSheet.Range("A${curRow}:A$endRow")
$RowMergeCells.Select()
$RowMergeCells.MergeCells = $true
$RowMergeCells.VerticalAlignment = -4108
}
else
{

throw $Error[0].exception
}

}
catch
{

$err = $_.Exception
Write-Host "Error in " $servername " :: " $err

$objWorkSheet.cells.item($intRow, 2) = $err
$MergeCells = $objWorkSheet.Range("B${intRow}:E$intRow")
$MergeCells.Select()|Out-Null
$MergeCells.MergeCells = $true
$MergeCells.Interior.ColorIndex = 3

$intRow = $intRow + 1

$temDir = "C:\temp"
if ((Test-Path $temDir) -eq 0)
{
mkdir $temDir | Out-Null
}

$a = Get-Date
$strComputer + "::" + $a.ToLongTimeString() + ":: $_" + "`r`n" | Out-File c:\temp\ServerDriversDetails_Logs-$Date.txt -Append
}

$intRow = $intRow + 1
Write-Host "Server:" $strComputer ": Processing Completed."
}

#Auto fit everything so it looks better
$usedRange = $objWorkSheet.UsedRange
$usedRange.EntireColumn.AutoFit() | Out-Null

Write-Host "Process Completed"
Read-Host "Press any key to exit"

=======================================================================

July 22, 2016 at 10:17 am

so you just want a way to email the content this generates automatically?

if so:

##Variables
$smtpServer = "smtp.contoso.com"
$From = "DiskSpaceMon@contoso.com"
$Email = "Recipient@contoso.com"
$SubjectCPY = "Disk Below 20%"
$Body = "$mailcontent"

#Email Content - (will only send if $mailcontent has a value, you could also use test path to validate or date modified that part is up to you, for this you will need to assign $mailcontent value to the action above & then output that to a file after.)

if ($mailcontent) {Send-Mailmessage -smtpServer $smtpServer -from $from -to $Email -subject $subjectCPY -priority High -Body $Body -Attachments "c:\temp\ServerDriversDetails_Logs-$Date.txt"}

  • This reply was modified 2 years, 1 month ago by  Mark Prior.

July 22, 2016 at 10:38 am

1.It should check the diskspace which is less than 20%
2.The generated excel sheet should be attached in the email thread, showing which servers are affected with less disk space.

July 22, 2016 at 12:44 pm

This is a very common question and there are thousands of iterations of a solution to gather and report on diskspace. Using Excel is an antiquated way of doing reporting and especially for something like diskspace on a single server, there's nothing to analyze or sort, so exporting to Excel is expensive just to get a simple report. Typically, you are going to see this kind of reporting as HTML which can be used directly as the body in the email, take a look at this:

https://gallery.technet.microsoft.com/scriptcenter/Disk-Space-Report-Reports-98e64d65

If you don't like this solution per se, you should just do some searching and you will find a lot of examples.