【问题标题】:Get-ChildItem -Filter ArrayGet-ChildItem -过滤器数组
【发布时间】:2014-12-21 12:18:32
【问题描述】:

情况:

  1. Get-ChildItem $Path -Filter *.dll 为我工作

  2. 这行得通:

    $Path = "$env:windir\system32\*"
    $GuyArray = @("*.dll", "*.exe")
    
    Get-ChildItem $Path -Include $GuyArray
    
  3. 但我无法正常工作:

    $Path = "$env:windir\system32\*"
    $GuyArray = @("*.dll", "*.exe")
    
    Get-ChildItem $Path -Filter $GuyArray
    

错误信息:

无法将“System.Object[]”转换为参数“Filter”所需的类型“System.String”。不支持指定的方法。

问题:

  1. 这是否意味着-Include 支持多个值,而-Filter 只允许一个值?
  2. 如果上面的解释是正确的,我有没有办法从Get-Help gci发现这个?

【问题讨论】:

  • 第 3 项应该是Get-ChildItem $Path -Filter $GuyArray吗?
  • 是的,我弄错了,#3 应该是 -Filter 而不是 -Include

标签: powershell filter


【解决方案1】:

这是否意味着-Include 支持多个值,而-Filter 只允许一个值?

是的。

如果上面的解释是正确的,我有没有办法从Get-Help gci发现这个?

是的,但您没有通过Get-Help gci -Parameter Filter 获得太多信息。但是你仍然可以看到它是一个字符串,而不是一个数组。至于细节,Filter 是提供者特定的过滤器。 Get-Help gci 无法告诉您有关特定提供程序中的实施的任何信息。理论上,Get-Help FileSystem(有关此提供者的帮助)应该已经解释了这一点。

附:另请注意,此过滤器使用 CMD 通配符规则而不是 PowerShell 通配符规则。

【讨论】:

  • 接受数组的参数在字符串后面有方括号吗? -包括。而那些只接受字符串的有 -Filter ?
【解决方案2】:

使用Get-Help

> get-help Get-ChildItem

NAME
    Get-ChildItem

SYNTAX
    Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Exclude <string[]>] [-Force] [-Include <string[]>] [-Name] [-Recurse] [-UseTransaction] [<CommonParameters>]

SYNTAX 部分包含参数类型,从中可以看出Filter 是字符串,Path 是数组。

【讨论】:

  • 谢谢,我知道现在要找什么了。
【解决方案3】:

问题一:

是的。 -Filter 仅接受 [string] 作为输入。 -Include 接受 [String[]]

问题 2:

Get-help get-childitem -parameter filter给了

-Filter <string>
...explanation...

Get-help get-childitem -parameter include给了

-Include <string[]>
...explanation...

【讨论】:

  • 感谢您指出 中 [方括号] 的重要性
猜你喜欢
  • 2020-05-27
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多