简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM