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

4

5

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

Savill answered 10/6, 2014 at 8:14 Comment(1)
N
17

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.

Newly answered 31/3, 2015 at 22:29 Comment(3)
Thank you soo much. That really helped. I was working on this for a few hours before I came along this comment.Sabotage
Just another powershell oddity that is not documented. Was shoehorning .net REPL really worth it?Leifleifer
Thank you so much! Without these options even mapping a network drive inside a PowerShell function would get lost in the main script section. This is beyound any oddity.Gillum
U
1

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.
Unroll answered 11/6, 2014 at 3:38 Comment(2)
Script is running under the same user that needs to use the drive. I noticed another anomaly aswell... If I put the drive mapping into a separate function the drive mapping disappears as soon as the function ends and script returns to "main". Same with conditions, when using if-else comparison (if drive is mapped do nothing, else map it) the drive mapping is lost when script exits the condition. W00t? :)Savill
If this script is running in the context of the user, you should remove the $credential. This worked on 3 computers under 3 different accounts and the drive persists as expected.Unroll
S
1

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.

Sluiter answered 3/12, 2014 at 13:54 Comment(0)
B
0

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.

Bipropellant answered 3/8, 2022 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.