vbs script running with keyboard and mouse lock - vbscript

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.

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 send username and password to the remote desktop (RDP) popup

Consider:
Dim WSHShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "MSTSC /v:servername"
WScript.Quit
I am able to open the RDP popup and give a server name to connect to. I need to provide the username, password, and click OK. Is there a way to achieve this from VBScript?
You can use the following code:
Dim objShell, strMachineName, strUserName, strUserPwd
set objShell = createObject("wscript.shell")
strMachineName = "enter-machine-name"
strUserName = "enter-your-user-name"
strUserPwd = "enter-user-password"
objShell.Run "cmdkey /generic:"&strMachineName&" /user:"&strUserName&" /pass:"&strUserPwd
objShell.run "mstsc /v: "&strMachineName
set objShell = Nothing
Reference on cmdkey
I have tested this on Windows 7 and it works.
I can suggest two options.
1: You can save an RDP connection (see picture below) and just run the new .rdp file from WshShell. After you create the .rdp file, you will have to log into it the first time, enter your credentials, and check the "Remember My Credentials" option.
2: You could use the SendKeys method. It is ugly but works. The downside is the password is left in the code, so you may want to look into encryption if you go this route. You may also need to tune a wait (sleep) for waiting for the popup to come up.
WScript.Sleep 5000 'Sleeps for 5 seconds
SendKeys “{TAB}”, 1 'Focus to the computer name
SendKeys "ServerName", 1
SendKeys "{TAB}", 1 'Focus to the user name
SendKeys "Password", 1
SendKeys "{ENTER}", 1 'Connect

Auto Closing Windows Credential Prompt for Outlook2016

Trying to use AutoHotkey to close this prompt as it comes up,
i'm closing the window based on Titles, that mean any other windows credential request will get auto closed also, is there any way that i can identify that this popup prompt is generated by Outlook2016 and only then the script can close the window?
OS: Windows 10
Script:
#NoEnv
#Persistent
SetTimer, ClosePopup, 250
return
ClosePopup:
WinClose, Windows Security
return
i also tried VBS, but 2 issue, i also cant make sure this prompt is from outlook and the second is that the script wont loop
Set wshShell = CreateObject("WScript.Shell")
Do
ret = wshShell.AppActivate("Windows Security")
If ret = True Then
wshShell.SendKeys "%{F4}" 'ALT F4
Exit Do
End If
WScript.Sleep 500
Loop
This should work:
WinClose Windows Security ahk_exe outlook.exe

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

Activate a running process with known process ID using VBScript

I have the process ID of the process for my application that is already running.
Now I want to activate that same window through this process id with VBScript.
Supposing mypid is the process ID
Set Shell = CreateObject("WScript.Shell")
Shell.AppActivate(mypid)
This is returning false on Windows 7.
If anyone has an answer, I would like to know it.
Check this link :
WshShell.AppActivate doesn't seem to work in simple vbs script
And this is the summary of good solution there:
Set WshShell = CreateObject("WScript.Shell")
for i=0 to 300 'this loop will continue about 30 sec if this not enough increase this number
Rtn=WshShell.AppActivate(myPID) 'have to be the windows title of application or its process ID
If Rtn = True Then
WshShell.SendKeys "......." ' send key you like
wscript.sleep 100 ' stop execute next line until finish close app
End If
wscript.sleep 100
Next

Resources