# Set the threshold percentage
$threshold = 10
# Office 365 SMTP server settings
$smtpServer = "smtp.office365.com"
$smtpPort = 587
$smtpUsername = "example@cloudrain.in"
$smtpPassword = ConvertTo-SecureString "password" -AsPlainText -Force
$toEmail = "admin@cloudrain.in"
# Create SMTP credentials
$credentials = New-Object System.Management.Automation.PSCredential ($smtpUsername, $smtpPassword)
# Continuous loop
while ($true) {
# Get disk information
$diskInfo = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType = 3"
# Iterate through each disk
foreach ($disk in $diskInfo) {
$freeSpacePercentage = [math]::Round(($disk.FreeSpace / $disk.Size) * 100, 2)
if ($freeSpacePercentage -lt $threshold) {
$systemName = $env:COMPUTERNAME
$driveLetter = $disk.DeviceID
$totalSpaceGB = [math]::Round($disk.Size / 1GB, 2)
$usedSpaceGB = [math]::Round(($disk.Size - $disk.FreeSpace) / 1GB, 2)
$remainingSpaceGB = [math]::Round($disk.FreeSpace / 1GB, 2)
$subject = "Disk Space Alert on $systemName - Drive $driveLetter"
$body = "The free space on $systemName - Drive $driveLetter is $($freeSpacePercentage)% which is below the threshold of $threshold%.`n"
$body += "Total Space: $totalSpaceGB GB`n"
$body += "Used Space: $usedSpaceGB GB`n"
$body += "Remaining Space: $remainingSpaceGB GB"
# Send email
Send-MailMessage -From $smtpUsername -To $toEmail -Subject $subject -Body $body -SmtpServer $smtpServer -Port $smtpPort -UseSsl -Credential $credentials
}
}
# Sleep for 3 hours (180 minutes)
Start-Sleep -Seconds (3 * 60 * 60)
}