foreach ($server in '192.168.1.5', '192.168.1.6', '192.168.1.7') {
$availablespace = [Math]::Truncate((Get-WmiObject -Class Win32_LogicalDisk -ComputerName $server -Credential $arpcred | Where-Object { $_.DeviceID -eq 'C:' }).FreeSpace / 1GB)
Write-Host $server has $availablespace GB of available space
if ($availablespace -lt 10) {
Write-Host $server only has $availablespace GB of available space
Write-Host sending email
Send-MailMessage -From $from -To $to -Subject $subj -Body "$server only has $availablespace GB of available space" -SmtpServer $smtp -Credential $smtpcred -UseSsl -Port 587
}
}
This will loop through a list of servers and send an email for each server that has less than 10GB of free space available on C:.
How can I change it so it will send a single email containing a list of all the servers with less than 10GB of free disk space?
CodePudding user response:
You can use dynamic array for the store value. Please find below code
$serverlist= $null
$serverlist= [System.Collections.ArrayList]@()
foreach ($server in '192.168.1.5', '192.168.1.6', '192.168.1.7') {
$availablespace = [Math]::Truncate((Get-WmiObject -Class Win32_LogicalDisk -ComputerName $server -Credential $arpcred | Where-Object { $_.DeviceID -eq 'C:' }).FreeSpace / 1GB)
Write-Host $server has $availablespace GB of available space
if ($availablespace -lt 10) {
$serverlist = $server
}
}
write-host $serverlist
You get all server list inside the $serverlist