Equivelent of "On Error Resume Next" for DOS batch...

trippleO7
trippleO7 used Ask the Experts™
on
In VB Scripts you can supress and resume errors by using "On Error Resume Next".  How can you do that in a DOS batch file?

I have a script that shuts down virtual servers in VMware for backup purposes, but if the virtual server is already shutdown, I get an error, and it won't move on to the next server....Here's my script:

-------
call vmware-cmd "C:\Virtual Machines\SERVER1\Windows Server 2003 Standard Edition.vmx" stop

call vmware-cmd "C:\Virtual Machines\SERVER2\Windows Server 2003 Standard Edition.vmx" stop
-------


Is it possible to resume on errors in DOS?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
Hmm - you can reference the error by using

if %errorlevel%==0
which indicates 'no' error...

what's in vmware-cmd?

Author

Commented:
Here's the syntax of "vmware-cmd" if that's what you mean...

C:\VMware\VMware Server>vmware-cmd
Usage: C:\VMware\VMware Server\vmware-cmd <options> <vm-cfg-path> <vm-action> <arguments>
       C:\VMware\VMware Server\vmware-cmd -s <options> <server-action> <arguments>

  Options:
    Connection Options:
       -H <host>       specifies an alternative host (if set, -U and -P must also be set)
       -O <port>       specifies an alternative port
       -U <username>   specifies a user
       -P <password>   specifies a password
    General Options:
       -h More detailed help.
       -q Quiet. Minimal output
       -v Verbose.

  Server Operations:
    C:\VMware\VMware Server\vmware-cmd -l
    C:\VMware\VMware Server\vmware-cmd -s register <config_file_path>
    C:\VMware\VMware Server\vmware-cmd -s unregister <config_file_path>
    C:\VMware\VMware Server\vmware-cmd -s getresource <variable>
    C:\VMware\VMware Server\vmware-cmd -s setresource <variable> <value>

  VM Operations:
    C:\VMware\VMware Server\vmware-cmd <cfg> getconnectedusers
    C:\VMware\VMware Server\vmware-cmd <cfg> getstate
    C:\VMware\VMware Server\vmware-cmd <cfg> start <powerop_mode>
    C:\VMware\VMware Server\vmware-cmd <cfg> stop <powerop_mode>
    C:\VMware\VMware Server\vmware-cmd <cfg> reset <powerop_mode>
    C:\VMware\VMware Server\vmware-cmd <cfg> suspend <powerop_mode>
    C:\VMware\VMware Server\vmware-cmd <cfg> setconfig <variable> <value>
    C:\VMware\VMware Server\vmware-cmd <cfg> getconfig <variable>
    C:\VMware\VMware Server\vmware-cmd <cfg> setguestinfo <variable> <value>
    C:\VMware\VMware Server\vmware-cmd <cfg> getguestinfo <variable>
    C:\VMware\VMware Server\vmware-cmd <cfg> getid
    C:\VMware\VMware Server\vmware-cmd <cfg> getpid
    C:\VMware\VMware Server\vmware-cmd <cfg> getproductinfo <prodinfo>
    C:\VMware\VMware Server\vmware-cmd <cfg> connectdevice <device_name>
    C:\VMware\VMware Server\vmware-cmd <cfg> disconnectdevice <device_name>
    C:\VMware\VMware Server\vmware-cmd <cfg> getconfigfile
    C:\VMware\VMware Server\vmware-cmd <cfg> getheartbeat
    C:\VMware\VMware Server\vmware-cmd <cfg> getuptime
    C:\VMware\VMware Server\vmware-cmd <cfg> getremoteconnections
    C:\VMware\VMware Server\vmware-cmd <cfg> gettoolslastactive
    C:\VMware\VMware Server\vmware-cmd <cfg> getresource <variable>
    C:\VMware\VMware Server\vmware-cmd <cfg> setresource <variable> <value>
    C:\VMware\VMware Server\vmware-cmd <cfg> setrunasuser <username> <password>
    C:\VMware\VMware Server\vmware-cmd <cfg> getrunasuser
    C:\VMware\VMware Server\vmware-cmd <cfg> getcapabilities
    C:\VMware\VMware Server\vmware-cmd <cfg> addredo <disk_device_name>
    C:\VMware\VMware Server\vmware-cmd <cfg> commit <disk_device_name> <level> <freeze> <wait>
    C:\VMware\VMware Server\vmware-cmd <cfg> answer

C:\VMware\VMware Server>






The Error I am getting when I issue my batch file is this:


C:\VMware\VMware Server>call vmware-cmd "C:\Virtual Machines\Windows Server 2003
 Std Shell\Windows Server 2003 Standard Edition.vmx" stop
