PowershellWhere-Object似乎没有过滤

2023-12-05

我正在尝试对 Azure 策略进行一些报告。我最终将过滤日期,但无法过滤任何内容,因此提供以下示例。

PS C:\>$defstrings = az policy definition list --management-group "mgsandbox"   # returns an array of strings
PS C:\>$def = ConvertFrom-Json -InputObject ($defstrings -join "`n") -depth 99  # converts to an array of PSCustomObject
PS C:\>$def.count
2070
PS C:\>$sel = Where-Object -inputobject $def -FilterScript { $_.displayName -eq "Kubernetes cluster containers should not share host process ID or host IPC namespace" }
PS C:\>$sel.count
2070
PS C:\> $def[0].displayName -eq "Kubernetes cluster containers should not share host process ID or host IPC namespace"
False

虽然我可能会在 displayName 上找到多个命中项,但显然有一组非零的 displayNames 可以做到这一点not匹配过滤器,但选择得到了所有这些。

有什么建议我的语法有什么问题吗?看起来很简单。


不要使用-InputObject argument with Where-Object; 相反,提供输入通过管道:

# Use the pipeline to provide input, don't use -InputObject
$def | Where-Object -FilterScript { $_.displayName -eq "Kubernetes cluster containers should not share host process ID or host IPC namespace" }

In mostcmdlet,-InputObject参数只是一个实施细节其目的是方便管道输入并且不能被有意义地使用directly; see 这个答案了解更多信息,以及GitHub 问题 #4242进行讨论。


As for 你尝试过什么:

当你使用-InputObject,传递一个集合(可枚举)的参数作为一个整体到 cmdlet,而使用相同的在管线中导致其枚举,即集合的elements都通过了,逐个.

一个简化的例子:

# Sample array.
$arr = 1, 2, 3

# WRONG: array is passed *as a whole*
#        and in this case outputs *all* its elements.
#        -> 1, 2, 3
Where-Object -InputObject $arr { $_ -eq 2 } 

也就是说,脚本块传递给Where-Object被执行once,与自动$_变量绑定到数组作为一个整体,因此上面的效果等效于:

if ($arr -eq 2) { $arr }

Since $arr -eq 2评估为$true在布尔上下文中(if有条件),$arr 作为一个整体是输出(尽管在输出时它被枚举),给人的印象是没有进行过滤。

  • The reason that $arr -eq 2 evaluates to $true is that the -eq operator, among others, supports arrays as its LHS, in which case the behavior changes to filtering, by returning the sub-array of matching elements instead of a Boolean, so that 1, 2, 3 -eq 2 yields @(2) (an array containing the one matching element, 2), and coercing @(2) to a Boolean yields $true ([bool] @(2)).[1]

相反,如果隐含条件产生$false (e.g., $_ -eq 5),根本不产生任何输出。

相比之下,如果您使用管道,您将获得所需的行为:

# Sample array.
$arr = 1, 2, 3

# OK: Array elements are enumerated, i.e. 
#     sent *one by one* through the pipeline.
#     -> 2
$arr | Where-Object{ $_ -eq 2 } 

或者,您可以bypass管道通过使用固有的.Where() method:

注意:这需要先收集内存中的所有输入;然而,特别是当数据已经在内存中时,这种方法的性能比管道方法更好:

# OK:
# -> 2 (wrapped in a collection)
@(1, 2, 3).Where({ $_ -eq 2 })

Note: .Where() always输出类似数组的收藏,即使只有一个single对象与过滤器匹配。但实际上,这通常并不重要。


