2015-11-10 63 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(',')