【发布时间】:2017-05-20 02:32:32
【问题描述】:
有人能告诉我为什么我不能在 PowerShell 脚本中调用函数吗?请参阅下面的代码:
Write-Host "Before calling Function."
testFunction
function testFunction()
{
Write-Host "Function has been called"
}
当我运行上述代码时,我收到以下错误消息:
testFunction : 术语“testFunction”未被识别为 cmdlet 的名称,
函数、脚本文件或可运行的程序。检查名称的拼写,或者如果
包含路径,验证路径是否正确,然后重试。
在 C:\Users\andrew.short\Documents\Powershell\Backups\functionTest.ps1:3 char:1
+ 测试功能
+~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (testFunction:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我确信必须可以在同一个 PowerShell 脚本中调用函数。有人可以帮忙吗?
【问题讨论】:
-
哦,实际上我想我可能刚刚发现了问题所在。我认为该函数必须在调用它的代码之前存在。
-
是的....这应该有效:
Write-Host "Before calling Function." function testFunction { Write-Host "Function has been called" } testFunction -
@Ed209 没错。
标签: function powershell call