[1] For a summary of PowerShell's to-Boolean coercion rules, see the bottom section of this answer.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PowershellWhere-Object似乎没有过滤 的相关文章

  • Start-Job 将 XML 对象传递给 -ArgumentList 在 Powershell V2 和 V3 中工作方式不同

    我正在使用 Powershell v3 在 Windows Server 2012 上测试我的应用程序的部署脚本 该脚本在使用 Powershell v2 的 Win Server 2008 R2 和 Win 7 上运行良好 我现在遇到的问
  • 多个组合框绑定到一个公共源,强制执行不同的选择

    我正在尝试将多个 ComboBox 绑定到一个公共源集合 并强制执行一旦进行 ComboBox 选择 该所选项目就有可能从其他 ComboBox 中删除 该集合是动态构建的 因此我是用代码来完成的 到目前为止 我已经尝试以多种方式实现这一点
  • 如何使用PowerShell批量调用Update-Database

    我们使用 Azure 弹性池生成多个客户端数据库和一个引用客户端数据库的主数据库 我们已经拥有多个数据库 并且正在开发新版本的代码 我们使用 EF6 代码优先 当我们更改模型 添加属性 时 我们创建迁移文件并需要调用Update Datab
  • Docker 在 Powershell 中登录 gcr.io

    我正在尝试使用 Windows 10 登录 Google 容器注册表JSON 密钥文件 https cloud google com container registry docs advanced authentication using
  • 更改每个命令的 powershell 标题

    我想在窗口标题中显示我在 powershell 中输入的最后一个命令 以便更容易找到 目前我有 C gt host ui rawui WindowTitle 但这只是获取相对于我输入时的上一个命令 所以如果我有 C gt cd C gt h
  • $ 之间的区别?和 PowerShell 中的 $LastExitCode

    在 PowerShell 中 有什么区别 and LastExitCode I read 关于自动变量 http technet microsoft com en us library dd347675 aspx 它说 Contains t
  • 基于局部变量的Django条件过滤器

    我是 django 的新手 想知道除了 if 语句之外是否还有更有效的条件过滤方法 Given test names all test types a b c more lists 我知道我可以这样做 q tests objects all
  • Powershell - 函数中的匹配 - 返回时获得额外的 true/false

    为什么我在这个函数的结果上得到提取 True 或 False 当我想要返回的只是邮政编码时 Function GetZipCodeFromKeyword String keyword pattern d 5 keyword match pa
  • 输入屏蔽密码

    我从其他帖子中拼凑了一个简单的例子 CmdletBinding DefaultParameterSetName Secret Param Parameter Mandatory True string FileLocation Parame
  • 如何从 Powershell 访问 COM 对象上的索引属性

    我正在使用 Powershell 通过 COM 与 Windows 7 任务计划程序服务进行通信任务调度2 0接口 http msdn microsoft com en us library aa383600 VS 85 aspx 例如 I
  • 如何在 Windows 7 上安装 ScheduledTasks 模块

    是否可以安装该模块 http technet microsoft com en us library jj649816 aspx与 Windows 8 和 Windows Server 2012 不同的操作系统上的 PS v3 0 附带吗
  • 为什么乘法不适用于 Read-Host 值

    table num Read Host Prompt Enter the table number you want to get printed for i 1 i lt 11 i ans table num i write table
  • Powershell 没有一些内置帮助主题

    我发现我的 powershell 没有一些帮助主题 例如about profiles 我试过update help但这没有帮助 有谁可以帮忙看一下吗 PS C gt systeminfo Host Name OS Name Microsof
  • 为所有用户持久安装 PowerShell 模块

    我正在通过以下方式安装 PowerShell 模块八达通部署 http octopusdeploy com 到许多不同的服务器上 出于测试目的 我按照 Microsoft 文档的指导进行了安装PowerShell模块 https learn
  • powershell 将文件添加到 zip

    我正在尝试使用 powershell 将文件添加到 zip 文件 我可以创建 zip 文件 但无法弄清楚如何将我的文件添加到其中 我在用着 zipfilename c cwRsync backup zip file c cwRsync ba
  • 使用 Powershell 远程安装 .msi

    我已经让他使用这个论坛上存在的代码来跟踪代码 cls computername Get Content C Users C201578 db Documents server txt sourcefile iceopsnas LNT Sof
  • 如何通过 *.csproject 文件查找参考路径

    我想制作一个自动化的 powershell 脚本 报告项目的引用和引用路径 当 csproj 中的提示路径未填写时 我找不到获取引用路径的方法 这是一个快速解决方案 它抓住了每一个 csproj当前目录下的文件 并检查每个引用 对于从 GA
  • 获取对 SOAP 的 XML 响应中的节点值

    我在 PowerShell 中发出 SOAP 请求 如下所示 uri https secure echosign com services EchoSignDocumentService20 WSDL sun Invoke WebReque
  • 用于验证 IIS 设置的 Powershell 脚本

    是否可以使用 Power Shell 脚本获取 IIS 设置 我希望使用脚本获取 检查以下信息 检查 Windows 身份验证提供程序是否正确列出 协商 NTLM 检查是否启用了 Windows 身份验证 Windows 身份验证高级设置
  • 使用Powershell访问远程Oracle数据库

    我需要能够连接到我的网络上基于 Windows 7 的 Oracle 服务器 32 位 Oracle XE 我需要连接的机器运行 Windows 7 64 位 两台机器上都安装了 Powershell 我已在 64 位计算机上安装了 Ora

随机推荐