forked from WillyMoselhy/ActiveDirectory-PowerShell-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplace-NTFSDomainACLs.ps1
215 lines (177 loc) · 8.35 KB
/
Replace-NTFSDomainACLs.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<#
.SYNOPSIS
This script replaces existing ACLs from a specific domain with entries from a new domain.
.DESCRIPTION
Typical use scenario: You are migrating from an old forest to a new one. Your file shares already have permissions that point
to users and groups in the old domain. Given that you arleady created new users in the new domain with the same SamAccountName,
you can use this script to go through each file and replace existing Access Rules from the old domain with idenitical ones that
point to the new domain.
The script requires cmdlets from the ActiveDirectory module.
For best performance, use local paths instead of network locations. However network URLs are supported.
.EXAMPLE
.\Replace-NTFSDomainACLs.ps1 -OldDomainName "OldDomain" -NewDomainName "NewDomain" -RootPath D:\FileShares\ExampleShare | Out-GridView
This will check the security permissions on each file under the root path, for each file where permissions are not inherited and
the Access Rule belong to objects in OldDomain, the script will try to find a match with the same username in NewDomain. If a
match is found, the script will add a new rule idenitical to the old one, and remove the OldDomain entry.
The domain names must be the same as the pre Windows 2000 format, DomainName\Username not [email protected]
The results will be displayed in a Grid View.
.EXAMPLE
.\Replace-NTFSDomainACLs.ps1 -OldDomainName "OldDomain" -NewDomainName "NewDomain" -RootPath D:\FileShares\ExampleShare -CSVLogPath C:\temp\example.csv
Same as the first example, additionally this will save the results in CSV file.
.LINK
https://github.com/WillyMoselhy/ActiveDirectory-PowerShell-Scripts/tree/master/Replace-NTFSDomainACLs
#>
#Requires -Modules ActiveDirectory
Param(
# Old domain name
[Parameter(Mandatory = $true)]
[string] $OldDomainName ,
# New domain name
[Parameter(Mandatory = $true)]
[string] $NewDomainName,
# Path of folder to replace permissions
[Parameter(Mandatory = $true)]
[ValidateScript( { Test-Path -Path $_ })]
[string] $RootPath,
# New domain controller FQDN. Specify this if the script is running from old domain.
[Parameter(Mandatory = $false)]
[ValidateScript( {Test-NetConnection -ComputerName $_ -InformationLevel Quiet})]
[string] $NewDCFQDN,
# List of exception objects in old domain to skip.
# This will not replace accounts for defined values.
# specify as pre Windows 2000 format "OldDomain\Name"
[string[]] $ExceptionList,
# List of object (users or groups) to replace
# This will only replace the accounts for defined values
# specify as per Windows 2000 format "OldDomain\Name"
[string[]] $IncludeList,
# Path to export results as CSV
[Parameter(Mandatory = $false)]
[string] $CSVLogPath
)
$ErrorActionPreference = "Stop"
$RootItem = [Array] (Get-Item -Path $RootPath)
$ChildItems = [Array] (Get-ChildItem -Path $RootPath -Recurse )
if ($ChildItems) { $AllItems = $RootItem + $ChildItems }
else { $AllItems = $RootItem }
if($NewDCFQDN){
#If new DC FQDN is specified, use it when getting the AD Object
$PSDefaultParameterValues = @{
"Get-ADObject:Server" = $NewDCFQDN
}
}
$Count = 0
$Result = foreach ($Item in $AllItems) {
#Update Progress bar
$Count++
$Progress = ($Count / $AllItems.Count) * 100
Write-Progress -Activity "Replacing security permissions of objects from $OldDomainName with $NewDomainName" `
-Status "$Count/$($AllItems.Count) - $($Item.FullName)" `
-PercentComplete $Progress
try {
# Handling for long paths
if($item.FullName.length -ge 255 -and $item.FullName -notlike "\\?\*"){
Write-Error -Exception @"
The file name is too long. Please use \\?\ or \\?\UNC\ prefix (for Windows 1607+) or use Mapped Drives to shorten the path.
For more info check this link: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation
"@
}
$ACL = $Item | Get-Acl
$OldDomainAccessRules = $Acl.Access | Where-Object { $_.IdentityReference.Value -like "$OldDomainName*" -and $_.IsInherited -eq $false}
#Remove accounts that are in the excpetion list
if($ExceptionList){
$OldDomainAccessRules = $OldDomainAccessRules | Where-Object {$_.IdentityReference.Value -notin $ExceptionList}
}
# Filter by accounts that are in the include list
if($IncludeList){
$OldDomainAccessRules = $OldDomainAccessRules | Where-Object {$_.IdentityReference.Value -in $IncludeList}
}
if ($OldDomainAccessRules) {
$ACLUpdates = 0
foreach ($OldAccessRule in $OldDomainAccessRules) {
$ObjectName = $OldAccessRule.IdentityReference -replace ".+\\(.+)", '$1'
$NewDomainADObject = Get-ADObject -Filter { SamAccountName -eq $ObjectName } -Properties CanonicalName
if ($NewDomainADObject) {
#User/group found in new forest
$ACLUpdates++
# Add new Acess rule for user in new forest
$NewAccessRule = New-Object system.security.AccessControl.FileSystemAccessRule("$NewDomainName\$ObjectName", `
$OldAccessRule.FileSystemRights, `
$OldAccessRule.InheritanceFlags, `
$OldAccessRule.PropagationFlags, `
$OldAccessRule.AccessControlType`
)
$ACL.AddAccessRule($NewAccessRule)
# Remove old access rule for user in old forest
$ACL.RemoveAccessRule($OldAccessRule) | Out-Null
#Return result
[PSCustomObject]@{
Type = "ACL Entry"
Path = $Item.FullName
OldForestObjectName = $ObjectName
FoundInNewForest = $true
ObjectType = $NewDomainADObject.ObjectClass
CanonicalName = $NewDomainADObject.CanonicalName
ACLUpdated = ""
ErrorMessage = ""
}
}
else {
#User / group not found in new forest
[PSCustomObject]@{
Type = "ACL Entry"
Path = $Item.FullName
OldForestObjectName = $ObjectName
FoundInNewForest = $false
ObjectType = ""
CanonicalName = ""
ACLUpdated = ""
ErrorMessage = ""
}
}
}
# Set the ACL
if ($ACLUpdates -gt 0) {
try {
Set-Acl -Path $Item.FullName -AclObject $ACL -ErrorAction Stop
$ACLUpdated = $true
$ErrorMessage = ""
}
Catch {
$ACLUpdated = $false
$ErrorMessage = $Error[0].Exception.Message
}
[PSCustomObject]@{
Type = "ACL Update"
Path = $Item.FullName
OldForestObjectName = ""
FoundInNewForest = ""
CanonicalName = ""
ObjectType = ""
ACLUpdated = $ACLUpdated
ErrorMessage = "$ErrorMessage"
}
}
}
}
catch {
$ErrorMessage = $Error[0].Exception.Message
[PSCustomObject]@{
Type = "Error"
Path = $Item.FullName
OldForestObjectName = ""
FoundInNewForest = ""
CanonicalName = ""
ObjectType = ""
ACLUpdated = ""
ErrorMessage = "$ErrorMessage"
}
}
}
if ($Result) {
if ($CSVLogPath) { $Result | Export-Csv -Path $CSVLogPath -NoTypeInformation -Force -ErrorAction Continue }
return $Result
}
else {
Write-Warning "No changes were applied."
}