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)))
CodePudding user response:
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.- See this answer for more information.
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.