Powershell - 对 HashMap 使用 "Contains"/"Match"函数

标签 powershell

我想检查以下单词中的以下字母是否存在。我的代码是:

$testString="Word"
$illegalCharacter =@{Warning="a","b";Error="c","d"}

if($testString -match $illegalCharacter.Error){
Write-Host "WORKS!"
}

我猜“-contains”仅适用于数组 - 不适用于 HashMap 。但为什么这不起作用?

谢谢:)

最佳答案

您必须使用稍微不同的方法:

$testString="Word"
$illegalCharacter =@{Warning=@("a","b");Error= @("c","d")}

$illegalCharacter.Error | ForEach-Object { 
    if( $testString.Contains( $_ ) ) {
        Write-Host "WORKS!"; break 
    }
}  

关于Powershell - 对 HashMap 使用 "Contains"/"Match"函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60396909/

相关文章:

powershell - 使用 Powershell 定位和更新文本文件中的键值

PowerShell - 如何在 REST 调用的 JSON 字符串中转义 {}

powershell - 修改值时的Powershell选择对象和更改属性名称

powershell - 比较 Powershell 中的两个列表

powershell - 通过 Exchange Online REST Api 修改用户

Powershell - 列出一个目录中的所有备用数据流信息

powershell - Powershell:从日志文件中提取数据以创建新对象

arrays - 如何将哈希表添加到多维数组?无法通过成员枚举分配值

.net - 是否有标准的 DateTime 字符串表示形式作为 Windows 文件名有效并且可以使用 DateTime.Parse 进行解析?

entity-framework - 无法加载文件,因为在此系统上禁用了脚本的执行