我试图将参数传递给函数来执行get-childitem。似乎无法做到。此外,我无法删除我的帖子,所以我只是将其清空,并对我想要弄清楚的内容进行了基本解释。似乎我无法传递带有通配符的文件名变量。到目前为止,每个答案都没有帮助我,因为我尝试过的所有内容都会生成一个零字节文件。
Function getFilenames($facPath,$facility,[string[]]$fileIncludes) {
gci $facPath\* -Include $fileIncludes -Recurse |
Select-Object @{ expression={$_.name}; label='FILENAME' },
@{Name='FACILITY';Expression={$facility}} |
Export-Csv -NoTypeInformation -Path $scriptPath"\filenames.txt" -Append
}
getFilenames $vPath "Building 1" $vFiles
您可以将功能更新为以下内容。我删除了几个Select-Object
命令,因为你只需要一个。
Function getFilenames($facPath,$facility,[string[]]$fileIncludes) {
gci $facPath -Include $fileIncludes |
Select-Object @{ expression={$_.name}; label='FILENAME' },
@{Name='FACILITY';Expression={$facility}} |
Export-Csv -NoTypeInformation -Path "$scriptPath\filenames.txt" -Append
}
您可以像下面这样运行:
getFilenames "c:\folder\facilities\*" "manufacturing" "ab*","cd*","ef*"
PowerShell通过各种表示法确定字符串数组:
"string1","string2","stringx"
:以逗号分隔的名单[string[]]$stringArray
:打字或铸造@("string1","string2","string3")
:数组子表达式有关使用数组的更多信息,请参阅About Arrays。
试试这个!
$Include=@("ab*","cd*","ef*")
Get-Childitem -Recurse -Include $Include