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
I think you want to look at $error[0].FullyQualifiedErrorId. I think that
was designed to replace integer error codes without the "namespace"
collisions.
That field is usually pretty general. For my example I get this:
PS> $error[0].FullyQualifiedErrorId
ComMethodTargetInvocation
Yep, it's not perfect and not always used properly (just like many utilities
that return -1 for all errors).