1

I have the following line of code that I use to download and combine dns text records from my website and execute them in powershell. I can run this command just fine from the console but can not figure out how to effectively run it from the run box (win+r)

1..3|%{$p+=Resolve-DnsName "$_.website.com." -Ty TXT -EA 0|% S*s};& ([scriptblock]::Create($p))

Below is a simplified version I use to pull down and execute a single dns text record using a single word instead of iterating through through numbers and combining them.

powershell . (nslookup -q=txt example.website.com)[-1]

The above simplified version works fine from the console but in order for it to work in the run box it has to be modified as seen below:

powershell "powershell . (nslookup -q=txt sub.website.com)[-1]"

I can not seem to find a way to modify that first example in a way that allows me to execute it from the run box. Doing something like the example below errors out. I have tried about 20 variations of the code below with no success

powershell .(1..3|%{$p+=Resolve-DnsName "$_.website.com." -Ty TXT -EA 0|% S*s};& ([scriptblock]::Create($p)))
mklement0
  • 382,024
  • 64
  • 607
  • 775
I am Jakoby
  • 577
  • 4
  • 19
  • You may need a continuation character : https://devblogs.microsoft.com/scripting/powertip-line-continuation-in-powershell/ – jdweng Nov 19 '22 at 10:52
  • What is `S*s` doing? What is the error? – jfrmilner Nov 19 '22 at 11:12
  • @jdweng, the line break in the original form of the question was a posting artifact (since corrected). In fact, the Run dialog won't even let you submit a multi-line command. – mklement0 Nov 19 '22 at 15:00
  • 1
    @jfrmilner, `S*s` selects a property name by wildcard pattern, which only works if only _one_ property of the input object matches. Looks like this was done for brevity. Try `'foo' | % L*h`, which is the same as `'foo' | % Length` – mklement0 Nov 19 '22 at 15:02
  • @mklement0 : I use the back tick all the time in my powershell scripts. – jdweng Nov 19 '22 at 15:23
  • @jdweng, so do I. How does this relate to my comment and to the question at hand? – mklement0 Nov 19 '22 at 15:38

1 Answers1

1

Try the following:

powershell 1..3|%{$p+=Resolve-DnsName \"$_.website.com.\" -Ty TXT -EA 0|% S*s};& ([scriptblock]::Create($p)))
  • The crucial change is to escape the " chars. as \", so that PowerShell considers them part of the command to execute.

  • There's no reason to use . (...) for execution - just use ... directly, as shown.

Note that the above command only works from no-shell environments such as the Windows Run dialog (WinKey-R) - to execute it from cmd.exe, you'd need additional quoting or escaping.

mklement0
  • 382,024
  • 64
  • 607
  • 775