vbscript >> run %comspec% AND how do you handel errors?

by OldDog » Wed, 05 Aug 2009 23:18:27 GMT

Hi,

I have a script that uses "net view" to see if the C: drive is sharred
on a remote computer.

Works OK most of the time, but I sometimes get a false reading if Net
View hits an error.
Usualy error 5, Access denied.

I know this be cause if I run the Net View command from the Cmd Prompt
it writes the error out to the screen.

C:\>net view \\MyOtherPc /all
System error 5 has occurred.

Access is denied.


However I can't catch the error in vbScript. Is there a way to
capture the error and pass it to my variables?


'<--- Script (Function in an HTA )----->

Function View(strComputer)
On Error Resume Next
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"
oShell.Run "%comspec% /c net View \\" & strComputer & " /ALL " &
">" & sTempFile, 0 , True
numerr = Err.number
abouterr = Err.description
statval.value = numerr & " " & abouterr
If numerr <> 0 Then
statval.value = numerr & " " & abouterr
Err.Clear
Exit Function
Else
Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)
View = CBool(InStr(sResults, "C$"))
End If
End Function

'<----- End Script ------->


vbscript >> run %comspec% AND how do you handel errors?

by Eric » Wed, 05 Aug 2009 23:40:17 GMT


You're not doing anything with your oShell.Run statement.
Check out the Run page:
http://msdn.microsoft.com/en-us/library/d5fk67ky (VS.85).aspx
The method returns an integer.
The return value should tell you if it failed.
Checking for return values is not required. You're throwing it away.

If you want to call a batch script file instead, the file needs to have the
line in it:
If %ErrorLevel% Neq 0 Exit %ErrorLevel%
after each statement you want to check for failure.








vbscript >> run %comspec% AND how do you handel errors?

by Richard Mueller [MVP] » Thu, 06 Aug 2009 01:27:17 GMT





The Run method can return a return code. For example:
========
Option Explicit
Dim objShell, strCmd, intReturn

Set objShell = CreateObject("Wscript.Shell")
strCmd = "%comspec% /c net view \\MyComputer /all > c:\Scripts\report.txt"
intReturn = objShell.Run(strCmd, 2, True)
Wscript.Echo "Return Code: " & CStr(intReturn)
======
However, while the error message at a command prompt matches yours, the
return code displayed for me is 2. In any case, it does return a non-zero
value. If there is no error, the return code is 0.

Another method is to use the Exec method of the wshShell object (if WSH is
version 5.6). You then have access to the standard output using the StdOut
object, but also any errors with the StdErr object. For example:
=========
Option Explicit

Dim objShell, strCmd, objExecObject, strResults

Set objShell = CreateObject("Wscript.Shell")
strCmd = "%comspec% /c net view \\MyComputer /all"

Set objExecObject = objShell.Exec(strCmd)

Do Until objExecObject.StdOut.AtEndOfStream
strResults = objExecObject.StdOut.ReadAll
Loop

Do Until objExecObject.StdErr.AtEndOfStream
strResults = objExecObject.StdErr.ReadAll
Loop

Wscript.Echo strResults
========
In this case you can use the FSO object to write the output if it comes from
StdOut, or do something else if the output comes from StdErr. I hope this
helps.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--




run %comspec% AND how do you handel errors?

by OldDog » Thu, 06 Aug 2009 04:33:47 GMT

n Aug 5, 12:27m, "Richard Mueller [MVP]" <rlmueller-
XXXX@XXXXX.COM > wrote:
> > onst ForReading > gt; >>
> > et oShell = CreateObject("WScript.Sh>l>")
> > et oFSO = CreateObject("Scripting.FileSystemOb>e>t")
> > Temp = oShell.ExpandEnvironmentStrings("%>E>P%")
> > TempFile = sTemp & "\runres>l>.tmp"
> > Shell.Run "%comspec% /c net View \\" & strComputer & ">/>LL>" &
> > ">" & sTempFile,>0>, True
> > umerr = E>r>number
> > abouterr = Err.de>c>iption
> > statval.value = numerr & " " &>a>outerr
> > >f numerr >>>0 Then
> > tatval.value = numerr & " " &>a>outerr
> > gt;r>.Clear
> > xi> >unction
> > Set fFile = oFSO.OpenTextFile(sTempFile, >o>Reading, _
> > > >ailIfNotExist, OpenAsDefault)> > > fFile.Clo>e> > > oFSO.DeleteFile(sTempFile)
> >>gt;View = CB>o>(InStr(sResults> ">$>))
> > <d If
> > End >un>ti>n
>
> > '<----- End Script ------->
>
> The Run meth>d can retur> a return code. Fo> example:
> ========
> Option Ex>li>it
> Dim objShell, strCmd, intReturn
>
> Set>objShell = CreateObject("Wscript.Shell")
> strCmd > "%comspec% /c net view \>MyComputer /all > c:\Scripts\report.txt"
> >ntReturn = objShell.Run(strCmd, 2, True)
> Wscri>t.Echo "R>turn Code: " & CStr(intReturn)
> ======
> However, while the error messa>e at a command prompt matches yours, the
> return code displayed for me is >. In any case, it does return a non-zero
> value. If>th>re is no error, the return code is 0.
>
> Another method is to use the Exec>method of the wshShell object (if WSH is
> version 5.6). You then have acces> to the standard output using the StdOut
> object, but also any er>ors with the>StdErr object. For>ex>mple:
> =========
> Option Explicit
>
> Dim ob>Sh>ll, strCmd, objExecObject, strResults
>
> Set>objShell = CreateObject("Wscript.Shell")
> strCmd = >%c>mspec% /c net view \\MyComputer /all"
>
> >et>objExecObject = objShell.Exec(strCmd)
>
> Do>Until objExecObject.StdOut.AtEndOfStream
> > strRes>lt> = objExecObject.StdOut.ReadAll
> Loop
>
> >o Until objExecObject.StdErr.AtEndOfStream
> > strR>su>ts = objExecObject.StdErr.>eadAll
> L>op
>
> Wscript.Echo strResults
> ========
> In this case you can use the FS> object to write the output if it comes from
> StdOut, or do something else>if the ou>pu> come> from StdErr. I ho>e this
> helps.
>
> --> > Richard Mueller
> MVP Directory Serv>ces
> Hilltop Lab -http:>/w>w.rlmueller.net
> --- Hide quoted text -
>
> - Show quoted text -

Thanks for your input.

I was able to trap the error by using DOS redirection.
Channel 2 is standard error and 1 is standard out.

If you want both standard out and standard error to the same file,
like I do, then

oShell.Run "%comspe>% /c net View \\" & >trComputer & " /ALL " & ">">&
sTempFile & " 2>&1", 0 , True

Note the 2>&1 that tells both standard error (2) and standard out (1)
to go to the sTempFile.

Then you can look in that file for your error codes.

For instance 5 means permission denied.


Similar Threads

1. objWSHShell.Run ("%comspec% /c____slower then____.bat

Good afternoon,

Do you have some idea please?

I test that if I run program.exe using two ways one of them is very
slow.

     using vbscript and code objWSHShell.Run ("%comspec% /c program.exe

     is much more slower then

     using batch file and code program.exe

Why?

     differencies are vbscript take 10 minutes and batch file take only
2 minutes

     Do I have somethnig wrong. Thank you for your time.

Bye

2. Can't open form run time error 0 and run time error 429 can't create object