2

I have written a simple VBScript code which:

  • opens an Excel workbook
  • makes the Excel workbook visible
  • update the contents of Cell(1,1) of Sheet1 with the current system time
  • saves the Excel workbook

The code is as follows:

Option Explicit
Dim objExcel, objBook, objSheet, strPath
strPath = "C:\Users\a614923\Work Repository\Local\Test Complete\Working\05012019\bonn-6.11.9_Regression\FIL_REGRESSION\Stores\Files\ExternalDriver\Test.xlsx"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objBook = objExcel.Workbooks.Open(strPath)
Set objSheet = objBook.Sheets("Sheet1")
objSheet.Cells(1,1) = "Time: " & time
objBook.Save
'objBook.Close
'objExcel.Quit

When I run this VBScript file(directly by double-clicking, not via Jenkins), all the 4 steps mentioned above run fine. When I run the same file via Jenkins, the following happens:

  • Excel workbook is opened
  • the contents of Cell(1,1) of Sheet1 are updated with the current system time
  • Excel workbook is saved

The issue is that the Excel workbook does not become visible. I have been stuck on this step for hours now. Today is the first time I am using Jenkins. Is there any Jenkins configuration that I am missing? Has anyone ever faced this issue?

I am running Windows batch command on Jenkins to achieve the same:

C:\Windows\SysWOW64\cscript.exe "C:\Users\a614923\Work Repository\Local\Test Complete\Working\05012019\bonn-6.11.9_Regression\FIL_REGRESSION\Stores\Files\ExternalDriver\TEST.vbs"

This is how my build console looks like after running the build:

Started by user Gurmanjot Singh
Building in workspace C:\Program Files (x86)\Jenkins\workspace\SuiteRunner
[SuiteRunner] $ cmd /c call C:\WINDOWS\TEMP\jenkins2976471610072523635.bat

C:\Program Files (x86)\Jenkins\workspace\SuiteRunner>C:\Windows\SysWOW64\cscript.exe "C:\Users\a614923\Work Repository\Local\Test Complete\Working\05012019\bonn-6.11.9_Regression\FIL_REGRESSION\Stores\Files\ExternalDriver\TEST.vbs" 
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Program Files (x86)\Jenkins\workspace\SuiteRunner>exit 0 
Finished: SUCCESS

Any help/suggestion would be appreciated.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
  • 4
    Jenkins is running its jobs in the background, just like the Windows Task Scheduler does (when you configure a task to run as a different user). Even if you make Excel visible, it becomes visible in a different context (i.e. not to the interactive user). – Ansgar Wiechers Jan 16 '19 at 15:18
  • 2
    You must comply with one of the 9 rules here - https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setforegroundwindow – catcat Jan 16 '19 at 19:57
  • Thanks @AnsgarWiechers and @catcat for your suggestions. I was finally able to solve the issue. I was earlier running Jenkins as a Windows service which opens applications in non-interactive mode. When I launched jenkins via command prompt using the command `java -jar jenkins.war`, I was able to launch the excel and it was visible too. Many thanks again for pointing me in the right direction. [Reference](https://stackoverflow.com/a/9668165/5331061) – Gurmanjot Singh Jan 17 '19 at 06:42
  • @Gurman you could have probably still done this using the Windows Service if you had checked "Allow service to interact with the desktop" on the "Login" tab in the properties dialog box for the service (would need to run as LocalSystem to use this option). – user692942 Jan 17 '19 at 08:27
  • @Lankymart Allowing privileged services to interact with the desktop is a *terrible* idea that allows for all kinds of mayhem. Microsoft actively [discourages this](https://support.microsoft.com/en-us/help/327618/security-services-and-the-interactive-desktop-in-windows). Jenkins with its zoo (in the worst sense of the word) of plugins probably is particularly vulnerable. – Ansgar Wiechers Jan 17 '19 at 08:46
  • @Lankymart I had tried that method. But it did not work for me. May be because I had not restarted the Jenkins service after changing the setting. I will give it another try, though. But, as mentioned by Ansgar, Microsoft discourages the services to interact with the desktop, I think I will be going with the current solution which I have. – Gurmanjot Singh Jan 17 '19 at 08:58
  • @AnsgarWiechers depends on the service and tbh that advice is aimed at Windows NT 4.0 and Windows 2000 so we are talking 10 - 20 years ago. Never had a problem using a privileged service in a controlled environment personally, but it's a moot point as the OP has a workaround anyway. – user692942 Jan 17 '19 at 09:07

1 Answers1

1

I took help from this answer To solve the issue, I performed these steps:

  1. Downloaded the generic war package from https://jenkins.io/download/

enter image description here

  1. Ran the jenkins from command line using the command java -jar jenkins.war

enter image description here

  1. Complete the Installation(configure proxy, if any; download plugins, credentials etc) enter image description here

  2. Entered the batch command enter image description here

  3. After clicking on Build now, I was able to see the excel getting launched. enter image description here

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43