使用Powershell匹配键时获取空哈希值 [英] Getting null hash value while matching key using powershell
问题描述
我试图通过匹配键名(忽略白色和字符大小写)来获取键的值.
I am trying to get the value of the key by matching key name(ignoring the white and character case).
代码:
$tagHash = (Get-AzResourceGroup -Name "twmstgmsnp").Tags
Write-Host "Resource Group tags key : " $tagHash.Keys
Write-Host "Resource Group tags value : " $tagHash.Values
$ownervalue = $tagHash.GetEnumerator() | ? {($_.Key).ToString().Replace(' ','') -eq 'CreatedBy'} | % Value
Write-Host "Resource Group CREATEDBY tag : " $ownervalue
结果:
资源组标签键:PURPOSE
Created By
资源组标签值:QA MS Team2 env
Shubham Mishra
资源组CREATEDBY标签:
Resource Group CREATEDBY tag :
注意:如果键的文本应为"createdby",则应始终获取该值.密钥是否为创建者",创建者",创建者",创建者".它应该忽略关键的空白和大小写.
推荐答案
我的猜测是,您可以通过首先替换键中的所有空格,然后将其与CreatedBy
进行比较,来最轻松地获得键的确切名称.
无需GetEnumerator()
方法,只需从.Keys
数组获取键名:
My guess is that you can get the exact name for the key easiest by replacing all whitespace from it first and next compare it to CreatedBy
.
No need for the GetEnumerator()
method, simply get the key name from the .Keys
array:
$ownerKey = $tags.Keys | Where-Object { ($_ -replace '\s') -eq 'CreatedBy'}
Write-Host "Resource Group CREATEDBY Tag : $ownerKey"
Write-Host "Resource Group CREATEDBY Value : $($tags[$ownerKey])"
输出:
Resource Group CREATEDBY Tag : Created By
Resource Group CREATEDBY Value : Shubham Mishra
默认情况下,-eq
运算符不区分大小写.如果您需要在其他地方进行区分大小写的比较,请使用-ceq
By default the -eq
operator works case-insensitive. If you need case-sensitive comparison somewhere else, use -ceq
这篇关于使用Powershell匹配键时获取空哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!