0

I'm trying to find a way to run any executable on a remote pc (we can use nslookup.exe as an example) and enter a few lines as if you were typing each line by line, pressing enter, etc:

nslookup.exe server 192.168.2.5 google.com yahoo.com exit

This can't be a script. It must be executed on a single line when running from powershell. Maybe something like this but this won't work because I don't know how to enter each new line in a single powershell command:

Invoke-Command -ComputerName server1 -ScriptBlock {nslookup.exe @{
server 192.168.2.5
google.com
yahoo.com
exit
 }
}

So no arguments, no reading from a text file. Basically a persistent remote session that runs the above commands and returns the results, all from one line in powershell (non-interactively). Is this possible?

UPDATE: I almost have it working with this:

$options = "server 8.8.8.8`ngoogle.com`n";$op1 | nslookup

but if I try the same with:

invoke-command -computername server1 -scriptblock {$options = "server 8.8.8.8`ngoogle.com`n";$options | nslookup}

it returns:

Default Server:  dc01.mydomain.local
Address:  192.168.200.21

> Default Server:  dns.google
Address:  8.8.8.8

Non-authoritative answer:
    + CategoryInfo          : NotSpecified: (Non-authoritative answer::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
    + PSComputerName        : server1

NotSpecified: (:) [], RemoteException
> Server:  dns.google
Address:  8.8.8.8

Name:    google.com
Addresses:  2607:f8b0:400b:802::200e
          172.217.164.238

So it worked but threw an error for some reason.

yorkman
  • 119
  • 2
  • 16
  • this should be easy You're not that wrong with your idea. But I think you hae to execute nslookup several times. Something like Invoke-Command ... -ScriptBlock { nslookup google.com 192.168.2.5; nslookup yahoo.com 192.168.2.5 } just seperate each lookup with a semicolon and hav a look at the nslookup command. nslookup /? tells you nslookup host server, where host is the host youre looking for and server the dns server – Eldo.Ob Feb 11 '21 at 23:29
  • I know I can do it that way but again, for my purposes it has to work the same way as if you typed nslookup on the remotepc and pressed enter, then you type server 8.8.8.8 and pressed enter, etc. The exe could be sqlcmd.exe instead of nslookup, where you would then enter queries or create tables, etc., on separate lines before typing exit to close sqlcmd.exe. So I think I'm missing a way to put in a carriage return inside @{server 8.8.8.8 google.com ...} – yorkman Feb 12 '21 at 01:15
  • Once you are inside nslookup or sqlcmd you are no longer in powershell and cannot use powershell to send additional commands. You have the right idea now where you are sending all the command you need to nslookup which allows this type of interaction. – Daniel Feb 12 '21 at 03:00
  • The Error istnt because of your Script. It's a normal "Error" as a response from nslookup. Just google it. But you could try to replace it while storing it in a var. $output = invoke-command..... after that replace the var – Eldo.Ob Feb 12 '21 at 21:31
  • @ Edlo: It can't be a normal error since when I type the same commands manually shows no error. @Daniel: I think you're wrong because I'm able to pass commands to any program with this method. I've already tested this on another exe. I just don't understand why I'm getting the error for nslookup. – yorkman Feb 13 '21 at 03:10
  • Yes, I agree. I think we are misunderstanding each other (maybe). I thought you were trying to have powershell first start nslookup, then enter each command individually like you can do with an interactive session at the console (you do say this in your comment). Still, what you pipe to an application or what arguments you can supply when you call the application depend on what the application is programmed to take. You can just take any number of arguments and just throw them at the application expecting it to do what you want. – Daniel Feb 13 '21 at 04:13
  • You also cannot start an exe interactively and and continue on to the next line of code in you script. At this point powershell has passed control to the application and is waiting patiently for the application to exit and return control to powershell where then it will continue with the rest of the script. You can start the exe in another window/process however your script will not magically start entering the next commands in the script in that new window. The commands would just be executed in the powershell session and probably error since they are not valid powershell commands. – Daniel Feb 13 '21 at 04:18
  • I recommend trying @Eldo.Ob suggestion. nslookup accepts the commands as he wrote them. This is a valid way of calling nslookup. If you do `nslookup /?` you will see `nslookup [-opt ...] host server # just look up 'host' using 'server'`. There are no options to call nslookup in the way you are describing. There is also no option that shows that you can stack multiple lookups into one single call to nslookup. I could be missing something I suppose. If you do figure it out please be sure to come back here and shame me :) – Daniel Feb 13 '21 at 04:24
  • and after all that I think I might have just figured it out :) `"yahoo.com 8.8.8.8", 'google.com 8.8.8.8', 'microsoft.com 8.8.8.8' | nslookup` Pipe an array/comma separated list of commands to nslookup. Need the host and alternate server in each – Daniel Feb 13 '21 at 04:30
  • https://stackoverflow.com/questions/66181442/sending-arguments-to-running-programs-that-expect-user-input-automatically – Daniel Feb 13 '21 at 04:43
  • Thanks but nslookup was just an example and it seems your solution doesn't work with every program. I tried 'help','quit' | telnet and it didn't show the help options at all. What I need is a way to enter certain commands after loading any exe as if I was typing it manually and pressing enter after each command. This seems to be taylored to work with nslookup only. So yes, I was saying it should work the same as if the session was interactive. – yorkman Feb 14 '21 at 05:19
  • @Eldo, sorry can you please show the full command? I don't understand what you mean by replace var(iable)? Keep in mind I'm not doing this in a script. It hast to be a one line command or commands separated by commas or whatever works. – yorkman Feb 14 '21 at 05:25

1 Answers1

1

Just use the 2>$null to suppress the output of STDERR by redirecting it to the $null variable,

invoke-command -computername server1 -scriptblock {$options = "server 8.8.8.8`ngoogle.com`n";$options | nslookup 2>$null}
Eldo.Ob
  • 774
  • 4
  • 16
  • Thanks. That gets rid of the error I was getting before unfortunately this is only for nslookup. It doesn't resolve the issue in question when using any exe. But something tells me this is not possible. – yorkman Feb 16 '21 at 22:20