Windows Tech Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Sunday, 30 October 2011

Error Handling: An Example

Posted on 09:17 by Unknown
Consider the following standard WMI query for a moment. Look at the code for a few seconds, at least...

[CODE]
strComputer = "computer123"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_PhysicalMemory",,48)
For Each objItem in colItems Wscript.Echo "Caption: " & objItem.Caption 
Next
[/CODE]

What could go wrong? What if "Computer123" doesn't exist?  What if "Computer123" is offline, or the firewall prevents a connection?  What if your user account doesn't have permissions to query remote WMI?  What if you typed in the wrong WMI class name, like "Win32_RAM" or something else?

You might get something like this...


[OUTPUT]
C:\Scripts\wmi-test.vbs(3, 1) Microsoft VBScript runtime error: _
The remote server machine does not exist or is unavailable: 'GetObject'
[/OUTPUT]
Is that a good way to handle this failure?  What if we add some explicit error checking and then handle the error?  Let's try this...


[CODE]
strComputer = "computer123" On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") If err.Number <> 0 Then
wscript.echo "unable to connect to: " & strComputer
wscript.echo "error: " & err.Number & " / " & err.Description
wscript.quit(err.Number)
End If
Set colItems = objWMIService.ExecQuery("Select * from Win32_PhysicalMemory",,48)
For Each objItem in colItems Wscript.Echo "Caption: " & objItem.Caption 
Next
[/CODE]


Now, if we run this and "Computer123" is not accessible, we should get the following...



[OUTPUT]
unable to connect to: computer123
error: 462 / The remote server machine does not exist or is unavailable

[/OUTPUT]


If we check %errorlevel% is should be set to 462 now as well.  This is an example of raising an error "implicitly" or simply passing it up without modifying it.  If we want to force our own error result value, we can simply modify the wscript.quit(value) statement to use our own numeric value...



[CODE]
strComputer = "computer123" On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") If err.Number <> 0 Then
wscript.echo "unable to connect to: " & strComputer
wscript.echo "error: " & err.Number & " / " & err.Description
wscript.quit(5)
End If
Set colItems = objWMIService.ExecQuery("Select * from Win32_PhysicalMemory",,48)
For Each objItem in colItems Wscript.Echo "Caption: " & objItem.Caption 
Next
[/CODE]


Now, the exit code (or %errorlevel%) value will be 5 when it fails for *ANY* reason.  There are situations when you will want to force a static error result, and situations where you want the real error value.  It's nice to know you have that option, and YOU have control over it.

Enjoy!

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in error monitoring, programming, scripting, vbscript | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Voting Time: Help Me Out?
    I need to get a better view of how I should manage this blog if I'm going to keep at it. I'd like to know how you typically discover...
  • A World Without Competition
    Try to imagine what things would be like today had there not been fierce competition in certain key parts of our world.  I’ll give you some ...
  • Book Update
    I posted some gibberish a few weeks ago about another book project.  Well, I'm getting close to wrapping it up, so I thought I'd go ...
  • Cost
    Software technology, like any technology, provides a means to solving problems.  Some big. Some small.  Some that help.  Some that hurt.  An...
  • Windows 7: Default User vs All Users
    A lot of confusion seems to occur with understanding the difference between the "Default User" profile, and the "All Users...
  • Time to Give Props
    With the ever-expanding volume and breadth of information on the Internet today, it's easy to focus on my own thoughts, experiences, ide...
  • Table of Contents (Preliminary)
    Here's the preliminary Table of Contents for my new book "The AutoCAD Network Administrator's Bible - 2013 Edition".  I...
  • The Nicest IT and IT Vendor Folks I Know
    I've ranted many times before how it's unfair to "hate" an entire company, without providing a rationale for it based on s...
  • Windows 8
    Two small, yet irritating things, that I hope Windows 8 addresses with respect to Windows 7: Being able to put the Recycle Bin in the S...
  • Stupid Assumptions
    After years of watching sci-fi TV shows, movies, etc. it's finally come to a point where even the so-called brightest of our authors and...

