Below is a PowerShell script that meets your requirements. This script includes functions to manage SIDHistory (getting from the domain, saving to a file, and loading from a file), and it iterates over a specified Windows share's filesystem, updating ACLs by replacing old SIDs with new ones based on the SIDHistory, and periodically backing up the changes to a CSV file to manage memory usage.
Please note the following before running the script:
- Domain Credentials: If you're not running this script with sufficient domain admin privileges, you might need to add a credential prompt or specify credentials within the script (not recommended for security reasons).
- Path Customization: You'll need to customize the
$SharePath and $BackupCSVPath variables to match your environment.
- SIDHistory File Path: The
$SIDHistoryFilePath is set to a default location; adjust as necessary.
- Error Handling: Enhanced error handling is included to manage potential issues gracefully.
# Define paths and file names
$SharePath = "YourWindowsSharePathHere" # Path to the Windows share to process
$BackupCSVPath = "C:\ACL_Backups\ACL_Backups_$(Get-Date -Format yyyyMMdd_HHmm).csv" # Path for ACL backup CSV
$SIDHistoryFilePath = "C:\SIDHistory\SIDHistory_$(Get-Date -Format yyyyMMdd_HHmm).csv" # Path for SIDHistory CSV
# Function to get SIDHistory from the domain and return it as an array
function Get-SIDHistoryFromDomain {
try {
$SIDHistoryArray = @()
$UsersAndGroups = Get-ADObject -Filter * -Properties SIDHistory
foreach ($Object in $UsersAndGroups) {
if ($Object.SIDHistory) {
foreach ($OldSID in $Object.SIDHistory) {
$SIDHistoryArray += [PSCustomObject]@{
OldSID = $OldSID.Value
NewSID = $Object.ObjectSID.Value
}
}
}
}
$SIDHistoryArray
}
catch {
Write-Error "Failed to retrieve SIDHistory from domain: $_"
@()
}
}
# Function to save SIDHistory array to a file
function Save-SIDHistoryToFile {
param (
[array]$SIDHistoryArray,
[string]$FilePath
)
try {
$SIDHistoryArray | Export-Csv -Path $FilePath -NoTypeInformation
}
catch {
Write-Error "Failed to save SIDHistory to file: $_"
}
}
# Function to load SIDHistory from a file into an array
function Load-SIDHistoryFromFile {
param (
[string]$FilePath
)
try {
if (Test-Path $FilePath) {
Import-Csv -Path $FilePath
}
else {
Write-Warning "SIDHistory file not found. Returning empty array."
@()
}
}
catch {
Write-Error "Failed to load SIDHistory from file: $_"
@()
}
}
# Function to update ACLs and backup changes
function Update-ACLsAndBackup {
param (
[array]$SIDHistoryArray,
[string]$Path,
[string]$BackupCSVPath,
[ref]$BackupArray,
[int]$Counter,
[int]$Threshold = 1000
)
try {
$ACL = Get-Acl -Path $Path
$Changed = $false
foreach ($ACE in $ACL.Access) {
$NewSIDFound = $SIDHistoryArray | Where-Object { $_.OldSID -eq $ACE.IdentityReference.Value }
if ($NewSIDFound) {
$ACE.IdentityReference = (New-Object System.Security.Principal.SecurityIdentifier($NewSIDFound.NewSID))
$Changed = $true
}
}
if ($Changed) {
# Backup old ACL
$BackupObject = [PSCustomObject]@{
Path = $Path
OldACEs = ($ACL.Access | ConvertTo-Json -Compress)
NewACEs = ($ACL.Access | ConvertTo-Json -Compress)
Timestamp = Get-Date
}
$BackupArray.Value.Add($BackupObject) # Use Add method for ArrayList
# Apply new ACL
Set-Acl -Path $Path -AclObject $ACL
}
$Counter.Value++
if ($Counter.Value % $Threshold -eq 0) {
# Backup to CSV and clear array
$BackupArray.Value | Export-Csv -Path $BackupCSVPath -Append -NoTypeInformation
$BackupArray.Value.Clear()
}
}
catch {
Write-Error "Error updating ACLs for $Path: $_"
}
}
# Main Script
$BackupArray = New-Object System.Collections.ArrayList
$Counter = 0
# Check if SIDHistory file exists, load or create it
if (Test-Path $SIDHistoryFilePath) {
Write-Host "Loading SIDHistory from file..."
$SIDHistory = Load-SIDHistoryFromFile -FilePath $SIDHistoryFilePath
}
else {
Write-Host "Getting SIDHistory from domain and saving to file..."
$SIDHistory = Get-SIDHistoryFromDomain
Save-SIDHistoryToFile -SIDHistoryArray $SIDHistory -FilePath $SIDHistoryFilePath
}
# Validate SIDHistory
if (-not $SIDHistory -or $SIDHistory.Count -eq 0) {
Write-Warning "SIDHistory is empty. No ACL updates will be made."
exit
}
# Process filesystem
Get-ChildItem -Path $SharePath -Recurse -Force | ForEach-Object {
Update-ACLsAndBackup -SIDHistoryArray $SIDHistory -Path $_.FullName -BackupCSVPath $BackupCSVPath -BackupArray ([ref]$BackupArray) -Counter ([ref]$Counter)
}
# Final backup if any changes were made
if ($BackupArray.Count -gt 0) {
$BackupArray | Export-Csv -Path $BackupCSVPath -Append -NoTypeInformation
$BackupArray.Clear()
}
Write-Host "Script completed."
Important Considerations:
- Permissions: Ensure you have the necessary permissions to read and modify ACLs across the specified share.
- Testing: It's crucial to test this script in a non-production environment first to validate its behavior with your specific setup.
- SIDHistory Accuracy: The effectiveness of this script depends on the accuracy and completeness of the SIDHistory attribute in your Active Directory.
- Performance: For very large datasets, consider running the script in batches or optimizing it further for performance.