Powershell脚本无法识别我的功能 [英] Powershell script not recognizing my function
问题描述
我有一个Powershell脚本,可以解析文件并在检测到特定模式时发送电子邮件.我在函数中设置了电子邮件代码,当我从ISE运行它时,一切正常,但是我使用 PS2EXE 能够将脚本作为服务运行,但无法识别该脚本.功能电子邮件".我的代码看起来与此类似
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
}
当我将其转换为exe并运行它时,出现此错误:
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按顺序进行处理(自上而下),因此函数定义必须在函数调用之前:
Powershell processes in order (top-down) so the function definition needs to be before the function call:
function email($text){
#email $text
}
#Do things |
foreach{
email($_)
}
它可能在ISE中正常工作,因为您的功能定义仍在内存中,而这仍来自先前的运行或测试.
It probably works fine in the ISE because you have the function definition in memory still from a prior run or test.
这篇关于Powershell脚本无法识别我的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!