Mapped network drive via New-PSDrive disappears after exiting powershell script

10.9k views Asked by At

I've tried looking for an answer for couple of days with no luck...

I have a powershell (v3.0) script that checks for a Network drive and maps it if it is not already mapped. The script itself works just fine to a certain point, drive is mapped and accessible during script execution but when the script ends, the mapped drive is disconnected. I am using the -Persist option which should preserve the drive mapping.

If I run the same commands from PowerShell prompt the drive is mapped and stays mapped even when exiting PowerShell prompt.

Sample code below

# Drive letter to map to
$destinationDriveLetter = "G"
# Username to map the network drive
$userName = "username"
# Password to use when mapping
$userPass = ConvertTo-SecureString "password" -AsPlainText -Force

#check if Drive is already mapped
if ( Test-Path -LiteralPath $destinationDriveLetter":" -ErrorVariable errVar ) {
    Write-Host "Drive already mapped.`r`n"
    Write-Host "Exiting script`r`n"
    exit
}

Write-Host "Destination Driveletter $destinationDriveLetter not mapped. Doing it...`r`n"

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $userPass
Write-Debug "Command: New-PSDrive -Persist -Name $destinationDriveLetter -PSProvider FileSystem -Root '\\server\share' -Credential $Credential -ErrorVariable errVar"

New-PSDrive -Persist -Name $destinationDriveLetter -PSProvider FileSystem -Root '\\server\share' -Credential $Credential -ErrorVariable errVar

if ( $errVar ) {
    Write-Error "`r`nError mapping destination drive $destinationDriveLetter. Check!`r`n"
}
else {
    Write-Host "Destination drive mapped successfully.`r`n"
    # test if drive truly mapped and destination folder exist
    Test-Path -LiteralPath $destinationDriveLetter":" -ErrorVariable errVar
    # debugging, roar!
    Write-Host "Drives during script:`r`n"
    Get-PSDrive
}
# dirty fix to actually confirm that the drive is mapped during script execution
pause

Clearly the issue here is in running the commands in a script but how to fix this? I need to get the drive mapped automatically after server restart. Using UNC paths won't work since the software I'm using doesnt understand them.

EDIT: Forgot to say that OS I'm running is Windows Server 2012

4

There are 4 answers

0
Sebastien Beny On

I had the same and in my case the issue was that I was calling the script from a powershell command window. If I executed the script directly with right-click on the script "Run with Powershell" the drives don't disappear after.

2
StephenP On

What user account is the script running under? The script has to be run as the user that will be using the drive. From Get-Help:

get-help new-psdrive -parameter Persist
<snipped>
NOTE: Mapped network drives are specific to a user account. Mapped network drives that you create in sessions that 
are started with the "Run as administrator" option or with the credential of another user are not visible in session 
that started without explicit credentials or with the credentials of the current user.
3
Fred On

I found that even with the -Persist parameter the drive mappings would disappear when run from a logon script.

The problem was resolved by adding the -Scope "Global" parameter as well.

0
Pierre-Luc St-Laurent On

The best way to keep a PSDrive alive for the duration of a script is to use -Persist and set the -Scope to "Script".

However, if you want the drive to persist even after the script exits, set the -Scope parameter to "Global".

New-PSDrive -Persist:$true -Name $letter -PSProvider FileSystem -Root '\\server\share' -Scope Global

I don't recommand to set a persistant drive as an admin, it should be created in the context of the user that it's intended for.