'获取-ChildItem -包含/-排除'如果作为参数传递给函数,PowerShell 中不会过滤
我在获取 Get-ChildItem 的筛选器参数时遇到问题功能。
以下工作正常并显示完整的文件列表:
c:\temp\Get-ChildItem -Include *deleteme*.txt -Recurse
现在假设我有以下脚本
#file starts here
#filename = GetLastFile.ps1
param([string] $filter)
$files = Get-ChildItem $filter
Write-Host $files #should print all matching files but prints nothing
$file = $files | Select-Object -Last 1;
$file.name #returns filename
#File ends here
现在尝试运行该脚本,但
c:\temp.\GetLastFile.ps1 "-Include *deleteme*.txt -Recurse"
没有返回任何内容。
提供过滤器 *.*
效果很好。由于 -Include
或 -Exclude
,它似乎失败了。有什么想法吗?
I have a problem getting a filter argument to Get-ChildItem in a function.
The following works fine and displays a whole list of files:
c:\temp\Get-ChildItem -Include *deleteme*.txt -Recurse
Now say I have the following script
#file starts here
#filename = GetLastFile.ps1
param([string] $filter)
$files = Get-ChildItem $filter
Write-Host $files #should print all matching files but prints nothing
$file = $files | Select-Object -Last 1;
$file.name #returns filename
#File ends here
Now trying to run the script,
c:\temp.\GetLastFile.ps1 "-Include *deleteme*.txt -Recurse"
returns nothing.
Supplying a filter, *.*
, works fine. It seems to be failing due to the -Include
or -Exclude
. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正开始进入 Powershell 2.0 代理功能可以提供帮助的领域。然而,除此之外,PowerShell 2.0 中还有一种简单的方法可以实现此目的,假设您只需要 -Include 和 -Recurse。实际上,我建议使用 -Filter 来代替它,它会做你想要的事情,坦率地说,它的速度要快得多(在我的一些测试中是 4 倍),因为 -filter 使用操作系统提供的文件系统过滤,而 -include 由 PowerShell 处理。
@
符号用于将数组或哈希表“分布”到命令的参数中。 $PSBoundParameters 变量是 PowerShell 2.0 中新增的自动变量,在函数中定义。它是一个包含所有有界(命名和位置)参数的哈希表,例如:当您针对命令使用这样的哈希表时,PowerShell 会将键的(例如 Recurse)值映射到命令上名为 Recurse 的参数。
You're starting to get into an area where where Powershell 2.0 proxy functions can help. However, short of that, here's a simple way in PowerShell 2.0 to do this assuming all you need is -Include and -Recurse. Actually, I would recommend using -Filter instead it will do what you want and frankly it's quite a bit faster (4x on some of my tests) because -filter uses filesystem filtering provided by the OS whereas -include is processed by PowerShell.
The
@
symbol is used to "splat" an array or hashtable across the parameters to a command. The $PSBoundParameters variable is an automatic variable new to PowerShell 2.0 that is defined in functions. It's a hashtable that contains all the bounded (named and positional) parameters e.g.:When you splat a hashtable like this against a command, PowerShell will map the key's (e.g. Recurse) value to the parameter named Recurse on the command.
我相信发生的情况是您的 $filter 参数被视为 Get-ChildItem 命令的单个字符串参数。因此,除非您有一个名为“-Ininclude deleteme.txt -Recurse”的目录,否则该命令将始终不返回任何内容。
至于解决你的问题,有很多方法可以解决它。更通用的方法之一可能是在传递 $filter 参数时切换程序行为,而不是传递整个过滤器字符串,只需传递“deleteme.txt”字符串。
I believe what is happening is your $filter parameter is being treated as a single string argument to the Get-ChildItem command. As such, unless you have a directory named "-Include deleteme.txt -Recurse", the command will always return nothing.
As for fixing your problem, well, there are a bunch of ways you could approach it. Probably one of the more versatile ways is to switch program behavior if the the $filter argument is passed, and instead of passing the entire filter string just pass the "deleteme.txt" string.
您可以使用 Invoke-Expression 来执行存储在变量中的命令。例如:
You can use Invoke-Expression to execute a command stored in a variable. For example: