⚠️ IMPORTANT: These scripts are AI-GENERATED and UNTESTED Exchange Scripts (5): - Get-MailboxPermissions.ps1: Audit delegate access permissions - Get-InactiveMailboxes.ps1: Identify stale mailboxes - Compare-MailboxDatabases.ps1: Database health comparison - Export-DistributionGroups.ps1: Distribution group inventory - Get-MailflowStats.ps1: Transport log analysis Active Directory Scripts (3): - Get-ADUserLastLogon.ps1: True LastLogon across all DCs - Export-OUStructure.ps1: OU hierarchy with GPO links - Compare-ADGroupMemberships.ps1: Compare user group memberships System Maintenance Scripts (4): - Get-ServerInventory.ps1: Hardware/software inventory report - Monitor-DiskSpace.ps1: Disk space monitoring with alerts - Backup-ExchangeCertificates.ps1: Certificate backup to PFX - Test-ExchangeHealth.ps1: Aggregated Exchange health checks Documentation: - Updated CLAUDE.md with AI-generated scripts section - Added AI-GENERATED-SCRIPTS.md with warnings and testing guide All scripts include prominent warnings and follow established patterns from existing scripts. Require thorough testing before production use. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
184 lines
5.2 KiB
PowerShell
184 lines
5.2 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Monitor disk space and alert when below threshold
|
|
|
|
.DESCRIPTION
|
|
Checks all fixed drives for available space and alerts when below threshold.
|
|
Can be scheduled as a task for proactive monitoring.
|
|
|
|
.PARAMETER ThresholdPercent
|
|
Alert when free space falls below this percentage (default: 15)
|
|
|
|
.PARAMETER ThresholdGB
|
|
Alternative: alert when free space falls below this many GB (default: not used)
|
|
|
|
.PARAMETER LogPath
|
|
Path to log file for alerts (default: C:\Temp\DiskSpace-Monitor.log)
|
|
|
|
.PARAMETER SendEmail
|
|
Send email alerts (requires email parameters, default: $false)
|
|
|
|
.PARAMETER SmtpServer
|
|
SMTP server for email alerts
|
|
|
|
.PARAMETER EmailFrom
|
|
From address for email alerts
|
|
|
|
.PARAMETER EmailTo
|
|
To address(es) for email alerts (comma-separated)
|
|
|
|
.NOTES
|
|
⚠️ AI-GENERATED SCRIPT - UNTESTED
|
|
This script was generated by Claude AI and has not been tested in production.
|
|
Review and test thoroughly in a non-production environment before use.
|
|
|
|
- Suitable for scheduled task execution
|
|
- Exit codes: 0=OK, 1=Warning (below threshold), 2=Error
|
|
- Email functionality requires SMTP access
|
|
|
|
.EXAMPLE
|
|
.\Monitor-DiskSpace.ps1
|
|
|
|
.EXAMPLE
|
|
.\Monitor-DiskSpace.ps1 -ThresholdPercent 20 -LogPath "D:\Logs\DiskMonitor.log"
|
|
|
|
.EXAMPLE
|
|
.\Monitor-DiskSpace.ps1 -SendEmail $true -SmtpServer "smtp.domain.com" -EmailFrom "alerts@domain.com" -EmailTo "admin@domain.com"
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
param(
|
|
[int]$ThresholdPercent = 15,
|
|
[int]$ThresholdGB = 0,
|
|
[string]$LogPath = "C:\Temp\DiskSpace-Monitor.log",
|
|
[bool]$SendEmail = $false,
|
|
[string]$SmtpServer = "",
|
|
[string]$EmailFrom = "",
|
|
[string]$EmailTo = ""
|
|
)
|
|
|
|
function Write-Log {
|
|
param([string]$Message, [string]$Level = "INFO")
|
|
$stamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
|
|
$line = "[$stamp] [$Level] $Message"
|
|
Write-Output $line
|
|
|
|
# Ensure log directory exists
|
|
$logDir = Split-Path -Path $LogPath -Parent
|
|
if ($logDir -and -not (Test-Path -Path $logDir)) {
|
|
try {
|
|
New-Item -ItemType Directory -Path $logDir -Force | Out-Null
|
|
} catch {}
|
|
}
|
|
|
|
try {
|
|
Add-Content -Path $LogPath -Value $line -Encoding UTF8 -ErrorAction SilentlyContinue
|
|
} catch {}
|
|
}
|
|
|
|
Write-Log "⚠️ AI-GENERATED SCRIPT - UNTESTED" "WARNING"
|
|
Write-Log "Starting disk space monitoring on $env:COMPUTERNAME"
|
|
|
|
# Get all fixed drives
|
|
$disks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" -ErrorAction SilentlyContinue
|
|
|
|
if (-not $disks) {
|
|
Write-Log "ERROR: Could not retrieve disk information" "ERROR"
|
|
exit 2
|
|
}
|
|
|
|
$alerts = @()
|
|
$allDisksOK = $true
|
|
|
|
foreach ($disk in $disks) {
|
|
$drive = $disk.DeviceID
|
|
$sizeGB = [math]::Round($disk.Size / 1GB, 2)
|
|
$freeGB = [math]::Round($disk.FreeSpace / 1GB, 2)
|
|
$usedGB = [math]::Round(($disk.Size - $disk.FreeSpace) / 1GB, 2)
|
|
$percentFree = [math]::Round(($disk.FreeSpace / $disk.Size) * 100, 2)
|
|
|
|
Write-Log "Drive $drive - Size: $sizeGB GB, Free: $freeGB GB ($percentFree%)"
|
|
|
|
# Check thresholds
|
|
$alertTriggered = $false
|
|
$alertReason = ""
|
|
|
|
if ($ThresholdGB -gt 0 -and $freeGB -lt $ThresholdGB) {
|
|
$alertTriggered = $true
|
|
$alertReason = "Free space ($freeGB GB) below threshold ($ThresholdGB GB)"
|
|
} elseif ($percentFree -lt $ThresholdPercent) {
|
|
$alertTriggered = $true
|
|
$alertReason = "Free space ($percentFree%) below threshold ($ThresholdPercent%)"
|
|
}
|
|
|
|
if ($alertTriggered) {
|
|
$allDisksOK = $false
|
|
$alertMsg = "ALERT: Drive $drive - $alertReason"
|
|
Write-Log $alertMsg "ALERT"
|
|
|
|
$alerts += [PSCustomObject]@{
|
|
Drive = $drive
|
|
SizeGB = $sizeGB
|
|
FreeGB = $freeGB
|
|
UsedGB = $usedGB
|
|
PercentFree = $percentFree
|
|
Reason = $alertReason
|
|
}
|
|
}
|
|
}
|
|
|
|
# Send email if configured and alerts exist
|
|
if ($SendEmail -and $alerts.Count -gt 0) {
|
|
if (-not $SmtpServer -or -not $EmailFrom -or -not $EmailTo) {
|
|
Write-Log "Email alerts enabled but SMTP parameters incomplete" "WARNING"
|
|
} else {
|
|
Write-Log "Sending email alert to $EmailTo via $SmtpServer"
|
|
|
|
$subject = "DISK SPACE ALERT: $env:COMPUTERNAME"
|
|
$body = @"
|
|
Disk Space Alert on $env:COMPUTERNAME
|
|
Generated: $(Get-Date)
|
|
|
|
The following drives have triggered disk space alerts:
|
|
|
|
"@
|
|
|
|
foreach ($alert in $alerts) {
|
|
$body += @"
|
|
|
|
Drive: $($alert.Drive)
|
|
Size: $($alert.SizeGB) GB
|
|
Free: $($alert.FreeGB) GB ($($alert.PercentFree)%)
|
|
Used: $($alert.UsedGB) GB
|
|
Alert: $($alert.Reason)
|
|
"@
|
|
}
|
|
|
|
$body += @"
|
|
|
|
|
|
Threshold Settings:
|
|
Percent Threshold: $ThresholdPercent%
|
|
GB Threshold: $ThresholdGB GB
|
|
|
|
Please investigate and free up disk space as needed.
|
|
"@
|
|
|
|
try {
|
|
Send-MailMessage -To $EmailTo.Split(',') -From $EmailFrom -Subject $subject -Body $body -SmtpServer $SmtpServer -ErrorAction Stop
|
|
Write-Log "Email sent successfully"
|
|
} catch {
|
|
Write-Log "ERROR sending email: $($_.Exception.Message)" "ERROR"
|
|
}
|
|
}
|
|
}
|
|
|
|
# Exit with appropriate code
|
|
if ($allDisksOK) {
|
|
Write-Log "All disks have sufficient free space"
|
|
exit 0
|
|
} else {
|
|
Write-Log "$($alerts.Count) disk(s) below threshold"
|
|
exit 1
|
|
}
|