Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBScript vs. PowerShell error handling

206 views
Skip to first unread message

Matt

unread,
Jul 2, 2007, 10:17:54 AM7/2/07
to
I'm still a little confused by error handling in PowerShell. In
VBScript I would identify the likely error conditions and then script
around them. For example, I map a drive and check errors. If the error
is that there is already a drive mapped, then I unmap the remembered
connection and try again. Drive mapping is just an example, but I'd
like to know how to deal with this in general.

The idea here is that I uniquely identify the scenario which causes my
script to fail and then I can repeatedly fix that error if it ever
crops up again.

#######################################################
#######################################################
set oNet = CreateObject("wscript.network")
err.Clear
oNet.MapNetworkDrive argDrive, argUNC
select case err.number
case 0 'Drive mapped successfully
wscript.echo argDrive & " mapped to " & argUNC
case -2147023570 'unknown username or bad password
wscript.echo "Cannot map " & argDrive & vbcrlf & vbtab & "Unknown
username or bad password"
case -2147024843 'network path not found - can be from a perms issue
wscript.echo "Cannot map " & argDrive & " to " & argUNC & vbcrlf &
vbtab & "Network path not found or access denied"
case -2147024829 'Network name cannot be found
wscript.echo "Cannot map " & argDrive & " to " & argUNC & vbcrlf &
vbtab & "The network name cannot be found"
case -2147024811 'local devicename is in use
'Disconnect the drive that is currently used
'Try mapping drive again
case -2147023677 'multiple connections to the same resource with
different usernames and passwords
wscript.echo "Multiple Connections Error" & vbcrlf & vbtab &
err.number & vbcrlf & vbtab & err.Description
return 2
case -2147023694 'Connecting to a drive that was remembered in
Windows Explorer
'Disconnect the drive that is currently used
'Try mapping drive again
case else
wscript.echo "Cannot map " & argDrive & vbcrlf & vbtab & "Unhandled
Error Number " & errNumber & vbcrlf & vbtab & err.Description
end select
#######################################################
#######################################################

In Powershell, I get the $error object but the only way I see to
identify a specific error condition is by using text matching on the
exception property:

PS> $onet = new-object -com wscript.network
PS> $onet.mapnetworkdrive("z:", "\\ny-dc-a\netlogon")
Exception calling "MapNetworkDrive" with "2" argument(s): "The local
device name has a remembered connection to another
network resource.
"
At line:1 char:20
+ $onet.mapnetworkdrive( <<<< "z:", "\\ny-dc-a\netlogon")
PS> $error[0].exception
Exception calling "MapNetworkDrive" with "2" argument(s): "The local
device name has a remembered connection to another
network resource.
"

So, do I have to do something like this:
if ($error[0].exception -like "*remembered connection*") { #fix it }

This does not seem as robust as the vbscript method (one error number
indicates one specific error condition)...or maybe I'm not
understanding .NET error handling as well as I should be.

Thanks for the help,
Matt

John Vottero

unread,
Jul 2, 2007, 12:12:19 PM7/2/07
to
"Matt" <mgr...@gia.edu> wrote in message
news:1183385874.2...@m37g2000prh.googlegroups.com...

> I'm still a little confused by error handling in PowerShell. In
> VBScript I would identify the likely error conditions and then script
> around them. For example, I map a drive and check errors. If the error
> is that there is already a drive mapped, then I unmap the remembered
> connection and try again. Drive mapping is just an example, but I'd
> like to know how to deal with this in general.
>
> The idea here is that I uniquely identify the scenario which causes my
> script to fail and then I can repeatedly fix that error if it ever
> crops up again.
>
[snip]

>
> So, do I have to do something like this:
> if ($error[0].exception -like "*remembered connection*") { #fix it }
>
> This does not seem as robust as the vbscript method (one error number
> indicates one specific error condition)...or maybe I'm not
> understanding .NET error handling as well as I should be.
>

I think you want to look at $error[0].FullyQualifiedErrorId. I think that
was designed to replace integer error codes without the "namespace"
collisions.

Matt

unread,
Jul 2, 2007, 12:28:44 PM7/2/07
to
On Jul 2, 12:12 pm, "John Vottero" <JVott...@mvpsi.com> wrote:
> "Matt" <mgro...@gia.edu> wrote in message

That field is usually pretty general. For my example I get this:
PS> $error[0].FullyQualifiedErrorId
ComMethodTargetInvocation

John Vottero

unread,
Jul 2, 2007, 1:26:08 PM7/2/07
to
"Matt" <mgr...@gia.edu> wrote in message
news:1183393724.9...@i38g2000prf.googlegroups.com...

Yep, it's not perfect and not always used properly (just like many utilities
that return -1 for all errors).

0 new messages