VMware Cloud Community
KRAEMS
Enthusiast
Enthusiast

Guest Script Manager not producing expected result in Guest VM.

I've been making frequent use of Christophe's Guest Script Manager package (https://communities.vmware.com/docs/DOC-25474) over the last few months.  All in all, I have seen tremendous benefit from it.  I do however, seem to fall victim to several 'false positives' where GSM reports a script has run to success, yet, the observed result in the Guest VM is anything but. 

The simplest example I can reproduce to date, is a batch script that creates 4 folders on 3 different drives that exist in a Windows Guest VM.  The drives are visible to windows ~ the only thing the script does, is a batch 'mkdir' for 3 folders in the root of the drive, and one subfolder. 

The script configuration has a 10 second timeout and a 10 second refresh, as these values *seemed* reasonable given the modest nature of the script.  There is no context interactivity, no working directory, and no file copy in the script configuration. 

Has anyone experienced something similar?  I've tried to script in batch 'sleep' periods *just in case* a 'race' was to blame, but, no change.  Any thoughts as to why such a simple script does not consistently demonstrate the expected result?  Failure to yield the expected result with such a modest script is discouraging from trying larger, more complex scripts.  Any feedback is appreciated! 

BIG P.S.  I just now tested some additional scenarios: 

Creating 4 folders on the root of C:\ in the Windows Guest VM - only the first 2 out of the 4 folders created on the Windows Guest. 

Jacked up the script timeout and refresh values to 90 seconds and 30 seconds (which I use successfully on longer running powershell scripts) - only the first 2 out of the 4 folders created on the Windows Guest. 

'REM'med out the first 2 folder creates - the second 2 folders ARE NOT created in the Windows Guest.  😮

(In each case, the GSM 'Run script in VM guest' workflow runs to completion with Workflow Designer indicating a successful run)

Ran the second two folder creates manually from Windows Guest VM command line - the folders create as expected. 

KRAEMS

vRO Appliance version: 7.1.0.19044 Build 4276164

Workflow Designer version: 7.1.0.4262825

Guest Script Manager version: 0.0.3a

Guest VM OS: Windows 2008 Standard R2 SP1

8 Replies
iiliev
VMware Employee
VMware Employee

Hi,

This is a bug in Guest Script Manager.

If you open the workflow 'Run Script in Guest' and the scriptable task labelled 'set for Bat', you'll see the following couple of lines of Javascript code:

script = script.replace("\r\n", " & ");

script = script.replace("\n", " & ");

It seems the intention has been to replace all occurrences of new line characters with ampersands; that is, to convert your multi-line batch commands to a single line. For example, the following 4 commands

mkdir c:\dir01

mkdir c:\dir02

mkdir c:\dir03

mkdir c:\dir04

to become

mkdir c:\dir01 & mkdir c:\dir02 & mkdir c:\dir03 & mkdir c:\dir04

The problem is that Javascript string's replace() method replaces only the first occurrence of the pattern, so instead of a single-line command you'll get

mkdir c:\dir01 & mkdir c:\dir02

mkdir c:\dir03

mkdir c:\dir04

When executing this, the first line will be executed correctly, but the other lines will be skipped.

The fix is easy - you just need to use a method that replaces all occurrences of the pattern. One possible way is to replace the 2 lines of scripting code with the following

script = script.replace(new RegExp("\r\n", 'g'), " & ");

script = script.replace(new RegExp("\n", 'g'), " & ");

which uses regular expressions to match all occurrences of the new lines pattern and replace them with ampersands.

0 Kudos
KRAEMS
Enthusiast
Enthusiast

As usual, Ilian, your input is most helpful - THANK YOU. 

In trying your proposed solution (which seems to work quite well) I'll mention a couple other observations.  I've not thoroughly debunked these, and since I seem to have a solution I don't need to pursue an explanation for this:

1) Using 'whitespace' carriage return + line feeds in the script content looks like it fails to interpret... doing so appears to result in consecutive ampersands which fail to be interpreted (would seem to make sense). 

2) The presence of 'REM'med out lines to serve as comments also had a negative impact on the outcome.  Script content of this:

mkdir c:\dir01

mkdir c:\dir02

mkdir c:\dir03

mkdir c:\dir04

Works fine and the folder are created, but this:

rem test line 1

rem test line 2

mkdir c:\dir01

mkdir c:\dir02

mkdir c:\dir03

mkdir c:\dir04

fails and the folders are NOT created, in my experience.  Thanks again! 

0 Kudos
iiliev
VMware Employee
VMware Employee

The last one isn't working because when these 6 commands get combined into a single-line script, the first word will be rem which will act as a comment keyword till the end of line, so effectively all commands will be commented out.

0 Kudos
qc4vmware
Virtuoso
Virtuoso

Good bug find.  In the past I was helping to support the Guest Script Manager but it seems that the service that was hosting the code has gone stagnant.  I might go ahead and setup a github in the mean time so we can continue to fix and expand the  functionality.  I recently added a "Run a script by name" workflow and have fixed a couple of other minor bugs I have run into.

One thing I do when trying to run more complex scripts is creating a resource for the script instead of putting it in the script to run.  Then in the run config specify that as a file to upload. So my script file can be huge and complex and my script to run in the configuration is a just a one liner like c:\temp\runthis.bat .  If it is more than a line or two you might want to try that approach.

Paul

0 Kudos
Burke-
VMware Employee
VMware Employee

Please note that this package has been relocated to the VMware Sample Exchange‌ due to the recent decommission of FlowGrab.com.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
qc4vmware
Virtuoso
Virtuoso

I must have missed this being moved although it looks like it was just done a week or so ago.  Thats awesome!  So I see you also created a git repository for it.  Can I assume this is where we will be handling collaboration from here on out?  If so I'll fork it and add my changes.

Paul

0 Kudos
Burke-
VMware Employee
VMware Employee

Yes, that does appear to be the case Smiley Happy ... and, I know that cdecanini_ has already done some work with accessing the Sample Exchange API...

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
qc4vmware
Virtuoso
Virtuoso

0 Kudos