gpt4 book ai didi

windows - 将变量与包含开关一起使用时 Get-ChildItem 失败

转载 作者:可可西里 更新时间:2023-11-01 11:29:10 26 4
gpt4 key购买 nike

Powershell v4.0Windows 7

此代码有效并检索了我要查找的 2 个文件:

$dir = Get-Item -Path "C:\TestSource"
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include *.txt,*.inf

此代码也有效,但它只能找到 txt 文件:

$Dir = Get-Item -Path "C:\TestSource"
$Filter = "*.txt"
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include $Filter

但是,这不会返回任何对象:

$Dir = Get-Item -Path "C:\TestSource"
$Filter = "*.txt,*.inf"
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include $Filter

有必要将 $Filter 变量构建成一个数组,如下所示:

$Dir = Get-Item -Path "C:\TestSource"
$Filter = @("*.txt","*.inf")
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include $Filter

Get-ChildItem 上的 Microsoft 页面让我相信可以将变量与 Get-ChildItem cmdlet 一起使用。但是,除非变量是数组,否则为什么 cmdlet 不返回对象?由于显式字符串在第一个示例中有效,那么第三个示例不应该也有效吗?

最佳答案

Include 的参数始终是一个数组 - 在您的第一个示例中 -Include *.txt,*.inf 传递一个双元素数组作为过滤器。

在您的第三个示例中,它是一个逗号分隔的字符串。如果你传递一个数组,它应该可以工作:

$Dir = Get-Item -Path "C:\TestSource"
$Filter = "*.txt", "*.inf"
Get-ChildItem -Path "$($dir.FullName)\*" -File -Include $Filter

关于windows - 将变量与包含开关一起使用时 Get-ChildItem 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25331144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com