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:
start telnet.exe 162.100.10.10
cscript 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?
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: