Kikopu
Kikopu

Reputation: 155

PowerShell - not recognizing the function

I'm new with PowerShell and i've got a problem I cant deal with. I have to make my function analyze a lot of folders in a row, but my programm wont work when I give parameters to my function...

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
}

It worked when 'analyse' didnt exist but stopped working just after that. CommandNoFoundException. It may be a stupid mistake but i cant deal with it... Thank you for your time.

Upvotes: 3

Views: 109

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174465

PowerShell scripts like yours will be read, line-by-line by the parser.

At the point in time where analyze $fpath is being parsed, the function analyze doesn't exist in the current scope, since the function definition is further down the script.

To use an inline function inside the script, move the definition up to a point before you call it:

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(',')

Upvotes: 4

Related Questions