0

I want to invoke telnet.vbs within my PowerShell script.

That script should restart the host $line in every iteration of foreach loop.

telnet.vbs script:

 Option explicit
    Dim oShell
    set oShell= Wscript.CreateObject("WScript.Shell")"
    oShell.Run "telnet"
    WScript.Sleep 3000
    oShell.Sendkeys "open $line~"
    WScript.Sleep 5000
    oShell.Sendkeys "y~"
    WScript.Sleep 5000
    oShell.Sendkeys "fusion~"
    WScript.Sleep 4000
    oShell.Sendkeys "tnniw~"
    WScript.Sleep 4000
    oShell.Sendkeys "shutdown -r -t 0~"
    Wscript.Quit}

I want to run this script in loop and inside telnet.vbs, $line should be replaced in every iteration:

foreach($line in $servers)
{
   & .\telnet.vbs
}

I tried this:

(Get-Content .\telnet.vbs).Replace("(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))","$line") | Set-Content .\telnet.vbs

But it do nothing. Certainly the syntax is wrong.

TraPS
  • 35
  • 3
  • 10

1 Answers1

0

Pass the server name as an argument to the VBScript file. Like so,

# Powershell
$servers = get-content c:\my\servers\list.txt
foreach($s in $servers)
{
   # Call the VBScript and pass each server as argument
   & c:\my\scripts\telnet.vbs $s
}

' VBScript
Option explicit
Dim oShell
set oShell= Wscript.CreateObject("WScript.Shell")"

' Build the open command string by catenating "open",
' server name from script arguments and "<enter>" together
dim openCmd
set openCmd = "open " & WScript.Arguments(0) & "~"

oShell.Run "telnet"
WScript.Sleep 3000
oShell.Sendkeys openCmd
WScript.Sleep 5000
oShell.Sendkeys "y~"
WScript.Sleep 5000
oShell.Sendkeys "fusion~"
WScript.Sleep 4000
oShell.Sendkeys "tnniw~"
WScript.Sleep 4000
oShell.Sendkeys "shutdown -r -t 0~"
Wscript.Quit
vonPryz
  • 22,996
  • 7
  • 54
  • 65