PowerShell - 无法识别功能

时间:2015-11-10 15:01:13

标签: function powershell

我是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
}

当“分析”不存在但在此之后停止工作时,它起作用了。 CommandNoFoundException。这可能是一个愚蠢的错误,但我不能处理它...谢谢你的时间。

1 个答案:

答案 0 :(得分: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(',')
相关问题