I need to build in logging and error handling into my script.
I am running a bunch of command line (wshell.run whatever.exe) in the
script and for each one I want a success/failure logged to a text file
or event log. I have no idea how to do this.

RE: Error handling and event loggin for command line within script by ESP

ESP
Tue Nov 08 16:10:22 CST 2005

This is a simple example of what you're asking. Error handling can be
extremely involved based on what you're trying to trap. So, this code is to
get ya started with the basics. This does not teach you that no errors means
"0". There are success outputs (or errors for a lack of terms) that some
apps/objects/etc... can throw out. So, you need to trap outputs in order to
know what is a true error and what is not ;-)

ESP

'---begin---
Const ForAppending = 8
Const OverwriteExisting = True

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("MyErrors.log",8,True)

WshShell.Run "whatever.exe"
If Err.Number <> 0 Then
objFile1.WriteLine Err.Number
Err.Number = Nothing
Else
End If
objFile1.Close
'---end---




"xr7" wrote:

> I need to build in logging and error handling into my script.
> I am running a bunch of command line (wshell.run whatever.exe) in the
> script and for each one I want a success/failure logged to a text file
> or event log. I have no idea how to do this.
>
>

Re: Error handling and event loggin for command line within script by xr7

xr7
Tue Nov 08 16:15:32 CST 2005

So I must look to the particular exe for an error/result code? I
assume there should be some documentation that would describe result
codes that this particular exe generates? Or am I not thinking
correctly?


Re: Error handling and event loggin for command line within script by ESP

ESP
Wed Nov 09 13:20:36 CST 2005

Kind of. Most programmers/tech writers won't take the time to document the
output/errors codes for apps. It's too time consuming and time is $$. You can
sometimes find them in a forum somewhere from google, but it's also just as
easy to trap them, then WScript.Echo them back to yourself.

For example, if I remember correctly, RoboCopy.exe from the resource kit
outputs a 17 if it succeeds, so Err.Number <> 0 would tell you there's an
error even though it's fine. I had to specifically trap 13 to know the copy
process failed. How did I know 13 was the right number ??? I removed all
rights to the server share that I was copying files to in order to force it
to fail. When it did (access denied) I knew I was on the right track. There
were other errors I trapped as well from RoboCopy, but this is the short
example version ;-)

So, let's say you know for sure that an app outputs "0", would Err.Number >
0 work for that app ?? Well maybe.... Sometimes you will see errors in the
negative as well (-24535670) or whatever. Error trapping can be tedious. Any
scripter can tell you that for every line of code, there's 10 lines of error
checking.

Stick with it, you'll be fine :-)

ESP





"xr7" wrote:

> So I must look to the particular exe for an error/result code? I
> assume there should be some documentation that would describe result
> codes that this particular exe generates? Or am I not thinking
> correctly?
>
>

Re: Error handling and event loggin for command line within script by xr7

xr7
Thu Nov 10 09:31:30 CST 2005

It seems to me that this err.numnber only returns a "VBscript" result.
No matter if my exe completes successfully or not, it always returns a
error of "0" as long as the actual command is launched, regardless
whether the actual work done by the exe is finished or succesful.
What I need to trap is the error code generated by the exe, how can I
do this?


Re: Error handling and event loggin for command line within script by ESP

ESP
Thu Nov 10 13:32:22 CST 2005

You more or less just did. Not all apps have error output codes written into
them. Are you seeing something along the lines where vbs reports a 0, but the
app fails or doesn't complete in some way ?? What app is this anyway ?

ESP

"xr7" wrote:

> It seems to me that this err.numnber only returns a "VBscript" result.
> No matter if my exe completes successfully or not, it always returns a
> error of "0" as long as the actual command is launched, regardless
> whether the actual work done by the exe is finished or succesful.
> What I need to trap is the error code generated by the exe, how can I
> do this?
>
>

Re: Error handling and event loggin for command line within script by xr7

xr7
Thu Nov 10 15:03:14 CST 2005

Yea it reports 0 even when I do things that cause the process to
terminate, such as making the file the exe runs against read only, or
denying permissions to everyone on the file.
The app is VMWare-vdiskmanager.exe , a utility to manage virtual hard
disk files for VMware virtual machines.
I have a script that iterates through virtual disks and performs 3
different commands on each one, I want to have a text log generated but
that is pretty tough when all I get is a 0 result.


