How to monitor filesize with Powershell – Windows


I added a basic powershell script to  monitor  filesize and send e-mail.

#Abdurrahim YILDIRIM#
#Define file that you need to monitor####
$targetFile = "C:\HPOM\ProgramData\HP\HP BTO Software\shared\tmp\queues\MsgQueue"
$path_file = "C:\HPOOconfig\SystemControl\quee_size_control"
$fileSize = ((Get-Item $targetFile).length/1KB)
$friendlySize = ([math]::Round($fileSize, 2))
$sto = "yourmail@casesup.com"
echo $fileSize
if ($fileSize -gt 100)
{
if (Test-Path $path_file)
{
echo $fileSize >> $path_file
}
else
{
echo $fileSize >> $path_file
Send-MailMessage -To $sto -From localmachine@test.com -SMTPServer smtpx.casesup.com -Subject "FileMonitor - Alert " -Body "$targetFile file size $friendlySize KB."
}
}
else
{
if (Test-Path $path_file)
{
Send-MailMessage -To $sto -From localmachine@test.com -SMTPServer smtpx.casesup.com -Subject "FileMonitor - Alert " -Body "$targetFile file size $friendlySize KB."
del $path_file
}
}

 

Tagged In:

I'm a IT Infrastructure and Operations Architect with extensive experience and administration skills and works for Turk Telekom. I provide hardware and software support for the IT Infrastructure and Operations tasks.

205 Total Posts
Follow Me