I made a PowerShell script to write a report and highlight any disk under 20% of space but the script keeps failing because the size value is not right. Can you please help me?
$Computers = Get-Content -Path C:\Users\gbekari\unbackup\Servers.txt
$results = foreach ($Computer in $Computers){
Get-WmiObject Win32_Volume -Filter "DriveType='3'" -ComputerName $Computer | ForEach {
New-Object PSObject -Property @{
Computername = $computer
date = (Get-Date -format "dd.MM.yy HH:mm")
size = ([Math]::Round($_.Size /1GB,2))
freeSpace = ([Math]::Round($_.FreeSpace /1GB,2))
Status = if ([Math]::Round(100 * $db.FreeSpace / $db.Size) -gt 19 ) {'NONE'} else {'Warning'}
empty = "Diskcheck"
}
}
}
$results | ConvertTo-Csv -NoTypeInformation -Delimiter "|" | % {$_-replace'"',''} | Set-Content -Path C:\Users\gbekari\unbackup\Sers.txt
Win32_Volumeclass doesn't have aSizeproperty; try changing it toCapacity. Also, when computing theStatusproperty you start using a$dbvariable — which isn't defined anywhere inside that loop — instead of$_.