2015-11-10 189 views
3

我是PowerShell的新手,我遇到了一個我無法處理的問題。 我必須讓我的功能分析很多文件夾在一排,但是當我給的參數在我的功能我PROGRAMM不會工作...PowerShell - 無法識別功能

Param(
    [string]$fPath 
) 

analyse $fPath 
$importList = get-content -Path C:\Users\lamaison-e\Documents\multimediaList.txt 
$multimediaList = $importList.Split(',') 




function analyse{ 

    Param(
    [parameter(Mandatory=$true)] 
    [String] 
    $newPath 
    ) 
    cd $newPath 
    $Resultat = "Dans le dossier " + $(get-location) + ", il y a " + $(mmInCurrentDir) + " fichier(s) multimedia pour un total de " + $(multimediaSize) + " et il y a " + $(bInCurrentDir) + " documents de bureautique pour un total de " + $(bureautiqueSize) + ". La derniere modification du dossier concerne le fichier " + $(modifDate) 
    $Resultat 
} 

它工作時,「分析」 didnt存在,但停止工作只是之後。 CommandNoFoundException。這可能是一個愚蠢的錯誤,但我不能處理它...謝謝你的時間。

+1

'function'是不是一個聲明,它是一個命令,而必須執行創建/修改功能。 – PetSerAl

回答

4

類似於你的PowerShell腳本將被解析器逐行讀取。

在正在解析analyze $fpath的時間點,函數analyze在當前作用域中不存在,因爲函數定義在腳本後面。

使用內聯函數的腳本中,將定義到一個點你怎麼稱呼它之前:

Param(
    [string]$fPath 
) 

# Define the function 
function analyse{ 

    Param(
    [parameter(Mandatory=$true)] 
    [String] 
    $newPath 
    ) 
    cd $newPath 
    $Resultat = "Dans le dossier " + $(get-location) + ", il y a " + $(mmInCurrentDir) + " fichier(s) multimedia pour un total de " + $(multimediaSize) + " et il y a " + $(bInCurrentDir) + " documents de bureautique pour un total de " + $(bureautiqueSize) + ". La derniere modification du dossier concerne le fichier " + $(modifDate) 
    $Resultat 
} 

# Now you can use it 
analyse $fPath 
$importList = get-content -Path C:\Users\lamaison-e\Documents\multimediaList.txt 
$multimediaList = $importList.Split(',')