Home > Net >  How to Expand a property in a variable
How to Expand a property in a variable

Time:10-27

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 ?

CodePudding user response:

You can use a calculated property to convert the string[] to string:

PS /> $c[0].strings.GetType()

IsPublic IsSerial Name                     BaseType
-------- -------- ----                     --------
True     True     String[]                 System.Array
Resolve-DnsName google.com -Type TXT -Server '8.8.8.8' |
Select-Object Name,Type,TTL,Section,@{n='Strings';e={[string]$_.Strings}}|
Format-Table

Results in:

Name       Type  TTL Section Strings                                                             
----       ----  --- ------- -------                                                             
google.com  TXT 3600  Answer docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e                       
google.com  TXT 3600  Answer google-site-verification=wD8N7i1JTNTkezJ49swvWW48f8_9xveREV4oB-0Hf5o
google.com  TXT 3600  Answer docusign=1b0a6754-49b1-4db5-8540-d2c12664b289                       
google.com  TXT 3600  Answer google-site-verification=TV9-DBe4R80X4v0M4U_bd_J9cpOJM0nikft0jAgjmsQ
google.com  TXT 3600  Answer globalsign-smime-dv=CDYX XFHUw2wml6/Gb8 59BsH31KzUr6c1l2BPvqKX8=    
google.com  TXT 3600  Answer MS=E4A68B9AB2BB9670BCE15412F62916164C0B20BB                         
google.com  TXT 3600  Answer apple-domain-verification=30afIBcvSuDV2PLX                          
google.com  TXT 3600  Answer v=spf1 include:_spf.google.com ~all                                 
google.com  TXT 3600  Answer facebook-domain-verification=22rm551cu4k0ab0bxsw536tlds4h95 

If there where more than one string in the Strings property using this would look like below:

Name       Type  TTL Section Strings                                                                                    
----       ----  --- ------- -------                                                                                    
google.com  TXT 3339  Answer docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e

If you wanted a multiline string instead, you could use Out-String however that would require first to trim() the output and also use -Wrap on Format-Table if you want it to be displayed correctly on the console.

An example of how the code would look like using a hardcoded value docusign=05958488 in this case :

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

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

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

Both snippets would 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 script 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})

$result = foreach($element in $result)
{
    $out = @{
        Name = $element.Name
        Type = $element.Type
        TTL = $element.TTL
        Section = $element.Section
    }

    $element.Strings.ForEach({
        $out.Strings = $_
        [pscustomobject]$out
    })
}

$result | Format-Table

This will recreate the object adding a new element if there is more than one value:

 TTL Section Name       Type Strings                                                             
 --- ------- ----       ---- -------                                                             
3444  Answer google.com  TXT apple-domain-verification=30afIBcvSuDV2PLX                          
3444  Answer google.com  TXT 1419241945                                                          
3444  Answer google.com  TXT MS=E4A68B9AB2BB9670BCE15412F62916164C0B20BB                         
3444  Answer google.com  TXT 463070462                                                           
3444  Answer google.com  TXT facebook-domain-verification=22rm551cu4k0ab0bxsw536tlds4h95         
3444  Answer google.com  TXT 958788674                                                           
3444  Answer google.com  TXT google-site-verification=TV9-DBe4R80X4v0M4U_bd_J9cpOJM0nikft0jAgjmsQ
3444  Answer google.com  TXT 1623605637                                                          
...
...
...
  • Related