2016-04-24 151 views
1

我正在學習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可以不從另一個函數調用一個函數嗎? 我懷疑這是一個範圍問題?有沒有辦法我可以解決這個問題,並仍然有功能的代碼?

回答

3

沒有問題 - 您正在調用您定義的還沒有的功能。

在模塊您的

plFileAppend 

名稱來定義的功能,但您的

plFileAppand 

即名稱調用一個函數。最後e已被替換爲a

+1

狗走了。我知道這很簡單,但是謝謝。今天還沒有足夠的咖啡 –

+1

即使對我們來說也是如此;-) –