I need to create a script to map a Windows shared folder. My script looks like:
$User = "domain\user"
$PWord = ConvertTo-SecureString -String "password" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
New-PSDrive -Name M -PSProvider FileSystem -Root \\192.168.1.1\c$ -Credential $Credential -persist
It works fine if there is no problem while accessing the shared folder.
How can I improve my script so that:
- If the mapping works fine I get a confirmation
- If the mapping fails in a given amopunt of time (let's say, 30 seconds) I get an error and the script goes on
- If the mapping fails I get a return code to understand the reasons because it failed
Can anybody please help?
Regards
marius
I made a test based on what I read on page 332 of the "Mastering Powershell" pdf manual:
My script looks like:
New-PSDrive -Name N -PSProvider FileSystem -Root \\192.168.1.1\c$ -Credential $Credential -persist -ErrorVariable mioerrore -erroraction SilentlyContinue
$mioerrore[0].exception.message