Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

promtforcredentials & catch errors

$
0
0

Hi guys,

I'am on the road of learning powershell and created a first (almost) usefull script. Now I'm running in to a brick wall with error handling.

I created a script for my users to map networkdrives from another domain.

Here is what i've got so far.

Clear-host

$cred = $host.ui.PromptForCredential("IFN Drive Mapper", " Connect to IFN networkshares.
 Please enter your IFN\username and password
 
                     eg. IFN\kaplo", "", "NetBiosUserName")
$username = $cred.username
$name = $cred.GetNetworkCredential().username
$password = $cred.GetNetworkCredential().password
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("G:", "\\IFN-fileserver\share$", $false,$username,$password)
$net.MapNetworkDrive("I:", "\\IFN-fileserver\share2$", $false,$username,$password)
$wshell = New-Object -ComObject Wscript.Shell
# checking input
        if ($cred.username.toUpper().startswith(‘IFN\‘) -and $cred.password -ne ('$null')) {
            $clear = $true
            }
       Else {
            $wshell = New-Object -ComObject Wscript.Shell
            $wshell.Popup("Please provide a valid domain\username and password",2,"IFN Drive Mapper")
            }
          
remove-variable cred

function Exists-Drive { 
    param($driveletter) 
 
    (New-Object System.IO.DriveInfo($driveletter)).DriveType -ne 'NoRootDirectory'   

# checking drives   
$d1 = Exists-Drive 'G:' 
$d2 = Exists-Drive 'I:' 

if ($d1 -eq -not $true) {
       $wshell.Popup("Something went wrong, your G: share is not mapped",2,"IFN Drive Mapper")
       }
else {
       $wshell.Popup("Your NFI drive G: is mapped",2,"IFN Drive Mapper")
       }

if ($d2 -eq -not $true) {
       $wshell.Popup("Something went wrong, your I: share is not mapped",2,"IFN Drive Mapper")
       }
else {
       $wshell.Popup("Your IFN drive I: is mapped",2,"IFN Drive Mapper")
       }
      
# exit if user selected cancel/escape 
if (!$cred) { 
    $wshell.Popup("Valid credentials must be supplied to continue. Exiting the program. You can re-run this by starting IFN Drive Mapper",3,"IFN Drive Mapper")   
    #exit 

Now i realy want to catch the error " Exception calling "MapNetworkDrive" with "5" argument(s): "The specified network password is not correct". and put this in a info pop-up box.

Any ideas?


Viewing all articles
Browse latest Browse all 6937

Trending Articles