Powershell mapped network drive doesn't persist

1

I'm trying to create a script that maps a network drive whenever I connect to a VPN, then disconnects the drive when I disconnect from the VPN, using Task Scheduler to launch the script when the event is created.

It launches the script, which creates the drive, but when Powershell closes, it disconnects the drive, so it only stays open for a few seconds, then closes it again. I have the persist parameter specified, but it doesn't persist.

New-PSDrive -Name "N" -PSProvider FileSystem -Root \(Serverpath)\ndrive -Persist

Davidw

Posted 2013-10-20T02:52:02.783

Reputation: 543

Answers

1

How about using batch? It's quite simple.

Net Use DriveLetter UNCPath [Optional: /User:Domain\Username] [Optional: Password] [Optional: /Persistent:Yes\No]
Example:
Net Use T: \\Fileserver\Share /user:my-domain\myuser Pa$$w0rd /Persistent:Yes

After the VPN disconnects you can remove the net drive:

Net Use T: /Delete /Y

The "/Y" skips the confirmation.

EliadTech

Posted 2013-10-20T02:52:02.783

Reputation: 2 076

1Could you share the answer here so others can benefit? – Chris Trombley – 2014-09-15T17:31:05.433

On the actions tab of a scheduled task, in the parameter box, place this line: -Command "& New-PSDrive -Name N -Root \ (serverpath) \ndrive -PSProvider FileSystem -Persist" – Davidw – 2014-09-27T01:28:02.923

I was able to get it to work with net use, but prefer Powershell, and with the help of someone on Facebook, was able to get it to use Powershell. – Davidw – 2013-10-20T19:08:26.873