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

问题描述 投票:0回答:2

我试图将参数传递给函数来执行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
powershell powershell-v3.0 powershell-v4.0
2个回答
0
投票

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


0
投票

试试这个!

$Include=@("ab*","cd*","ef*")
Get-Childitem -Recurse -Include $Include
© www.soinside.com 2019 - 2024. All rights reserved.