![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| | |||||||
| | Vista - run %comspec% AND how do you handel errors? |
| |
| 08-05-2009 | #1 |
| | run %comspec% AND how do you handel errors? 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 -------> |
| My System Specs |
| 08-05-2009 | #2 |
| | Re: run %comspec% AND how do you handel errors? You're not doing anything with your oShell.Run statement. Check out the Run page: http://msdn.microsoft.com/en-us/libr...ky(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. "OldDog" <mikef2691@xxxxxx> wrote in message news:38465f95-665f-4280-a62d-0045bae788c1@xxxxxx Quote: > 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 -------> |
| My System Specs |
| 08-05-2009 | #3 |
| | Re: run %comspec% AND how do you handel errors? "OldDog" <mikef2691@xxxxxx> wrote in message news:38465f95-665f-4280-a62d-0045bae788c1@xxxxxx Quote: > 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 -------> ======== 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 -- |
| My System Specs |
| 08-05-2009 | #4 |
| | Re: run %comspec% AND how do you handel errors? On Aug 5, 12:27*pm, "Richard Mueller [MVP]" <rlmueller- nos...@xxxxxx> wrote: Quote: > "OldDog" <mikef2...@xxxxxx> wrote in message > > news:38465f95-665f-4280-a62d-0045bae788c1@xxxxxx > > > > > Quote: > > Hi, Quote: > > I have a script that uses "net view" to see if the C: drive is sharred > > on a remote computer. Quote: > > Works OK most of the time, but I sometimes get a false reading if Net > > View hits an error. > > Usualy error 5, Access denied. Quote: > > I know this be cause if I run the Net View command from the Cmd Prompt > > it writes the error out to the screen. Quote: > > C:\>net view \\MyOtherPc /all > > System error 5 has occurred. Quote: > > Access is denied. Quote: > > However I can't catch the error in vbScript. *Is there a way to > > capture the error and pass it to my variables? Quote: > > '<--- Script (Function in an HTA )-----> Quote: > > Function View(strComputer) > > * *On Error Resume Next > > * *Const OpenAsDefault = -2 > > * *Const FailIfNotExist = *0 > > * *Const ForReading = *1 Quote: > > * *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 Quote: > > '<----- End Script -------> > 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 > --- Hide quoted text - > > - Show quoted text - 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 "%comspec% /c net View \\" & strComputer & " /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. |
| My System Specs |
![]() |
| Thread Tools | |
| |
| Similar Threads for: run %comspec% AND how do you handel errors? | ||||
| Thread | Forum | |||
| Stop Errors - Troubleshooting Specific Stop Errors | Tutorials | |||
| How to handel RPC failure in Posh? | PowerShell | |||
| Errors - please help | Vista mail | |||
| Errors, errors and more errors - I have Issues! | Vista performance & maintenance | |||
| explorer errors, program errors, no task manager | Vista performance & maintenance | |||