VMControl error -8: Invalid operation for virtual machine's current state: The requested operation ("stop") could not be completed because it conflicted with the state of the virtual machine ("off") at the time the request was received.  This error often occurs because the state of the virtual machine changed before it received the request.
VMControl error -8: Invalid operation for virtual machine's current state: Make sure the VMware Server Tools are running

Press any key to continue . . .





From what I can tell, it's running the command, but it see's that the Virtual Guest Machine is already off, so it's presenting me with this error, and won't continue on to the next server.

I guess if there's an easy way to convert this batch file to a VB script, I'm all for that too.  I was just going on what little knowledge of scripting I know :D
Top Expert 2007

Commented:
You could 'ping' it first...

ping Vmware-PC1
if %errorlevel%==0 goto shutdown
echo Unable to contact virtual PC
goto :eof

:shutdown
call vmware-cmd "C:\Virtual Machines\SERVER1\Windows Server 2003 Standard Edition.vmx" stop


But yes, we can probably convert this to vbs if the above doesn't work..
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
Running this just seemed to close out....

@echo


ping SERVER1_HOSTNAME
if %errorlevel%==0 goto shutdown
echo Unable to contact virtual PC
goto :eof

:shutdown
call vmware-cmd "C:\Virtual Machines\SERVER1\Windows Server 2003 Standard Edition.vmx" stop

call vmware-cmd "C:\Virtual Machines\SERVER2\Windows Server 2003 Standard Edition.vmx" stop

pause

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
As a side note, you do not need to use "call" when running external programs; you only need to use call when calling other batch files.
Other than that, the problem seems to be that vmware-cmd is waiting for you to press a key when it comes back with an error ("Press any key to continue . . .")
Try to add the "-q" to the options, it might get rid of it:

vmware-cmd -q "C:\Virtual Machines\SERVER1\Windows Server 2003 Standard Edition.vmx" stop
vmware-cmd -q "C:\Virtual Machines\SERVER2\Windows Server 2003 Standard Edition.vmx" stop

Or try to simulate the key:
echo | vmware-cmd "C:\Virtual Machines\SERVER1\Windows Server 2003 Standard Edition.vmx" stop
echo | vmware-cmd -q "C:\Virtual Machines\SERVER2\Windows Server 2003 Standard Edition.vmx" stop
Top Expert 2007
Commented:
@echo ?

Remove that line...not needed.

I'm not familiar with this vmware-cmd (don't seem to find it on my system).
Did it respond to a ping?

Adjust it this way...

for %%a in (SERVER1 SERVER2) do call :process %%a
goto :eof

:process
ping %1
if not %errorlevel%==0 (
  echo Unable to contact virtual PC
  goto :eof
)

echo. About to shutdown %1
pause
call vmware-cmd "C:\Virtual Machines\%1\Windows Server 2003 Standard Edition.vmx" stop

Author

Commented:
@ oBdA

The vmware-cmd is actually a .bat file included with the VMWare Server 1.0.1 install which triggers a perl script (I think)...sorry, should have mentioned that initially.  So I will need the "call" in my custom batch file.  I also tried the "-q" option without luck.  Correct me if I'm wrong (I'm wrong a lot), but the "Press any key to continue..." may be coming from the "pause" at the end of my file.

@SirBounty
"vmware-cmd" and "vmware-cmd.bat" was located in my C:\VMWare\VMware Server directory.  I'm running VMware Server 1.0.1, not Workstation or ESX versions if that helps.  That last adjustment seemed to work great.

Let me compile the final script and I will post it, and anymore questions I have on it.

Author

Commented:
Okay, I tested it with every variable I could think of, and it seems to work great.  I'll be using them in conjunction with Backup Exec, so here are my Pre/Post commands to shutdown my Virtual Guests so I can back them up.

Here's my pre-command:

vmware_shutdown.bat
------------------------------
for %%a in (clay-shell clay-altiris) do call :process %%a
goto :eof

:process
ping %1
if not %errorlevel%==0 (
  echo Unable to contact virtual PC
  goto :eof
)

echo. About to shutdown %1

call vmware-cmd "C:\Virtual Machines\Windows Server 2003 Std Shell\Windows Server 2003 Standard Edition.vmx" stop

call vmware-cmd "C:\Virtual Machines\CLAY-Altiris\Windows Server 2003 Standard Edition.vmx" stop




And once the job completes, it will run this post command to get the virtual servers back up and running....

vmware_startup.bat
------------------------------
call vmware-cmd "C:\Virtual Machines\Windows Server 2003 Std Shell\Windows Server 2003 Standard Edition.vmx" start

call vmware-cmd "C:\Virtual Machines\CLAY-Altiris\Windows Server 2003 Standard Edition.vmx" start

Author

Commented:
Thanks again!!!
Top Expert 2007

Commented:
Happy to help - thanx for the grade! :^ )

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial