internal/functions/Update-NetworkDrive.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
function Update-NetworkDrive { <# .SYNOPSIS Remaps mapped network drives if needed. .DESCRIPTION Remaps mapped network drives if needed. Performs no operation, if no network drives are mapped on a GPO. Migration tables do not correctly update mapped drives, unfoortunately. Requires valid source data to be already imported, for example by running Import-GptDomainData or Import-GptIdentity. .PARAMETER GpoName Name of the GPO to update. .PARAMETER Domain The destination domain into which the GPO has been imported. .EXAMPLE PS C:\> Update-NetworkDrive -GpoName 'Share Y:' -Domain 'contoso.com' Updates the GPO "Share Y:" for the domain contoso.com, remapping the share from the source domain to the destination domain. #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string] $GpoName, [Parameter(Mandatory = $true)] [string] $Domain ) begin { try { $gpoObject = Get-GPO -Domain $Domain -Name $GpoName -ErrorAction Stop $destinationDomain = (Get-DomainData -Domain $Domain).ADObject $gpoADObject = Get-ADObject -Server $destinationDomain.PDCEmulator -Identity $gpoObject.Path -Properties gPCFileSysPath -ErrorAction Stop } catch { throw } if ($script:sourceDomainData) { $sourceDomainDNS = $script:sourceDomainData.DomainDNSName $sourceDomainNetBios = $script:sourceDomainData.NetBIOSName } elseif ($script:identityMapping.Count -gt 0) { $sourceDomainDNS = $script:identityMapping[0].DomainFqdn $sourceDomainNetBios = $script:identityMapping[0].DomainName } else { throw "Unable to determine source domain. Run Import-GptDomainData or Import-GptIdentity first!" } } process { Write-Verbose "$GpoName : Processing Network Shares" $driveXmlPath = Join-Path -Path $gpoADObject.gPCFileSysPath -ChildPath 'User\Preferences\Drives\Drives.xml' if (-not (Test-Path -Path $driveXmlPath)) { Write-Verbose "$GpoName : Does not contain Network Shares" return } try { $driveString = Get-Content -Path $driveXmlPath -Raw -ErrorAction Stop -Encoding UTF8 } catch { Write-Verbose "$GpoName : Could not access Network Shares file" return } $driveStringNew = $driveString.Replace("\\$sourceDomainDNS\", "\\$($destinationDomain.DNSRoot)\").Replace("\\$sourceDomainNetBios\", "\\$($destinationDomain.NetBIOSName)\") if ($driveStringNew -eq $driveString) { Write-Verbose "$GpoName : Nothing to remap in the defined shares" return } try { Set-Content -Value $driveStringNew -Path $driveXmlPath -Encoding UTF8 -ErrorAction Stop } catch { throw } } } |