Powershell gci 中的一个函数。将带有通配符的文件名变量传递给函数

Powershell gci in a function. Pass filename variables with wildcards into the function

我试图将参数传递给一个函数来执行 get-childitem。似乎无法做到。此外,我无法删除我的 post,所以我只是清空它,并对我试图弄清楚的内容进行基本解释。似乎我无法传递带有通配符的文件名变量。到目前为止,这里的每个答案都对我没有帮助,因为我尝试过的所有内容都会生成一个零字节文件。

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

试试这个!

$Include=@("ab*","cd*","ef*")
Get-Childitem -Recurse -Include $Include

您可以将函数更新为以下内容。我删除了几个 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