Re: Error handling and event loggin for command line within script by xr7

xr7
Thu Nov 10 15:03:18 CST 2005

I think the 0 is vbscript's error code saying "successfully initiated
the command" It logs it as soon as the exe is launched.
What I need is the exe's termination result. IE. failed because file
is in use, or permission denied, or completed successfully.
The app is vmware-vdiskmanager.exe, a utility to work with virtual
disks for vmware virtual machines.
Here is the code for the script that works. Please don't make fun of
my sloppy code, it's my 2nd script ever :P
varVMpath = "C:\Virtual Machine\"
varMountPoint = "V:"

'Create collection of Virtual Machines by reading subfolder names below
the varVMpath specified above
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(varVMpath)
Set colSubfolders = objFolder.Subfolders

'Begin For Each loop
For Each objSubfolder in colSubfolders

'Build path to Virtual disks
strVMname = varVMpath & objSubfolder.Name & "\" & objSubfolder.Name
strVolC = strVMname & ".vmdk"
strVolE = strVMname & "e.vmdk"
strVolF = strVMname & "f.vmdk"

'Defragment C
Set objVMDK = CreateObject("Scripting.FileSystemObject")
If objVMDK.FileExists(strVolC) Then
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware
Workstation\vmware-vdiskmanager.exe"" -d """ & strVolC & """",,True
End If
Set objWShell = nothing
Set objVMDK = nothing

'Defragment E
Set objVMDK = CreateObject("Scripting.FileSystemObject")
If objVMDK.FileExists(strVolE) Then
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware
Workstation\vmware-vdiskmanager.exe"" -d """ & strVolE & """",, True
End If
Set objWShell = nothing
Set objVMDK = nothing

'Defragment F
Set objVMDK = CreateObject("Scripting.FileSystemObject")
If objVMDK.FileExists(strVolF) Then
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware
Workstation\vmware-vdiskmanager.exe"" -d """ & strVolF & """",, True
End If
Set objWShell = nothing
Set objVMDK = nothing

'Mount C
Set objVMDK = CreateObject("Scripting.FileSystemObject")
If objVMDK.FileExists(strVolC) Then
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware DiskMount
Utility\vmware-mount.exe""" & " " & varMountPoint & " """ & strVolC &
"""",,True

'Preshrink C
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMWare
Workstation\vmware-vdiskmanager.exe"" -p " & " " & varMountPoint,,True

'Dismount C
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware DiskMount
Utility\vmware-mount.exe""" & " " & varMountPoint & " " & "/d",,True
End if
Set objWShell = nothing
Set objVMDK = nothing

'Mount E
Set objVMDK = CreateObject("Scripting.FileSystemObject")
If objVMDK.FileExists(strVolC) Then
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware DiskMount
Utility\vmware-mount.exe""" & " " & varMountPoint & " """ & strVolE &
"""",,True

'Preshrink E
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMWare
Workstation\vmware-vdiskmanager.exe"" -p " & " " & varMountPoint,,True

'Dismount E
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware DiskMount
Utility\vmware-mount.exe""" & " " & varMountPoint & " " & "/d",,True
End if
Set objWShell = nothing
Set objVMDK = nothing

'Mount F
Set objVMDK = CreateObject("Scripting.FileSystemObject")
If objVMDK.FileExists(strVolC) Then
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware DiskMount
Utility\vmware-mount.exe""" & " " & varMountPoint & " """ & strVolF &
"""",,True

'Preshrink F
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMWare
Workstation\vmware-vdiskmanager.exe"" -p " & " " & varMountPoint,,True

'Dismount F
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware DiskMount
Utility\vmware-mount.exe""" & " " & varMountPoint & " " & "/d",,True
End if
Set objWShell = nothing
Set objVMDK = nothing

'Shrink C
Set objVMDK = CreateObject("Scripting.FileSystemObject")
If objVMDK.FileExists(strVolC) Then
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware
Workstation\vmware-vdiskmanager.exe"" -k """ & strVolC & """",,True
End If
Set objWShell = nothing
Set objVMDK = nothing

