Script not working on windows 7 - vbscript

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").

Related

VBS Run exe opens app many times instead of one

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.

How to invisibly execute a privilege command in VBScript with a normal user

Today I do e.g.:
Set WshShell = CreateObject("WScript.Shell")
Call WshShell.Run("psexec -u administrator -p pw1234 cmd /c netsh interface ip show address > C:\Output.txt", 2, True)
This works, but the command prompt window is always visible for a short time.
I've been searching the web and I've tried everything I could possibly think of to hide/minimize this window, but unfortunately without success.
So I'm afraid that there's no way to run psexec in a hidden/minimized window.
But is there perhaps another way to execute a privilege command in VBScript with a normal user where no command prompt window is visible?

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.

Windows scheduler unable to send command to putty

I am logging into the server as Administrator to winserver2008.
I created a script called: vbscript.vbs
The purpose of this script is to auto login to linux via putty, then perform command line task.
Dim Shell
Set Shell = CreateObject("WScript.Shell")
output = Shell.Run("C:\putty.exe 1.2.3.4 9321")
wscript.sleep(500)
Shell.Sendkeys "root" & VBCrLf
wscript.sleep(30)
Shell.Sendkeys "password" & VBCrLf
wscript.sleep(30)
When I manually click on vbscript.vbs to execute it, vbscript will fill in root and password to putty.
When I use windows scheduler call to vbscript.vbs to execute it, vbscript won't fill in root and password to putty.
I suspect some permission issue.
I already set putty.exe to run as administrator, allow administrator, administrators group permission for it, but still fail to work when call via windows scheduler.
=====
I just tried with the second scenario, send 2 to the windows calculator, fail too..
testcalc.vbs
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "Calc.exe"
objShell.AppActivate "Calculator"
objShell.SendKeys "2"
Give up on trying to get SendKeys to work from a scheduled task, it's not going to happen. Instead simply pass the login and password on the command line:
output = Shell.Run("C:\putty.exe -l root -pw password 1.2.3.4 9321")
Alternatively do it with a session file and use -load.
If you are then going to execute commands over this connection then I believe you actually want plink rather than putty.

Resources