[英]Powershell : passing parameters to a function
我正在尝试创建一个 function,它采用两个参数并执行一些重复性任务,但执行失败:这是 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","");
}
}
我这样称呼 function:
$Fld = Get-ChildItem -Path "$varCheminRepertoireScript" -Recurse -Directory | Where-Object {$_.FullName -like "* _ tt*"}
RenameFolderFiles -FldPath $Fld -TypeFld "Nb of Folders & Sub-Folders"
但结果是:
.
.
.
Cannot index into a null array.
At C:\Test.ps1:10 char:9
+ write-host $i " : " $FldPath.FullName[$i]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
传递的参数/类型有什么问题? 感谢您的澄清! BR
在 string[] $FldPath 或 $FldPath[0] 索引项 $FldPath.FullName 上没有像 FullName 这样的属性
在 Function 的参数中设置 [object[]]$FldPath 而不是 [String[]]$FldPath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.