'Srhink E
Set objVMDK = CreateObject("Scripting.FileSystemObject")
If objVMDK.FileExists(strVolE) Then
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware
Workstation\vmware-vdiskmanager.exe"" -k """ & strVolE & """",, True
End If
Set objWShell = nothing
Set objVMDK = nothing

'Shrink F
Set objVMDK = CreateObject("Scripting.FileSystemObject")
If objVMDK.FileExists(strVolF) Then
Set objWShell = CreateObject("WScript.Shell")
objWshell.Run """C:\Program Files\VMware\VMware
Workstation\vmware-vdiskmanager.exe"" -k """ & strVolF & """",, True
End If
Set objWShell = nothing
Set objVMDK = nothing

'Return to the beginning of For Each loop
Next

wscript.echo "Shrinking complete"


Re: Error handling and event loggin for command line within script by ESP

ESP
Thu Nov 10 15:45:10 CST 2005

Naaa, the code looks great. Bottom line: You cant trap an error if an app
isn't coded to return an error code. There is nothing you can do that I know
of. Error codes are not specific to VBS in this sense. VBS is the wrapper,
and it watches the app to see if anything shoots out of it by way of an
error. If you're getting a 0, then the app is outputting a 0 no matter what.
It's simply the way some apps are written.

ESP




"xr7" wrote:

> I think the 0 is vbscript's error code saying "successfully initiated
> the command" It logs it as soon as the exe is launched.
> What I need is the exe's termination result. IE. failed because file
> is in use, or permission denied, or completed successfully.
> The app is vmware-vdiskmanager.exe, a utility to work with virtual
> disks for vmware virtual machines.
> Here is the code for the script that works. Please don't make fun of
> my sloppy code, it's my 2nd script ever :P
> varVMpath = "C:\Virtual Machine\"
> varMountPoint = "V:"
>
> 'Create collection of Virtual Machines by reading subfolder names below
> the varVMpath specified above
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.GetFolder(varVMpath)
> Set colSubfolders = objFolder.Subfolders
>
> 'Begin For Each loop
> For Each objSubfolder in colSubfolders
>
> 'Build path to Virtual disks
> strVMname = varVMpath & objSubfolder.Name & "\" & objSubfolder.Name
> strVolC = strVMname & ".vmdk"
> strVolE = strVMname & "e.vmdk"
> strVolF = strVMname & "f.vmdk"
>
> 'Defragment C
> Set objVMDK = CreateObject("Scripting.FileSystemObject")
> If objVMDK.FileExists(strVolC) Then
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware
> Workstation\vmware-vdiskmanager.exe"" -d """ & strVolC & """",,True
> End If
> Set objWShell = nothing
> Set objVMDK = nothing
>
> 'Defragment E
> Set objVMDK = CreateObject("Scripting.FileSystemObject")
> If objVMDK.FileExists(strVolE) Then
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware
> Workstation\vmware-vdiskmanager.exe"" -d """ & strVolE & """",, True
> End If
> Set objWShell = nothing
> Set objVMDK = nothing
>
> 'Defragment F
> Set objVMDK = CreateObject("Scripting.FileSystemObject")
> If objVMDK.FileExists(strVolF) Then
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware
> Workstation\vmware-vdiskmanager.exe"" -d """ & strVolF & """",, True
> End If
> Set objWShell = nothing
> Set objVMDK = nothing
>
> 'Mount C
> Set objVMDK = CreateObject("Scripting.FileSystemObject")
> If objVMDK.FileExists(strVolC) Then
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware DiskMount
> Utility\vmware-mount.exe""" & " " & varMountPoint & " """ & strVolC &
> """",,True
>
> 'Preshrink C
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMWare
> Workstation\vmware-vdiskmanager.exe"" -p " & " " & varMountPoint,,True
>
> 'Dismount C
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware DiskMount
> Utility\vmware-mount.exe""" & " " & varMountPoint & " " & "/d",,True
> End if
> Set objWShell = nothing
> Set objVMDK = nothing
>
> 'Mount E
> Set objVMDK = CreateObject("Scripting.FileSystemObject")
> If objVMDK.FileExists(strVolC) Then
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware DiskMount
> Utility\vmware-mount.exe""" & " " & varMountPoint & " """ & strVolE &
> """",,True
>
> 'Preshrink E
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMWare
> Workstation\vmware-vdiskmanager.exe"" -p " & " " & varMountPoint,,True
>
> 'Dismount E
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware DiskMount
> Utility\vmware-mount.exe""" & " " & varMountPoint & " " & "/d",,True
> End if
> Set objWShell = nothing
> Set objVMDK = nothing
>
> 'Mount F
> Set objVMDK = CreateObject("Scripting.FileSystemObject")
> If objVMDK.FileExists(strVolC) Then
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware DiskMount
> Utility\vmware-mount.exe""" & " " & varMountPoint & " """ & strVolF &
> """",,True
>
> 'Preshrink F
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMWare
> Workstation\vmware-vdiskmanager.exe"" -p " & " " & varMountPoint,,True
>
> 'Dismount F
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware DiskMount
> Utility\vmware-mount.exe""" & " " & varMountPoint & " " & "/d",,True
> End if
> Set objWShell = nothing
> Set objVMDK = nothing
>
> 'Shrink C
> Set objVMDK = CreateObject("Scripting.FileSystemObject")
> If objVMDK.FileExists(strVolC) Then
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware
> Workstation\vmware-vdiskmanager.exe"" -k """ & strVolC & """",,True
> End If
> Set objWShell = nothing
> Set objVMDK = nothing
>
> 'Srhink E
> Set objVMDK = CreateObject("Scripting.FileSystemObject")
> If objVMDK.FileExists(strVolE) Then
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware
> Workstation\vmware-vdiskmanager.exe"" -k """ & strVolE & """",, True
> End If
> Set objWShell = nothing
> Set objVMDK = nothing
>
> 'Shrink F
> Set objVMDK = CreateObject("Scripting.FileSystemObject")
> If objVMDK.FileExists(strVolF) Then
> Set objWShell = CreateObject("WScript.Shell")
> objWshell.Run """C:\Program Files\VMware\VMware
> Workstation\vmware-vdiskmanager.exe"" -k """ & strVolF & """",, True
> End If
> Set objWShell = nothing
> Set objVMDK = nothing
>
> 'Return to the beginning of For Each loop
> Next
>
> wscript.echo "Shrinking complete"
>
>

Re: Error handling and event loggin for command line within script by Michael

Michael
Thu Nov 10 18:50:37 CST 2005

> What I need to trap is the error code generated by the exe, how can I
> do this?


intExitCode = WshShell.Run("c:\somepath\some.exe", 0, True)

Check the docs for the bWaitOnReturn argument of the Run method

Download details: Windows Script 5.6 Documentation
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en

bWaitOnReturn

Optional. Boolean value indicating whether the script should wait for the
program to finish executing before continuing to the next statement in your
script. If set to true, script execution halts until the program finishes,
and Run returns any error code returned by the program. If set to false (the
default), the Run method returns immediately after starting the program,
automatically returning 0 (not to be interpreted as an error code).

--
Michael Harris
Microsoft MVP Scripting





Re: Error handling and event loggin for command line within script by xr7

xr7
Wed Nov 16 11:21:29 CST 2005

Interesting. Michael's method returns a 1 when i cause the exe to
fail, however ESP, your method always returns 0. What is the reason
for this? I also find it weird that vbscript requires parentheses when
you do "value = objWshell.run(command)", but if you do a "wshell.run
(command)" it says "cannot use parentheses when calling a sub"... what
is up with that?


Re: Error handling and event loggin for command line within script by ESP

ESP
Wed Nov 16 16:40:13 CST 2005

I'll have to go with Michael on this one. After playing with it, I got too
close to the project and complete forgot about bWaitOnReturn. We were getting
a 0 because we weren't having the script wait long enough for a return ;-)

ESP



"xr7" wrote:

> Interesting. Michael's method returns a 1 when i cause the exe to
> fail, however ESP, your method always returns 0. What is the reason
> for this? I also find it weird that vbscript requires parentheses when
> you do "value = objWshell.run(command)", but if you do a "wshell.run
> (command)" it says "cannot use parentheses when calling a sub"... what
> is up with that?
>
>