dcaz
dcaz

Reputation: 909

How to Expand a property in a variable

if I go

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

When I enter $c I get

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}

How do I expand $c.strings ?

I know I can get the expanded strings and lose the rest with

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

How do I get the entire answer expanded ?

Upvotes: 3

Views: 530

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60045

You can use a calculated property to redefine the Strings property from string[] to 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

This would result in:

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....

If there where more than one string in the Strings property, using above code would join the strings with $OFS (Out Field Separator), by default a space:

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

If you want a multi-line string instead, you could use Out-String, however that would require first to .Trim() the output and also use -Wrap on Format-Table to be displayed correctly.

An example of how the code would look like using a hardcoded value (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

You could also use the -join operator to concatenate the strings with a CRLF (`r`n) to get a multi-line string:

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

Both examples result in:

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

If you want a solution that could handle both cases, Strings having only one value and Strings having multiple values, you could use something like this:

$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

Above logic would be creating a new object per element in the Strings property. The output would be:

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              

Upvotes: 8

Related Questions