Good morning,
Sorry to be a bother but I’ve run into a brick wall.
Quest: Create a login script that will search for a specified mapped server share, remove that share and remap to a new server with the same file path…
Problem: If I run these commands on their own, it works exactly as expected. When it’s scripted, all of the sudden my new drive will never actually stick even though my verbose output looks fine. Pulling my hair out in handfuls…
Code:
function Switch-Drive
{
[CmdletBinding()]
[OutputType([int])]
Param
(
# Old server name (netbios)
[Parameter(Mandatory = $true)]
[String]
$old,
# New server name (netbios)
[Parameter(Mandatory = $true)]
[String]
$new
)
Clear-Host
#Locate first available drive letter
function Get-DriveLetter
{
[char[]]"DEFGJKLMNOPQRTUVWXY" |`
?{ !(Get-PSDrive $_ -ea 'SilentlyContinue') } |`
select -f 1
}
$newletter = Get-DriveLetter
Write-Host "Found drive letter: $newletter"
Write-Host "Old drive designated as: $old"
Write-Host "New drive designated as: $new"
Start-Sleep -Seconds 1
Write-Host "Searching for old drive"
$search = Get-WmiObject Win32_mappedlogicaldisk
If (($search.ProviderName) -like "*$old*")
{
$fullpath = $search | Where-Object { $_.ProviderName -like "*$old*" } | Select-Object -ExpandProperty ProviderName
$fullname = $fullpath.ToString()
Write-Host "Found $fullname is mapped on $ENV:Computername"
}
Else
{
Write-Host "Unable to find $old"
Exit 99
}
$search | Where-Object { $_.ProviderName -like "*$($old)*" } | ForEach-Object{
$name = ($_.ProviderName)
$share = $name.TrimStart("\\" + $old)
Write-Host "Found share path $share"
$newpath = ("\\" + "$new" + "$share")
Write-Host "Creating $newpath"
Get-PSDrive | where {$_.DisplayRoot -eq $fullname} | Remove-PSDrive -Force -Verbose
Start-Sleep -Seconds 5 #added during troubleshooting
New-PSDrive -Name $newletter -PSProvider FileSystem -Root $newpath -Persist -Verbose
}
}
Dyin over here,
Paul ![]()