gpt4 book ai didi

function - 在 PowerShell ISE 中调用函数

转载 作者:行者123 更新时间:2023-12-02 10:03:48 26 4
gpt4 key购买 nike

有人可以告诉我为什么我无法在 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 ifa 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 脚本中调用函数。有人可以帮忙吗?

最佳答案

您必须在使用该函数之前声明该函数。

Write-Host "Before calling Function."

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

testFunction

关于function - 在 PowerShell ISE 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41486891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com