I have a powershell script that parses a file and send an email if it detects a certain pattern. I have the email code setup inside a function, and it all works fine when I run it from the ISE, but I used PS2EXE to be able to run the script as a service but it does not recognize the function "email". my code looks similar to this
#Do things |
foreach{
email($_)
}
function email($text){
#email $text
}
When I convert it to exe and run it I get this error:
The term 'email' is not recognized as teh name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
Powershell processes in order (top-down) so the function definition needs to be before the function call:
It probably works fine in the ISE because you have the function definition in memory still from a prior run or test.
When it comes to the Function-Calls, PowerShell is fairly different from other programming-languages in the following ways:
Be careful in where you define the function. As PowerShell sequentially processes line-by-line in top-down order, hence the function must be defied before that function is called:
In ISE, function defined below the function-call may appear as working but (beware) it's the cached function definition in memory from a previous run, so if you have updated the function you are screwed.