2

I’m new to powershell and development in general. I’m trying to write a script that will email a contact once a file exceeds a certain size. I have two individual functions both working separately (one to check the file size and one to generate a file for sendmail to use) but I can’t get them to interact.

I want to execute the function CheckSize and if the variable $ExceedsSize gets set to 1 then call function SendMail otherwise the script should finish with no other action.

I’ve searched through the forums but couldn’t find anything to apply to what I’m doing.

   ##Check file to see if it is over a particular size and then send email once threshold is reached.

param( 
    [string]$SiteName = "TestSite",                                                 #Name of Site (No Spaces)
    [string]$Path = "\\NetworkPath\Directory",                                      #Path of directory to check
    [int]$FileSizeThreshold = 10,                                                   #Size in MB that will trigger a notification email
    [string]$Contacts = "[email protected]"
    )    

CLS

##Creates variable $ExceedsSize based on newest file in folder.
Function CheckSize {
IF ((GCI $Path -Filter *.txt | Sort LastWriteTime -Descending | Select-Object -first 1 | Measure-Object -property Length -sum).sum / 1000000 -gt $FileSizeThreshold) {$ExceedsSize = 1}
ELSE {$ExceedsSize = 0}

Write-Host $ExceedsSize
}


Function SendMail {
    Param([string]$Template, [string]$Contacts, [string]$WarnTime)

    $EmailLocation = "\\NetworkPath\Scripts\File_$SiteName.txt"

    #Will Generate email from params
        New-Item $EmailLocation -type file -force -value "From: [email protected]`r
To: $Contacts`r
Subject: $SiteName file has exceeded the maximum file size threshold of $FileSizeThreshold MB`r`n"

    #Send Email
    #CMD /C "$SendMail\sendmail.exe -t < $EmailLocation"

    }

2 Answers 2

2

Add this before or after your Write-Host $ExceedsSize:

return $ExceedsSize

Add this to the bottom:

$var = CheckSize

if ($var -eq 1){
    SendMail
}

Explanation
You have two functions, but don't actually run them. The part at the bottom does that.
Your CheckSize function does not return the $ExceedsSize for the rest of the function; by default it remains within the scope of the function. return x means the variable is passed back to the main script. $var = means it is assigned ot that variable.

Sign up to request clarification or add additional context in comments.

2 Comments

I removed Write-Host $ExceedsSize completely and replaced it with Return $ExceedsSize. After that, adding your IF onto the bottom made perfect sense and works just like I wanted it. It didn't occur to me to assign a variable to the output of the function.
Great, glad to hear it's now working! As per the other answer, another approach is to call SendMail from within CheckSize. Another way - which you should avoid as generally it's bad practice - is to assign a variable global scope: $global:ExceedsSize and then check the value of $ExceedsSize within SendMail. about_scopes is worth reading.
1

Per the other answer, you need to return $ExceedsSize instead of using Write-Host (see here for why Write-Host is considered harmful: http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/).

You could alternatively call the SendMail function from within the CheckSize function, e.g:

 if ($ExceedsSize -eq 1){SendMail}

You will still need to call the CheckSize function somewhere also:

CheckSize

You might also want to give consideration to naming your functions in the verb-noun style of the built in cmdlets. This really helps make their use more explicit to you and others. When choosing a verb, its best to stick to the approved list: https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx

And also to use names that are fairly unique to avoid possible conflicts.

I'd suggest something along the lines of:

Get-NewestFileSize

(although that's what it should then return)

and

Send-CCSMail

1 Comment

I've gone from Admin to Help Desk to L2 Support and have ended up as a developer. I've been on a figure it out path for quite a while now. The Verb-Noun naming convention makes a lot of sense.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.