1

Our servers team has implemented a DFS, but users across the company still have drives mapped using the server name(s) at various sites. I'd like to push out a PS script that updates a SINGLE registry value (per drive).

My goal is to look through each drive letter key, if the key exists and the remote path starts with the server name then replace it with, the DFS name \\domain.com\SITE\+remainder of the path. This way users keep the same drive letters without having to "remap" their drives

Using Denver office as an example...

$OldServer = "\\denvernas01\"
$NewServer = "\\domain.com\DEN\"

$DriveLetterArray = "A","B","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
    foreach ($DriveLetter in $DriveLetterArray)
        { $Drives = Get-ItemPropertyValue HKCU:\Network\$DriveLetter -Name RemotePath -ErrorAction SilentlyContinue 
          $RemainingPath = $Drives.Replace($OldServer,"")
    foreach ($Drive in $Drives)
        { if ($Drive -like "*$OldServer*")
        { Set-ItemProperty HKCU:\Network\$DriveLetter -Name RemotePath -Value "$NewServer"+"$RemainingPath" }}}

EDIT ^^^This currently works, but only if the server name in RemotePath is all lower case. I.e. the server variables are case sensitive. Any thoughts on how to define the $OldServer & $NewServer variables so it will work with case variations???? e.g. Denvernas01, DENVERNAS01 (or anything inbetween)

I've come across a few threads discussing New-PSDrive, Get-WMIObject, etc, but I'd really like to just replace this one registry value. This would be a good "patch" that would take some stress off of our desktop support team. Trust me - I'll be advocating for GPO to push out common mapped drives once this is all over.

Any feedback is greatly appreciated. Thank you!

aceventura
  • 21
  • 3
  • 4
    Seems group policy would be the better tool for the job. You can set up a GPO to delete all drive maps (to remove even those manually mapped) and then GPOs to map new drives as appropriate. The way you're trying to approach it would require you to check each users hku\ntuser.dat hive, including mounting those that aren't logged in and unmounting when you're done. – Doug Maurer Mar 01 '21 at 23:20
  • @DougMaurer Thanks for your input. We are going to implement GPO policies to map common departmental drives, but many users have manually mapped random directories within these drives. Many have macros referencing these locations for example, so this work will need to be done either by our help desk, or automatically. Our endpoint management tool can easily call the primary user's hive to make changes and run at the appropriate time whether the user is connected to the domain or remote. – aceventura Mar 02 '21 at 16:18

1 Answers1

1

If anyone out there is interested, this is what I ended up with, and it worked like a charm. Thought I'd share...

$OldServer = '\\denvernas01'
$NewServer = '\\Domain.com\DEN'

$DriveLetterArray = "A","B","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
foreach ($DriveLetter in $DriveLetterArray){
    $DrivePath = $null; $ConvertedPath = $null
    $DrivePath = Get-ItemPropertyValue -Path "HKCU:\Network\$DriveLetter" -Name "RemotePath" -ErrorAction SilentlyContinue
    if ($DrivePath -eq $null) {continue}
    #Replace Old Drive Path
    if ($DrivePath -like "*${OldServer}*") {
        $ConvertedPath = $DrivePath -ireplace [regex]::Escape("$Oldserver"), $NewServer
        $ConvertedPath
        Set-ItemProperty -Path "HKCU:\Network\$DriveLetter" -Name "RemotePath" -Value "${ConvertedPath}"
    } else { 
    #Write-Host "no match"
    continue
    }
    #Write-Host ""
}
#Remove previous mountpoints 
Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\* | Where-Object Name -Match "##denvernas" | Remove-Item
aceventura
  • 21
  • 3