0

I have a Windows service which calls a bat file. That bat file calls a PowerShell script, and inside that PowerShell script a VBScript is called.

Windows Service > bat file > powershell file > vbscript

When I manually run the bat file, the VBscript is successfully executed, but if I execute the same bat file from the Windows service, then all scripts are called, but the VBScript skips to run.

Executing a bat file manually successfully executes the VBScript, but not via Windows service

I tried to call The VBScript inside PowerShell in different ways:

  1. & c:\windows\system32\cscript.exe NameOfFile.vbs
  2. start-process
  3. invoke-expression
  4. C:\Windows\System32\cscript.exe NameOfFiles.vbs //B //Nologo $IP_SU $RemoteSessions_Output $user

My VBScript is:

dim ip
dim sessions_dir
dim temp
dim username
dim password

set temp = Wscript.Arguments
ip = temp(0)
sessions_dir = temp(1)
username = temp(2)
password = temp(3)

Sub WaitEnter()
    WshShell.AppActivate("telnet " & ip )
    WScript.Sleep 2000
    WshShell.AppActivate("telnet " & ip)
    WshShell.SendKeys "{Enter}"
    WshShell.AppActivate("telnet " & ip)
    WScript.Sleep 2000
End Sub

set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Sleep 1000
WshShell.AppActivate("telnet " & ip )
WshShell.Run "telnet " & ip & " -f " & sessions_dir & "\" & ip & "_SU_Status_Output.txt",2
WshShell.AppActivate("telnet " & ip)
WScript.Sleep 1000
WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys username
WaitEnter

WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys password
WaitEnter

WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys "SU_INrOmk=` pl | awk '{{}print {$}3{}}' | head -3 | cut -d '=' -f2`; SU_type=` pl | grep $SU_INrOmk | tail -1 | awk '{{}print {$}12{}}'`"
WaitEnter

WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys "echo $SU_type"
WaitEnter

WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys "exit"
WshShell.AppActivate("telnet " & ip)
WshShell.SendKeys "{Enter}"

and PowerShell script from where it is called is like:

if(Test-Path C:\Windows\System32\cscript.exe){
    echo "Cscript found"
    $command = "& C:\Windows\System32\cscript.exe NameOfFile.vbs $IP_SU $RemoteSessions_Output $user $DecPwd | out-null"
    Invoke-Expression -Command $Command
    start-Sleep 10
    if($?){
        start-sleep 10
        $SU_Output_File = $IP_SU + "_SU_Status_Output.txt"
        $SU_Remote_FilePath = $RemoteSessions_Output + "\" + $SU_Output_File
    }
}

i expect that VBScript is called when Windows service calls the bat file.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Please [format your code and sample input/output properly](http://meta.stackexchange.com/a/22189/248777). – mklement0 Nov 07 '19 at 12:22
  • 4
    `Windows Service -> Batch File -> PowerShell -> VBScript`. Just one question, why?? – user692942 Nov 07 '19 at 13:18
  • Do you need to specify the full path to your vbs file? Or at least set a working directory? Either specify the full path, or perhaps something like: Start-Process -FilePath "c:\windows\system32\cscript.exe" -ArgumentList "NameOfFile.vbs" -WorkingDirectory "c:\path\to\vbs"... – Captain_Planet Nov 07 '19 at 13:40

1 Answers1

0

I see several things that could be causing issues for you here.

I'm no master of VBS, but my guess here is that you are using Wscript which requires interactivity I think, but you should be using Cscript variant calls instead. My guess is that your script may be bombing due to this. Services can not run in an interactive context since Vista/Windows Server 2008.

You are calling this with Invoke-Expression, which (almost) always returns successful even if the cmdlet failed. In other words, Invoke-Expression will (almost) always set $? to $True even if the command failed. However, you can slip in an evaluation of ; $? at the end of your expression which will end up setting $? as you would expect, which breaks the notion of "always sets $? to $True".

However, you are also using $? incorrectly as well. $? only evaluates the success of cmdlets, not commands. cscript.exe is an executable, and must have its success evaluated with the $LASTEXITCODE automatic variable instead. This will be typically be 0 for success and any other value for non-success. You will have to check the success of this $LASTEXITCODE yourself, as even when the ErrorActionPreference is set to Stop, it won't automatically treat non-zero exit codes as terminating errors.

Outside of the scope of this answer, but worth a mention, I would recommend replacing Invoke-Expression with a straight call to the executable and splat your parameters instead.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • Thanks Bender for replying, but i didn't get the first para. i am using cscript only and not the wscript. i am newbie to vbscript and telnet, please let me know which code line shows that i am using wscript and not cscript. – user6055818 Nov 08 '19 at 06:24
  • Line 7 of your vbs is the first occurrence I see – codewario Nov 08 '19 at 12:11