需要帮助从 Powershell 中的 Hashtable 检索 Deep 值
Need help retrieving Deep values from Hashtable in Powershell
我有点需要帮助从 HashTable 中检索深层值。
我的情况:我编写了一个 C# 可执行文件,它以 JSON 格式提供我存储在变量 $Result 中的数据。
我能够获取和处理 $Result 的所有字段,除了名为 Properties 的嵌套哈希表及其键(Locations).我只需要列出名为 Properties
的 HashTable 中提到的城市
现在我可以得到 Locations Key,如下所示
PS C:\WINDOWS\system32> $Result.Properties.Locations
City=NewYork,Country=USA,Year=2001;City=Seattle,Country=USA,Year=2002;City=Miami,Country=USA,Year=2010;City=SanJose,Country=USA,Year=2009
PS C:\WINDOWS\system32>
预期输出:
NewYork
Seattle
Miami
SanJose
属性哈希表
[Properties]@
{
accountExpires=Never;
AccountLockedOut=False;
mail=JamesBond@internationalspy.com;
cn=Tom Cruise;
codePage=0;
countryCode=0;
displayName=Tom Cruise;
employeeID=743355;
Enabled=True;
givenName=Tom;
instanceType=4;
lastLogoff=12/31/1600 4:00:00 PM;
lastLogon=3/27/2020 7:52:36 AM;
lastLogonTimestamp=5/25/2020 12:47:07 PM;
lockoutTime=12/31/1600 4:00:00 PM;
logonCount=4;
mail=jamesbond@InternalActor.com;
Locations=City=NewYork,Country=USA,Year=2001;City=Seattle,Country=USA,Year=2002;City=Miami,Country=USA,Year=2010;City=SanJose,Country=USA,Year=2009
}
我需要获取嵌套在上述哈希表中的城市列表。
我只想要城市列表,以便我可以进一步应用我的业务逻辑并继续进行下去。
由于数据不是 json 数据,您将无法使用内部 powerhsell json cmdlet 来操作数据。由于我不知道任何可以开箱即用地处理您的结构的内部 powershell 对象,我建议通过 .net regex 提取数据:
$psString = '[Properties]@
{
accountExpires=Never;
AccountLockedOut=False;
mail=JamesBond@internationalspy.com;
cn=Tom Cruise;
codePage=0;
countryCode=0;
displayName=Tom Cruise;
employeeID=743355;
Enabled=True;
givenName=Tom;
instanceType=4;
lastLogoff=12/31/1600 4:00:00 PM;
lastLogon=3/27/2020 7:52:36 AM;
lastLogonTimestamp=5/25/2020 12:47:07 PM;
lockoutTime=12/31/1600 4:00:00 PM;
logonCount=4;
mail=jamesbond@InternalActor.com;
Locations=City=NewYork,Country=USA,Year=2001;City=Seattle,Country=USA,Year=2002;City=Miami,Country=USA,Year=2010;City=SanJose,Country=USA,Year=2009
}
'
$extract = [regex]::Match($psString, 'Locations=(.*)').Groups[1].Value
$extract = [regex]::Matches($extract, 'City=([^,]*)')
$extract | foreach {echo $_.Groups[1].Value}
给出所需的输出。
这里还有一些适合您的选择:
拆分和替换:
($Result.Properties.Locations -split '[;,]' | Where-Object { $_ -match '^City=' }) -replace '^City='
拆分和 ConvertFrom-StringData
($Result.Properties.Locations -split '[;,]' | ConvertFrom-StringData).City
遍历匹配项
$match = ([regex]'(?i)City=([^,]+)').Match($Result.Properties.Locations)
while ($match.Success) {
$match.Groups[1].Value
$match = $match.NextMatch()
}
全部结果
NewYork
Seattle
Miami
SanJose
我有点需要帮助从 HashTable 中检索深层值。
我的情况:我编写了一个 C# 可执行文件,它以 JSON 格式提供我存储在变量 $Result 中的数据。 我能够获取和处理 $Result 的所有字段,除了名为 Properties 的嵌套哈希表及其键(Locations).我只需要列出名为 Properties
的 HashTable 中提到的城市现在我可以得到 Locations Key,如下所示
PS C:\WINDOWS\system32> $Result.Properties.Locations
City=NewYork,Country=USA,Year=2001;City=Seattle,Country=USA,Year=2002;City=Miami,Country=USA,Year=2010;City=SanJose,Country=USA,Year=2009
PS C:\WINDOWS\system32>
预期输出:
NewYork
Seattle
Miami
SanJose
属性哈希表
[Properties]@
{
accountExpires=Never;
AccountLockedOut=False;
mail=JamesBond@internationalspy.com;
cn=Tom Cruise;
codePage=0;
countryCode=0;
displayName=Tom Cruise;
employeeID=743355;
Enabled=True;
givenName=Tom;
instanceType=4;
lastLogoff=12/31/1600 4:00:00 PM;
lastLogon=3/27/2020 7:52:36 AM;
lastLogonTimestamp=5/25/2020 12:47:07 PM;
lockoutTime=12/31/1600 4:00:00 PM;
logonCount=4;
mail=jamesbond@InternalActor.com;
Locations=City=NewYork,Country=USA,Year=2001;City=Seattle,Country=USA,Year=2002;City=Miami,Country=USA,Year=2010;City=SanJose,Country=USA,Year=2009
}
我需要获取嵌套在上述哈希表中的城市列表。 我只想要城市列表,以便我可以进一步应用我的业务逻辑并继续进行下去。
由于数据不是 json 数据,您将无法使用内部 powerhsell json cmdlet 来操作数据。由于我不知道任何可以开箱即用地处理您的结构的内部 powershell 对象,我建议通过 .net regex 提取数据:
$psString = '[Properties]@
{
accountExpires=Never;
AccountLockedOut=False;
mail=JamesBond@internationalspy.com;
cn=Tom Cruise;
codePage=0;
countryCode=0;
displayName=Tom Cruise;
employeeID=743355;
Enabled=True;
givenName=Tom;
instanceType=4;
lastLogoff=12/31/1600 4:00:00 PM;
lastLogon=3/27/2020 7:52:36 AM;
lastLogonTimestamp=5/25/2020 12:47:07 PM;
lockoutTime=12/31/1600 4:00:00 PM;
logonCount=4;
mail=jamesbond@InternalActor.com;
Locations=City=NewYork,Country=USA,Year=2001;City=Seattle,Country=USA,Year=2002;City=Miami,Country=USA,Year=2010;City=SanJose,Country=USA,Year=2009
}
'
$extract = [regex]::Match($psString, 'Locations=(.*)').Groups[1].Value
$extract = [regex]::Matches($extract, 'City=([^,]*)')
$extract | foreach {echo $_.Groups[1].Value}
给出所需的输出。
这里还有一些适合您的选择:
拆分和替换:
($Result.Properties.Locations -split '[;,]' | Where-Object { $_ -match '^City=' }) -replace '^City='
拆分和 ConvertFrom-StringData
($Result.Properties.Locations -split '[;,]' | ConvertFrom-StringData).City
遍历匹配项
$match = ([regex]'(?i)City=([^,]+)').Match($Result.Properties.Locations)
while ($match.Success) {
$match.Groups[1].Value
$match = $match.NextMatch()
}
全部结果
NewYork Seattle Miami SanJose