在PowerShell ISE中调用函数[关闭]

有人能告诉我为什么我不能在PowerShell脚本中调用函数吗?见下面我的代码:

Write-Host "Before calling Function."

testFunction

function testFunction()
{ 
    Write-Host "Function has been called"
}

当我运行上面的代码时,我收到以下错误消息:

testFunction : The term 'testFunction' is not recognized as the 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.
At C:\Users\andrew.short\Documents\Powershell\Backups\functionTest.ps1:3 char:1
+ testFunction
+ ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (testFunction:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我确信必须能够在同一个PowerShell脚本中调用函数 . 有人可以帮忙吗?

回答(1)

2 years ago

您必须在使用之前声明该功能 .

Write-Host "Before calling Function."

function testFunction {
    Write-Host "Function has been called"
}

testFunction