我正在学习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和Force导入模块,输出似乎正在以正确的顺序加载?
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'.
Google发现的所有内容似乎都解决了被调用函数的定义必须在调用函数之前的情况。这似乎不是这种情况。我错过了什么? Powershell可以不从另一个函数调用一个函数吗? 我怀疑这是一个范围问题?有没有办法可以解决这个问题并且仍然有函数中的代码?
答案 0 :(得分:3)
没有问题 - 您正在调用未定义的功能。
在模块中,您可以通过名称
定义功能plFileAppend
但您可以通过名称
调用函数plFileAppand
即。最后的 e 已被 a
取代