gpt4 book ai didi

powershell - 如何扩展变量中的属性

转载 作者:行者123 更新时间:2023-12-04 16:23:09 25 4
gpt4 key购买 nike

如果我去

$c = Resolve-DnsName facebook.com -Type TXT -Server '8.8.8.8'

当我输入 $c 我得到

Name                                     Type   TTL   Section    Strings
---- ---- --- ------- -------
facebook.com TXT 7200 Answer {v=spf1 redirect=_spf.facebook.com}
facebook.com TXT 7200 Answer {google-site-verification=A2WZWCNQHrGV_TW
wKh6KHY90tY0SHZo_RnyMJoDaG0s}
facebook.com TXT 7200 Answer {google-site-verification=wdH5DTJTc9AYNwV
unSVFeK0hYDGUIEOGb-RReU6pJlY}

如何扩展 $c.strings

我知道我可以得到扩展的字符串并丢失其余的

Resolve-DnsName facebook.com -Type TXT -Server '8.8.8.8' | Select-Object -ExpandProperty strings

如何扩展整个答案?

最佳答案

您可以使用 calculated propertyStrings 属性string[] 重新定义为 string:

Resolve-DnsName google.com -Type TXT -Server '8.8.8.8' |
Select-Object Name, Type, TTL, Section, @{ N = 'Strings'; E = { [string] $_.Strings }} |
Format-Table

这将导致:

Name         Type    TTL   Section   Strings           
---- ---- --- ------- -------
google.com TXT 3600 Answer docusign=05958488-....
google.com TXT 3600 Answer google-site-verifi....
google.com TXT 3600 Answer docusign=1b0a6754-....
google.com TXT 3600 Answer google-site-verifi....
google.com TXT 3600 Answer globalsign-smime-d....
google.com TXT 3600 Answer MS=E4A68B9AB2BB967....
google.com TXT 3600 Answer apple-domain-verif....
google.com TXT 3600 Answer v=spf1 include:_sp....
google.com TXT 3600 Answer facebook-domain-ve....

如果 Strings 属性中有多个字符串,使用上面的代码会将字符串与 $OFS (Out Field Separator) 连接起来。 , 默认为空格:

Name         Type    TTL   Section   Strings
---- ---- --- ------- -------
google.com TXT 3339 Answer docusign=4752-4e... docusign=4752-4e...

如果你想要一个多行字符串,你可以使用Out-String , 但是这需要先到 .Trim()输出并在 Format-Table 上使用 -Wrap才能正确显示。

使用硬编码值(docusign=05958488)的代码外观示例:

Resolve-DnsName google.com -Type TXT -Server '8.8.8.8' |
Select-Object Name, Type, TTL, Section, @{
Name = 'Strings'
Expression = { ('docusign=05958488', 'docusign=05958488' | Out-String).Trim() }
} | Format-Table -Wrap

您也可以使用 -join operatorCRLF (`r`n) 连接字符串以获得 多行字符串:

Resolve-DnsName google.com -Type TXT -Server '8.8.8.8' |
Select-Object Name, Type, TTL, Section, @{
Name = 'Strings'
Expression = { 'docusign=05958488', 'docusign=05958488' -join "`r`n" }
} | Format-Table -Wrap

这两个例子的结果都是:

Name         Type    TTL   Section   Strings
---- ---- --- ------- -------
google.com TXT 3574 Answer docusign=05958488
docusign=05958488
google.com TXT 3574 Answer docusign=05958488
docusign=05958488
google.com TXT 3574 Answer docusign=05958488
docusign=05958488

如果您想要一种可以同时处理这两种情况的解决方案,Strings 只有一个值,而Strings 有多个值,您可以使用如下方式:

$result = Resolve-DnsName google.com -Type TXT -Server '8.8.8.8'
# Add random values to Strings for testing
$result.ForEach({ $_.Strings += Get-Random })
# Code logic
$result = foreach($element in $result) {
$out = [ordered]@{
Name = $element.Name
Type = $element.Type
TTL = $element.TTL
Section = $element.Section
}

foreach($e in $element.Strings) {
$out.Strings = $e
# output one object per string
[pscustomobject] $out
}
}

$result | Format-Table

上述逻辑将创建一个新对象Strings 属性中的每个元素。输出将是:

TTL   Section  Name       Type   Strings
--- ------- ---- ---- -------
3444 Answer google.com TXT apple-domain-verificatio...
3444 Answer google.com TXT 1419241945
3444 Answer google.com TXT MS=E4A68B9AB2BB9670BCE15...
3444 Answer google.com TXT 463070462
3444 Answer google.com TXT facebook-domain-verifica...
3444 Answer google.com TXT 958788674
3444 Answer google.com TXT google-site-verification...
3444 Answer google.com TXT 1623605637

关于powershell - 如何扩展变量中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69731373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com