Executable running in background - vbscript

I am trying to run a simple batch script via Jenkins (which in turn calls a VBscript). The script which i am executing in Jenkins is:
cd "C:\Product\workspace"
cscript Test.vbs
The test.vbs is simple code which calls an exe in console mode
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /K C:\Product\workspace\Product.exe -c -dir C:\ProductDir", 1
Set objShell = Nothing
The parameter 1 : Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position.
The problem which I am facing is I am not able to see the cmd.exe and the Product.exe installer. Though the process explorer shows cmd.exe and Product.exe running. I don't get why I its not running in foreground and only in background.
How can I get the exe to run in foreground?
When I tried running directly on VM, I can see it running in foreground. Cant understand this situation. Any light on this?
Thanks.

Are you running Jenkins slave agent as headless service on windows? I remember in this case the GUI would have problem. You should run the agent with jnlp when you add the slave VM. This works perfect with me.

Here's another way to skin the cat using VBScript.
I experienced the same issue trying schedule a task to launch Internet Explorer into the foreground. I was using WScript's Run method with the 3 window option to force it to be maximized. I just couldn't force it to come up in the foreground.
I FINALLY got that to work with WScript's AppActivate method. The trick was to monitor AppActivate's return value in a loop to ensure the application is fully launched with the correct TITLE before using AppActivate to bring it to the foreground.
AppActivate Method
Here's my example script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "iexplore.exe https://www.google.com", 3, false
WScript.Sleep 2000
While WshShell.AppActivate("Internet Explorer") = FALSE
WScript.Sleep 1000
Wend
WshShell.AppActivate "Internet Explorer"
WScript.Quit
=========================
Note: AppActivate will choose the closest match for the application TITLE (or process ID, which is not as simple). You don't have to have the complete TITLE. I'm showing "Internet Explorer" here, but I was able to use the TITLE of the web site that I was redirecting to ("Google" would work OK in this example). So, if you don't want to pull up any random instance of an application you may already have open, be as specific as possible. A CMD.EXE TITLE wouldn't be your best bet.
AppActivate works especially well for CMD/COMMAND windows, as (mentioned previously) you can use the TITLE batch file command to specify a unique window title.

I think im late but I did this and it worked:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /K C:\Product\workspace\Product.exe -c -dir C:\ProductDir", 0, False Rem 0: run in background, False: exit without waiting process to stop, True to wait for process
Set objShell = Nothing

You can use the .visible property in order to display the applications that are running and bring them to the foreground e.g. objShell.Visible = True
Example below of how I used it when launching an application:
Dim objQtpApp
Set objQtpApp = CreateObject("QuickTest.Application")
'make QTP visible
objQtpApp.Visible = True

Related

vbscript open folder in same explorer window

I am not so good in VBScript at all, but thanks to Google I was able to put together script which is able to open file path in explorer.exe
I would like to open the specific path in same window not in the new one. Is VBScript able to do it?
Here is my code:
Dim SH, FolderToOpen
Set SH = WScript.CreateObject("WScript.Shell")
FolderToOpen = "C:\path\to\my\folder"
SH.Run FolderToOpen
Set SH = Nothing
Thank you for your advice.
Try this:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Target
Here is a hackish approach using SendKeys that will work if the open instance of explorer.exe has the focus:
Set WshShell = WScript.CreateObject("WScript.Shell")
target = "C:/programs"
WshShell.SendKeys "%d"
WshShell.SendKeys target
WshShell.SendKeys "{ENTER}"
This will work if you e.g. have the above code (with the intended target) in a script in one folder. Click on the script icon and it will send you to the target folder.
[On Edit] An explanation of how it works: If you are using Windows Explorer and type Alt+d (which is what SendKeys "%d" simulates) then the focus is shifted to the address bar. For years I have been using this trick to open a command prompt in the current folder (Alt - d then type cmd then press Enter and the prompt opens with the open folder as the working directory). When I saw this question I wondered if essentially the same trick (but automated with VBScript) would work for navigation purposes and was pleasantly surprised when it worked as intended the very first time. Alt-d is a useful keyboard shortcut to keep in mind.

Unable to run VBScript on Windows Kiosk computer using iexplore.exe as Shell

