1

I'm trying to run a Powershell script that will call a batch script that will start a telnet session and run the following command:

mediaGet("gei",1)

Thanks to this stackoverflow question Is it possible to use a batch file to establish a telnet session, send a command and have the output written to a file? I have written two scripts:

telnet.bat

start telnet.exe 162.100.10.10
cscript telnet.vbs

telnet.vbs

set OBJECT=WScript.CreateObject("WScript.Shell")
Dim cmd
cmd = "mediaGet("gei",1)
WScript.sleep 50 
OBJECT.SendKeys cmd
WScript.sleep 50 
OBJECT.SendKeys "{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys "exit{ENTER}" 
WScript.sleep 5000
OBJECT.SendKeys " "

However, when I run the scripts, the vbs script cannot read the string of the variable cmd. So I changed it as such:

cmd = "mediaGet" & chr(40) & chr(34) & "gei" & ",1" & chr(41)

But still, it outputs it on the telnet terminal as such:

mediaGet"gei",1

How can I make it read the parenthesis in the string variable? If the scripts end up working, does anyone know how to save the output of the vbs script into a log file?

Komputer
  • 51
  • 1
  • 16

1 Answers1

2

I have no idea what mediaget is or what it does (actually I've never heard of it), so, I will not comment on that.

However, running external commands via PowerShell is very common and well documented. See these:

PowerShell: Running Executables

Execution of external commands in PowerShell done right

All that being said...

If you are using the powershell.exe consolehost that is one thing, but all you are doing is calling VBScript and cmd.exe items. Since that is the case then why not just use cmd.exe?

Yet, PowerShell can do this directly, so, why the extra steps to .bat and .vbs?

Example: rough and untested, because I have no need to download and test mediaget

Start-Process -FilePath 'telnet.exe' -ArgumentList '162.100.10.10'
[system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")
$SendKeys = [System.Windows.Forms.SendKeys]
$sendkeys::SendWait('mediaGet gei,1')
Start-Sleep -Seconds .5
$sendkeys::SendWait("{ENTER}")
Start-Sleep -Seconds .5
$sendkeys::SendWait("exit{ENTER}")

You can even replace Telnet with your own code, using the .Net namespace.

New-Object System.Net.Sockets.TcpClient

Calling it this way ...

New-Object System.Net.Sockets.TcpClient('162.100.10.10', 80)

You can use that to write your own module or use this one from the MS powershellgallery.com.

A quick YouTube search shows me that mediaget is a web download tool.

If all you are doing is hitting a website / ftp site to download a file, you can do that directly in PowerShell as well. See this:

3 ways to download files with PowerShell

postanote
  • 15,138
  • 2
  • 14
  • 25
  • I did not realize it was possible to do it directly on PS. Thank you for this very informative and helpful response. mediaGet turned out to be a tool developed internally on the machine I'm using. I was not aware of this when posting the question. This tool returns the duplex status among other things, with a format that looks like: "media = 1234566, up: 1000 full duplex". It is returned on the telnet shell and I'd like to retrieve that result on my Powershell terminal. – Komputer May 15 '19 at 10:12
  • One weird thing that I noticed is when typing SendWait('mediaGet gei,1'), the telnet shell only reads that command as "diaGet gei,1". So I had to type '(space)(space)mediaGet gei,1' for it to properly read the command. It somehow disregards the first two characters, I have no idea why though. – Komputer May 15 '19 at 10:20
  • I have tried adding the parameters -NoNewWindow and -RedirectStandardOutput but both don't work. I still cannot retrieve my output results into my Powershell terminal. Do you have an idea how to do that? Thanks in advance. – Komputer May 15 '19 at 14:09