Search code examples
powershellif-statementthreshold

powershell if -gt script not working correctly results always in above limit


please help me, my powershell script somehow doesnt see the correct value and always reports to the email adress even if the space is less then the threshold.

$PSEmailServer = 'spamtitan.domain.nl'
$username = [Environment]::UserName 
$folderSizeOutput = "{0:N0}" -f ((Get-ChildItem C:\users\$username -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)
$threshold = "4500"
$folderSizeOutput
if ($folderSizeOutput -gt "$threshold"){
Send-MailMessage -From "[email protected]" -To "[email protected]" -Subject "ser Profile Disk $username above threshold " -Body "User Profile folder size: $folderSizeOutput / 5000 MB" 
}
else {
Write-Host "under limit"
}

Solution

  • $PSEmailServer = 'spamtitan.domain.nl'
    $username = [Environment]::UserName 
    $folderSizeOutput = [math]::round((Get-ChildItem C:\users\$username -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)
    $threshold = 4500
    $maxhold = 5000
    Write-Host "Actual Size = $folderSizeOutput MB"
    $stringSizes = "$folderSizeOutput MB / $maxhold MB"
    if ($folderSizeOutput -gt $threshold){
    Send-MailMessage -From "[email protected]" -To "[email protected]" -Subject "User Profile Disk $username $StringSizes " -Body "User Profile folder above $threshold MB : $StringSizes" 
    }
    else {
        Write-Host "Under limit : $stringSizes"
    }