3

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.

1
  • 1
    function is not a declaration, it is a command, that have to be executed to create/modify function. Commented Nov 10, 2015 at 15:19

1 Answer 1

4

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(',')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.