Created
January 14, 2018 21:46
-
-
Save jermdavis/80e0f04d2f6cb574da7eb352c949e437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$true)] | |
[string]$configFolder, | |
[Parameter(Mandatory=$true)] | |
[string]$configPattern, | |
[Parameter(Mandatory=$true)] | |
[string]$currentRoles | |
) | |
function Update-ConfigFile | |
{ | |
param | |
( | |
[string]$configFile, | |
[string[]]$roles | |
) | |
process | |
{ | |
$xml = New-Object System.Xml.XmlDocument | |
$xml.Load($configFile) | |
$xns = New-Object System.Xml.XmlNamespaceManager $xml.NameTable | |
$xns.AddNamespace("role", "RoleManagement") | |
$elements = $xml.DocumentElement.SelectNodes("descendant::*[@role:RemoveOn]", $xns) | |
Write-Host "Found $($elements.Count) elements to process in $configFile" | |
$count = 0 | |
foreach($element in $elements) | |
{ | |
$value = $element.SelectSingleNode("@role:RemoveOn", $xns).Value | |
if($roles -match $value) | |
{ | |
$count = $count + 1 | |
$element.ParentNode.RemoveChild($element) | Out-Null | |
} | |
else | |
{ | |
$element.Attributes.RemoveNamedItem("RemoveOn", "RoleManagement") | Out-Null | |
} | |
} | |
Write-Host "Made $count removals in $configFile" | |
$xml.Save($configFile) | |
} | |
} | |
function Remove-UnwantedNamespaces | |
{ | |
param | |
( | |
[string]$file | |
) | |
process | |
{ | |
$txt = Get-Content $file | |
$expr = '(xmlns:[^=]*="RoleManagement"\s*)|([\w]*:RemoveOn=".*?"\s*)' | |
$result = $txt -replace $expr, "" | |
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False | |
[System.IO.File]::WriteAllLines($file, $result, $Utf8NoBomEncoding) | |
} | |
} | |
function Split-RoleString | |
{ | |
param | |
( | |
[string] $roleString | |
) | |
process | |
{ | |
$splitString = $roleString -split "," | |
return $splitString | % { $_.Trim() } | |
} | |
} | |
$roles = Split-RoleString $currentRoles | |
$filesToProcess = Get-ChildItem -Path $configFolder -Filter $configPattern -Recurse | Select-Object -ExpandProperty FullName | |
Write-Host "Start: Updating config files to remove instance-specific features" | |
foreach($file in $filesToProcess) | |
{ | |
Update-ConfigFile $file $roles | |
Remove-UnwantedNamespaces $file | |
} | |
Write-Host "Done: Updating config files to remove instance-specific features" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment