StartingPS
StartingPS

Reputation: 31

Powershell : passing parameters to a function

I'm trying to create a function which takes two parameters and do some repetitive tasks, but the execution fails : Here is the function :

Function RenameFolderFiles{
    param(
        [String[]]$FldPath, [String]$TypeFld
    )
    
    Write-Host $TypeFld " : " $FldPath.Length
    Write-Host $FldPath
    for ( $i = 0; $i -lt $FldPath.Length; $i++ ) {
        write-host $i " : " $FldPath.FullName[$i]
        $NewFld = ([string]$FldPath.FullName[$i]).Replace(" _ tt","");
    }
}

I call the function like this :

$Fld = Get-ChildItem -Path "$varCheminRepertoireScript" -Recurse -Directory | Where-Object {$_.FullName -like "* _ tt*"}

RenameFolderFiles -FldPath $Fld -TypeFld "Nb of Folders & Sub-Folders"

But the result is :

.
.
.
Cannot index into a null array.
At C:\Test.ps1:10 char:9
+         write-host $i " : " $FldPath.FullName[$i]
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

What is wrong with the parameters / types passed ? Thanks for your clarifications !! BR

Upvotes: 0

Views: 68

Answers (1)

ABC DEF
ABC DEF

Reputation: 329

There is No property like FullName on string[] $FldPath or on $FldPath[0] index Item $FldPath.FullName

set [object[]]$FldPath instead of [String[]]$FldPath in param of Function

Upvotes: 1

Related Questions