yorkman
yorkman

Reputation: 119

Powershell execute some.exe on a remote pc and enter multiple lines of text

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.

Upvotes: 0

Views: 273

Answers (1)

Eldo.Ob
Eldo.Ob

Reputation: 794

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}

Upvotes: 1

Related Questions