0

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 "spamtitan@domain.nl" -To "reporting@domain.nl" -Subject "ser Profile Disk $username above threshold " -Body "User Profile folder size: $folderSizeOutput / 5000 MB" 
}
else {
Write-Host "under limit"
}
IIIdefconIII
  • 353
  • 2
  • 5
  • 13

2 Answers2

3

You store a String in $folderSizeOutput

$folderSizeOutput returns for example 10 MB instead of 10.

Replace by :

$username = [Environment]::UserName 
$folderSizeOutput = [math]::round((Get-ChildItem C:\users\$username -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)
$threshold = 4500
Write-Host "Actual Size = $folderSizeOutput MB"
$stringSizes = "$folderSizeOutput MB / $threshold MB"
if ($folderSizeOutput -gt $threshold){
    Write-Host "Above limit : $stringSizes"
}
else {
    Write-Host "Under limit : $stringSizes"
}
Manu
  • 1,685
  • 11
  • 27
  • that works indeed but how can i make this easieer to read then? final code should mail something like 100 MB / 5000 MB i have edit the OP to the full script – IIIdefconIII Aug 04 '17 at 09:45
  • I just edited the code to display folder size and total size in the console this way : `100 MB / 5000 MB` – Manu Aug 04 '17 at 09:50
  • TY so far, but im getting consule output ctual Size = 4174.06918144226 MB Under limit : 4174.06918144226 MB / 4500 MB – IIIdefconIII Aug 04 '17 at 09:52
  • if you want to round values use `$folderSizeOutput = [math]::round((Get-ChildItem C:\users\$username -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)` – Manu Aug 04 '17 at 09:53
  • I edited the code above with the round value. You can validate the answer as OK – Manu Aug 04 '17 at 09:54
0
$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 "alerts@domain.nl" -To "reporting@domain.nl" -Subject "User Profile Disk $username $StringSizes " -Body "User Profile folder above $threshold MB : $StringSizes" 
}
else {
    Write-Host "Under limit : $stringSizes"
}
IIIdefconIII
  • 353
  • 2
  • 5
  • 13