Home > Back-end >  Shortening this script as much as possible using wildcard/regex/code golfing
Shortening this script as much as possible using wildcard/regex/code golfing

Time:09-17

I have made a short script that will pull dns txt records down from a server and combine them into a single string. DNS txt records are:

1.website.com 2.website.com 3.website.com

each of these are iterated through using the single digit number. the double quotes at the beginning and the end of the strings are trimmed This works exactly as I would like it too I am simply interested in seeing how short I can get it assistance is appreciated please and thank you

$i = 1
1..2 | foreach {
$w="$i.website.com"
sv -N v -Va ((nslookup -q=txt $w )[-1]).Trim().Trim('"')
$p  = "$v"
$i  = 1
}

and ideally i'd like to return the $p variable, that being the concatenated string

CodePudding user response:

There's a number of obvious consolidations you can make here, most notably the fact that neither $i nor $w is necessary.

That being said, I'd strongly recommend using Resolve-DnsName and let that take care of parsing the output for you

1..3|%{$p =Resolve-DnsName "$_.website.com." -Ty TXT -EA 0|% S*s}

The % S*s command will resolve the Strings property on the output records (only property matching the wildcard pattern)

  • Related