none
Powershell script to monitor file size and alerts

    Question

  • I've been working on a script to alert me when certain files within a folder exceed a limit, getting stuck on the syntax of the script and can't seem to get it working properly, scripting is not my best and any help to point out the errors would be great.

    It sends the email regardless of the file size.



    $filesize = (Get-Item \\server\file).length -gt 1.5GB
    if($filesize = "True")
    {
        Send-MailMessage -from nobody@acme.com -To somebody@acme.com -subject "File size has exceeded 1.5GB" -Body "$filesize has exceeded 1.5GB" -SmtpServer smtp.acme.com
    }else{
    }


    • Edited by Scovy Wednesday, August 9, 2017 6:48 PM
    Wednesday, August 9, 2017 6:45 PM

Answers

  • $true I not a string it is a Boolean and does not need to be tested for:

    if((Get-Item \\server\file).length -gt 1.5GB) {
    	Send-MailMessage -from nobody@acme.com -To somebody@acme.com -subject "File size has exceeded 1.5GB" -Body "$filesize has exceeded 1.5GB" -SmtpServer smtp.acme.com
    }
    Note that 1.5Gb is not 1,500,000,000 bytes it s 1,610,612,736 because 1Gb is a binary number and not a decimal number.  It is ( 1024 * 1024 * 1024) or 1Kb * 1K b * 1Kb.  A Kb is 1024 decimal bytes or 0x400 hex bytes or binary 100 0000 0000


    \_(ツ)_/


    • Edited by jrvModerator Wednesday, August 9, 2017 6:59 PM
    • Marked as answer by Scovy Wednesday, August 9, 2017 8:10 PM
    Wednesday, August 9, 2017 6:52 PM
    Moderator

All replies

  • $true I not a string it is a Boolean and does not need to be tested for:

    if((Get-Item \\server\file).length -gt 1.5GB) {
    	Send-MailMessage -from nobody@acme.com -To somebody@acme.com -subject "File size has exceeded 1.5GB" -Body "$filesize has exceeded 1.5GB" -SmtpServer smtp.acme.com
    }
    Note that 1.5Gb is not 1,500,000,000 bytes it s 1,610,612,736 because 1Gb is a binary number and not a decimal number.  It is ( 1024 * 1024 * 1024) or 1Kb * 1K b * 1Kb.  A Kb is 1024 decimal bytes or 0x400 hex bytes or binary 100 0000 0000


    \_(ツ)_/


    • Edited by jrvModerator Wednesday, August 9, 2017 6:59 PM
    • Marked as answer by Scovy Wednesday, August 9, 2017 8:10 PM
    Wednesday, August 9, 2017 6:52 PM
    Moderator
  • That worked, also noticed I had a typo that I corrected
    Get-Item \\server\file\*.*
    Wednesday, August 9, 2017 8:10 PM
  • That worked, also noticed I had a typo that I corrected
    Get-Item \\server\file\*.*

    You cannot correctly use Get-Item with a collection of files.  It will not give you a total.

    (Get-ChildItem \server\file\* -File | Measure-Object -Sum Length).Sum


    \_(ツ)_/

    Wednesday, August 9, 2017 8:17 PM
    Moderator