I have a computer set up as a kiosk running iexplore.exe as the shell and the below VBScript is set to run at login for the kiosk user. The script is supposed to open Internet Explorer if it is closed. The script works fine with explorer.exe set as the shell, but not iexplore.exe. Can anyone tell me why?
ie=1
While ie=1
Set WshShell = WScript.CreateObject("WScript.Shell")
ie = WshShell.Run("iexplore.exe http://www.marion.k12.fl.us/", 3, true)
Do Until ie = WshShell.Run("iexplore.exe http://www.marion.k12.fl.us/", 3, True)
Wscript.Sleep 1000
Loop
Wend
First rule if .Run (or .Exec) fails (with or without a "File not found" error message): Use full pathes!. That is especially important, if the user is not the 'normal one' or the script is run 'early' (before the settings you rely on are actually active).
P.S. .Run(,, True) waits for the process and returns its exit code. So I can't see any rhyme or reason in Do Until ie = WshShell.Run().
I found that you cannot run a script with IE set as the shell. To run a script it requires that explore.exe to be set as the shell.
https://social.technet.microsoft.com/Forums/scriptcenter/en-US/0966e5e5-2d16-4d5c-a720-409dacf0acc1/cant-run-vbscript-code-without-explorerexe-process-running?forum=ITCG

VBS - how to run two browser one is full screen and another is minimised?

I have a kiosk where i need to run Chrome.exe as --kiosk and another Chrome.exe minimised. Using regedit run method VBS is executed.
Problem is on reboot it is messed up always the minimised chrome.exe does not start as minimised, always its shows on top of --kiosk mode.
How can i be 100% sure then when VBS is executed one chrome is running minimised and another is --kiosk mode?
Dim url
Dim shell
-- Part 1 - "has to run as --kiosk, always on screen never ever exit from the display screen"
url = "www.stackoverflow.com"
Set shell = CreateObject("Shell.Application")
shell.ShellExecute "chrome.exe", url, "", "", 1
-- Part 2 - "has to run as minimised when PC starts and VBS is executed, it should never ever pretend to show on screen."
url = "www.icanhazip.com"
Set shell = CreateObject("Shell.Application")
shell.ShellExecute "chrome.exe", url, "", "", 1
This script is not working for me, can anyone please help me?
Don't use Shell.Application, use the Run method of WScript.Shell. Here I assume that chrome.exe is either on the path or in the current working directory:
Option Explicit
With CreateObject("WScript.Shell")
Call .Run("chrome.exe www.icanhazip.com", 6, false)
Call .Run("chrome.exe --kiosk www.stackoverflow.com", 1, false)
End With
The second parameter is the window style.
Documentation Here

Running chrome in full screen using vb script

Hi I have problem with a vb script i created to run chrome in full screen.
The script simulates the F11 key press when chrome starts.
The script works when i run it but when I set it to run on startup on Windows XP chrome does not go full screen.
This is the script i have.
Option Explicit
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "chrome"
Wscript.Sleep 1000
objShell.SendKeys "{F11}"
WScript.Quit
Try launching Chrome with the command line switch "--full-screen".
It specifies if the browser should start in fullscreen mode, like if the user had pressed F11 right after startup. However, it seems it is ignored if the value of the "On Startup" setting is "Continue where you left off" (it works for the other two values).

Kill explorer.exe with windows title

I'm new with programing and my question is now, how i can close some specific explorer.exe windows. My Problem is, i have a program that call some windows:
Option Explicit
Dim shell, expl1, expl2, expl3, Terminate
Dim uprgExplorer
set shell = WScript.CreateObject("WScript.Shell")
set expl1 = shell.exec("C:\WINDOWS\explorer.exe c:\Documents and Settings")
set expl2 = shell.exec("C:\WINDOWS\explorer.exe C:\WINDOWS\system32\CCM\Cache")
set expl3 = shell.exec("C:\WINDOWS\explorer.exe c:\SCRIPTS\LOG")
Now i will kill only this 3 windows NOT the explorer.exe.
Can some one help me?
Greetings,
matthias
You could use the SendKeys function to close the Explorer windows:
set shell = WScript.CreateObject("WScript.Shell")
set expl1 = shell.exec("C:\WINDOWS\explorer.exe c:\tmp")
MsgBox "Explorer started."
success = shell.appactivate("c:\tmp")
if success then shell.sendkeys "%{F4}"
You might also want to have a look at AutoHotkey, which allows you to record macros and manipulate windows.

Resources