powershell - Where-Object 不过滤

标签 powershell

为什么在这种情况下,where-object 不起作用?

$controlFlowArgs = @{ waitForEnter = $false }
$controlFlowArgs | Format-Table
$controlFlowArgs | Format-List
$result = $controlFlowArgs | Where-Object -FilterScript { $_.Name -eq "waitForEnter" }
$result

输出

Name         Value # Format-Table
----         -----
waitForEnter False

Name  : waitForEnter # Format-List
Value : False

# Missing result would be here

最佳答案

$controlFlowArgs 是一个哈希表。您可能应该有不同的想法。

$result = $controlFlowArgs | Where-Object { $_["waitForEnter"] }

会将$false存储在$result中。 否则你可以直接使用哈希表:

if ($controlFlowArgs["waitForEnter"]) {
  ...
}

关于powershell - Where-Object 不过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70159011/

相关文章:

PowerShell New-Item 将创建脚本所在的目录,而不是指定的路径

powershell - 如何在 PowerShell 中获取进程启动时间

如果在 exe 中转换,Powershell 进度条不显示

azure - 用于在 blob 容器内创建子目录并向其中插入一些数据的 PowerShell 脚本

regex - 如果前缀与列表中的一个匹配,则使用正则表达式提取字符串中的子字符串

windows - 在使用 CredSSP 身份验证能够运行无人值守命令时,是否有任何方法可以绕过凭据提示?

python - 从按日期/名称分组的单独目录中的 JPEG 生成 PDF

Powershell 命令仅在作为脚本而不是在控制台中运行时失败并显示 "Invalid namespace"

azure - 是否可以通过单用户登录提示使用 MFA 对 Connect-AzAccount 和 Connect-AzureAD 进行身份验证?

powershell - 带双引号的参数没有通过 ArgumentList 正确传递给 Scriptblock