在PowerShell ISE中调用函数

时间:2017-01-05 13:56:19

标签: function powershell call

有人能告诉我为什么我不能在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 个答案:

答案 0 :(得分:18)

你必须在之前声明函数

Write-Host "Before calling Function."

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

testFunction