Categories

  • a
  • activation
  • active directory
  • advertising
  • agile
  • agility
  • amazon
  • american
  • apple
  • application virtualization
  • applications
  • art
  • articles
  • asp
  • augi
  • authors
  • autocad
  • AutoCAD Autodesk
  • autodesk
  • autolisp
  • automation
  • automotive
  • backups
  • batch
  • beer
  • beta
  • blackberry
  • blogs
  • bongloads
  • book
  • books
  • Books writing kindle amazon technology business projects
  • browsers
  • business
  • cad
  • career
  • certification
  • chrome
  • city government
  • civilization
  • cloud services
  • cmd
  • cmmi
  • comedy
  • command
  • community
  • computers
  • conferences
  • config manager
  • consultants
  • consulting
  • contracting
  • cranium drainium
  • crapware
  • culture
  • data center
  • data mining
  • databases
  • deployment
  • directx
  • DLL
  • domains
  • dumb
  • earth
  • economy
  • editor
  • education
  • election
  • elections
  • employment
  • engineering
  • entertainment
  • environment
  • error monitoring
  • events
  • exchange
  • facebook
  • family
  • firefox
  • flexnet
  • fud
  • fun
  • funny
  • games
  • gary vaynerchuk
  • gmail
  • google
  • government
  • group policy
  • hampton roads
  • health
  • history
  • holidays
  • home
  • html5
  • humor
  • hyper-v
  • iis
  • industry
  • infrastructure
  • installation
  • installshield
  • internet
  • internet explorer
  • interviews
  • jobs
  • jtbworld
  • kindle
  • kixtart
  • lab setup
  • languages
  • ldap
  • learning
  • legal
  • licensing
  • life
  • lifecycle
  • linux
  • lisp
  • logging
  • management
  • manufacturing
  • marketing
  • markets
  • mdop
  • mdt
  • medical
  • messaging
  • microsoft
  • microsoft access
  • military
  • mountains
  • movies
  • mozilla
  • music
  • nature
  • network administration
  • news
  • nook
  • nothing
  • office
  • open source
  • openoffice
  • opera
  • operating systems
  • oracle
  • osx
  • packaging
  • patches
  • people
  • photos
  • podcasts
  • policy
  • politics
  • powershell
  • predictions
  • process automation
  • products
  • programming
  • projects
  • psychology
  • publishing
  • rail
  • reading
  • registry
  • religion
  • reporting
  • reviews
  • rsat
  • rss
  • safari
  • safety
  • sales
  • satire
  • sccm
  • scheduling
  • science
  • scripting
  • search
  • security
  • servers
  • services
  • sharepoint
  • shopping
  • sms
  • social stuff
  • society
  • softgrid
  • software assurance
  • software deployment
  • software development
  • software packaging
  • sony
  • speaking
  • sports
  • sql express
  • sql server
  • statistics
  • Statistics news marketing
  • steve jobs
  • stories
  • stuff
  • stupidity
  • symantec
  • sysinternals
  • system center
  • systems architecture
  • t-sql
  • taxes
  • technet
  • technical support
  • technology
  • TED
  • ted talks
  • testing
  • textpad
  • thoughts
  • traffic
  • training
  • transportation
  • travel
  • troubleshooting
  • tutorials
  • twitter
  • ubuntu
  • unattend
  • unemployment
  • updates
  • upfront ezine
  • utilities
  • vacation
  • vba
  • vbscript
  • video
  • virginia
  • virginia beach
  • virtualization
  • visual lisp
  • vmware
  • vmware server
  • voting
  • war
  • weather
  • web
  • web browsers
  • web development
  • web sites
  • windows
  • windows 7
  • windows live
  • windows server
  • windows server 2012
  • windows8
  • winpe
  • wise
  • wmi
  • work
  • writing
  • ws08
  • wsus
  • wwa
  • x64
  • xml
  • ze frank

Blog Archive

  • ►  2013 (37)
    • ►  October (1)
    • ►  September (5)
    • ►  August (8)
    • ►  July (2)
    • ►  June (4)
    • ►  May (4)
    • ►  April (2)
    • ►  March (2)
    • ►  February (8)
    • ►  January (1)
  • ►  2012 (120)
    • ►  December (14)
    • ►  November (12)
    • ►  October (10)
    • ►  September (7)
    • ►  August (3)
    • ►  July (2)
    • ►  June (6)
    • ►  May (6)
    • ►  April (20)
    • ►  March (16)
    • ►  February (18)
    • ►  January (6)
  • ▼  2011 (343)
    • ►  December (15)
    • ►  November (23)
    • ▼  October (27)
      • Campaign Advertising Template
      • Error Handling: An Example
      • The Next Book Project
      • What is It?
      • Thoughts of Autonomous Robotic Combatants
      • One More Time
      • Infographic: Manual Installation vs. Packaged Inst...
      • So, You Wanna Be a Software Repackager?
      • If You Don't Know - SAY You Don't Know
      • A Short Recap of my Career Thus Far
      • Signs of IT Failure
      • AutoCAD: PURGE, AUDIT, RECOVER, Repeat...
      • Children's Guide to Politics and Elections
      • What's Next?
      • Software Development Tips
      • PSEXEC, Computer$ and SYSTEM Access
      • Winding Down, Weekend Brain Dump
      • What if AutoLISP were Unleashed?
      • Because it Was Overdue
      • Amazon Kindle Books: German and French available
      • Self-Assembly is the Future
      • IT Job Openings in Hampton Roads, Virginia
      • When Applications Take a Dump
      • Software Development's Biggest Mistakes
      • The Never-Ending War: Centralized IT vs Department...
      • What Can Fail: Software Deployments
      • Weekend
    • ►  September (35)
    • ►  August (29)
    • ►  July (17)
    • ►  June (23)
    • ►  May (20)
    • ►  April (38)
    • ►  March (61)
    • ►  February (54)
    • ►  January (1)
Powered by Blogger.

About Me

Unknown
View my complete profile