Reputation: 61
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
Upvotes: 6
Views: 11510
Reputation: 1
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.
Upvotes: 0
Reputation: 181
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.
Upvotes: 18
Reputation: 11
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.
Upvotes: 1
Reputation: 4081
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.
Upvotes: 1