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

Mapping a Windows shared folder

$
0
0

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

IP address 192.168.1.1 does not exist in my network.
When I execute the command PowerShell hangs and I get no message.
What can I do in order do detect the causes of the problem (in my case, no host available) and let the script go onj?
 
Regards
marius

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles