VBS Run exe opens app many times instead of one - vbscript

I'm automating process of connection to remote PC which includes VPN+SSH+VNC and ssh stage automatization opens too much new windows instead of one expected.
Code:
Set oShell = CreateObject("WScript.Shell")
oShell.Run("""G:\Git\git-bash.exe""")
oShell.AppActivate "MINGW64:/"
Dim command
command = "ssh -A username#adress options~"
oShell.SendKeys command
Opens from 3 to 5 git-bash instances instead of 1.
Sending keys works as intended in 1 of this 3-5 window. How to prevent opening others?

oShell.Run(oShell.ExpandEnvironmentStrings("%COMSPEC% /C (start G:\Git\git-bash.exe)")) should help.
Might also try adding a WScript.Sleep 3000 before calling oShell.AppActivate to give the OS time to launch git-bash.exe and its dependencies.
Hopefully, drive G: is a local drive. If not, increase the Sleep time to give any anti-virus time to scan the process.

Related

vbs script running with keyboard and mouse lock

The script I created below opens a number of virtual machines whilst the script is running the local machine's keyboard and mouse is locked via the shortkeys entry of the script CTRL ALT + F. When the script is finished I would like the keyboard and mouse to unlock using the same function.
Note that even though the keyboard is locked the hot keys to unlock the keyboard and mouse are still active these are CTRL ALT + F.
The script runs perfectly but does not unlock the keyboard and mouse, please can anyone see any issues with this script?
Dim shell
Set shell = wscript.CreateObject("Shell.Application")
set x = CreateObject("Wscript.shell")
x.sendkeys("^%{f}")
shell.Open "C:\Program Files\Oracle\VirtualBox\VirtualBox.exe"
WScript.Sleep 3000
shell.Open
"\\leeds.smoothwall.net\DFS\homefolder\steve.newton\Documents\Local
Automation scripts\VBS files\Paths\NetworkGuardian.lnk"
WScript.Sleep 120000
shell.Open
"\\leeds.smoothwall.net\DFS\homefolder\steve.newton\Documents\Local
Automation scripts\VBS files\Paths\IE11 - Win10 Admin.lnk"
WScript.Sleep 20000
shell.Open
"\\leeds.smoothwall.net\DFS\homefolder\steve.newton\Documents\Local
Automation scripts\VBS files\Paths\IE11 - Win10.lnk"
WScript.Sleep 20000
Set shell = wscript.CreateObject("Shell.Application")
set x = CreateObject("Wscript.shell")
x.sendkeys("^%{f}")
WScript.Sleep 2000
MsgBox "Automation Startup complete"
I found the solution, when the virtual machines started they automatically sent the user controls to the active windows, I turned this option off and now the script works perfectly.

How to launch .exe from vbscript on windows server 2003

I've got a script which needs to run on various different versions of windows server, including 2003. Yeah, I already KNOW it's "unsupported".
My script has to launch an executable, in a hidden window (though the code to do this is not shown below, because I was asked to cut it down to the bare minimum). I'm currently using win32_Process.Create as follows:
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("C:\myprog.exe", null, null, intProcessID)
This works ok on 2008 and 2012, but it is to be failing on 2003 with error code 3 "Insufficient privilege" returned in errReturn. It also works when run through cmd.exe, as an ordinary user, but the parent program is a service, check_mk_agent.exe, and so is not an "ordinary user". This script is run as one of check_mk_agent.exe's plugins.
I'm now going to work out how to use runas to try to simulate running it as the same user as the service runs as.
The two most common ways to launch a .exe from vbscript is WScript.Shell Run and Exec method. The main difference between the two is that you can capture the StdIn/StdOut/StdErr with the Exec method because applications are run in a child command shell.
Exec example:
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("calc")
Run example:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad " & WScript.ScriptFullName

VBscript run cmd and wait for output

I'm trying to read a user's PKI card data with Certutil and dump the data to a text file. The idea is to put this as part of a login script to gather some data on user's with expiring certificates.
Here is my code section:
set oShell = WScript.CreateObject("WScript.Shell")
strcommand = "cmd /c certutil -scinfo -silent > " & StrPath
oShell.Run strcommand, true
it seems to work, dumping the cert data to a text file (strpath variable), but once I add more lines to the script it never waits for the command window to finish. It just closes in a fraction of a second or so. I know it takes aobut 7 seconds to read the PKI Card. I've tried sleep as well as a do/while loop and nothing seems to allow the command window run its course. I've also tried the various intwindowstyle options listed here.
Appreciate any help.
oShell.Run strcommand, ,true should work. The second argument is the intWindowStyle argument, not bWaitOnReturn. You could also use
oShell.Run strcommand, bWaitOnReturn := true
If this doesn't work, you could try to use the Exec Method . The documentation that this link leads to has a nice example of using a loop with sleep that runs until the process finishes.

Script not working on windows 7

I am working on a VBScript where it activates an application, sends a password to that application and minimizes the application. My script fails sending the keys. The application does not get the password, but when I double-click on the script it receives the password and minimizes. I don't know where the error is.
It's called like this:
InitliszeUSb.bat:
pause
START %myDrive%"RunSanDiskSecureAccess_Win.exe"
pushd %~dp0
ping 10.10.10.10 -n 1 -w 10000 >nul
start /b "" cscript "D:\min.vbs"
Min.vbs:
Option Explicit
Dim oSHL : Set oSHL = CreateObject("WScript.Shell")
oSHL.AppActivate "SanDisk SecureAccess"
oSHL.SendKeys "pass1_word~" 'Enters Password
WScript.Sleep(3000)
oSHL.SendKeys "% n" 'Minimises the window
WScript.Quit
In Windows 7, I experienced issues when using SendKeys to send keys to other apps (the way like you do), when SendKeys is not running in elevated process. Sometimes the macro/script works 100%, sometimes it looks like keystrokes are getting lost. The only way around I found for that was running sender application (or script) elevated (i.e. "As Administrator").

Running bash script from IIS6

Running IIS6 on Windows 2003. I'm trying to set up a simple ASP page which runs a bash script:
dim wshShell
set wshShell = CreateObject("WScript.Shell")
dim command
command = "c:\inetpub\wwwroot\bin\bash.exe /cygdrive/c/inetpub/wwwroot/test.sh"
wshShell.Run(command)
set wshShell = nothing
I've configured IIS6 to use the account IUSR_SERVERNAME as the identity for the default application pool, and confirmed that the script executes when I run it from the command line using
runas /usr:IUSR_SERVERNAME [command]
If I set the command to be notepad.exe, IIS6 launches it (with no window, of course, but I can see it in Task Manager, and the user name is set to IUSR_SERVERNAME).
Is there something I'm overlooking that I need to configure? I've got a similar script running on Windows 7 / IIS7, and it wasn't difficult to get running.
Resolved: switched to the lightweight Mongoose web server (using a Tcl CGI script instead of ASP). For our purposes -- running a simple demo -- this works fine, and it was much less painful to get it up and running.

Resources