Powershell从另一个函数调用一个函数
问题描述:
我正在学习Powershell的过程中,并遇到一种情况,我似乎无法从另一个函数调用一个函数?Powershell从另一个函数调用一个函数
我的测试脚本有以下代码:
Import-Module PSStdLib -Verbose -Force
plSetLevel 12
导入的模块具有以下代码:
function plFileAppend {
<#
.SYNOPSIS
.DESCRIPTION
.PARAMETER FileName
FileName
.PARAMETER Output
Output
.PARAMETER Variables
Variables
.EXAMPLE
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true, ParameterSetName="Default", Position=1)]
[Parameter(Mandatory=$true, ParameterSetName="Variable", Position=1)]
[ValidateNotNullOrEmpty()]
[String]$FileName,
[Parameter(Mandatory=$true, ParameterSetName="Default", Position=2)]
[Parameter(Mandatory=$true, ParameterSetName="Variable", Position=2)]
[String]$Output,
[Parameter(Mandatory=$true, ParameterSetName="Variable", Position=3)]
[String[]]$Variables
)
# Scan the output for variable markers.
$lVarsInOutput = ($Output.ToCharArray() | Where-Object {$_ -eq '{'} | Measure-Object).Count
# No variable substitutions.
if ($lVarsInOutput -eq 0) {
$Output | Out-File $FileName -Append
} else {
# Variables passsed to substitute into output.
$lVaiablesOut = $Variables[ 0..($lVarsInOutput-1) ]
$Output -f $lVaiablesOut | Out-File $FileName -Append
}
}
和上述函数的定义之后....
function plSetLevel {
<#
.SYNOPSIS
.DESCRIPTION
.PARAMETER Output
Step
.EXAMPLE
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true, Position=1)]
[ValidateNotNullOrEmpty()]
[ValidateRange(1,9999)]
[int]$Step
)
if (plIfExists($psRestartFile)) { Remove-Item $psRestartFile }
plFileAppend $psRestartFile "STAMP=$psExecStamp"
plFileAppand $psRestartFile "PID=$psPID"
plFileAppand $psRestartFile "STEP=$Step"
}
当我尝试运行plSetLevel函数时,得到以下结果:
plFileAppand : The term 'plFileAppand' 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 S:\PS\Home\Library\PSStdLib\PSStdLib.psm1:1093 char:5
+ plFileAppand $psRestartFile "PID=$psPID"
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (plFileAppand:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
plFileAppand : The term 'plFileAppand' 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 S:\PS\Home\Library\PSStdLib\PSStdLib.psm1:1094 char:5
+ plFileAppand $psRestartFile "STEP=$Step"
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (plFileAppand:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我已经导入模块与详细和强制和输出似乎东西正在加载正确的顺序?
VERBOSE: Loading module from path 'S:\PS\Home\Library\PSStdLib\PSStdLib.psm1'.
VERBOSE: Importing function 'plDebugPrint'.
VERBOSE: Importing function 'plFileAppend'.
VERBOSE: Importing function 'plGetKeyValue'.
VERBOSE: Importing function 'plGetKeyValues'.
VERBOSE: Importing function 'plIfExists'.
VERBOSE: Importing function 'plOSInfo'.
VERBOSE: Importing function 'plSetLevel'.
我发现的一切似乎都是为了解决被调用函数的定义必须位于调用函数之前的情况。这似乎并不是这种情况。我错过了什么? PowerShell可以不从另一个函数调用一个函数吗? 我怀疑这是一个范围问题?有没有办法我可以解决这个问题,并仍然有功能的代码?
答
没有问题 - 您正在调用您定义的还没有的功能。
在模块您的
plFileAppend
名称来定义的功能,但您的
plFileAppand
即名称调用一个函数。最后e已被替换为a
狗走了。我知道这很简单,但是谢谢。今天还没有足够的咖啡 –
即使对我们来说也是如此;-) –