Skip to content

Instantly share code, notes, and snippets.

@bpalfaro
Forked from Froosh/Backup-MIMConfig.ps1
Created June 20, 2018 12:03
Show Gist options
  • Save bpalfaro/1d74c327765a33e8c39a785c185c0610 to your computer and use it in GitHub Desktop.
Save bpalfaro/1d74c327765a33e8c39a785c185c0610 to your computer and use it in GitHub Desktop.
Backup MIM Config (Synchronisation and Service/Portal) and create diffs/history with git
#Requires -Version 5
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$MIMConfigPath
,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$MIMServiceURI = "http://localhost:5725"
,
[Parameter()]
[switch]
$SyncConfig
,
[Parameter()]
[switch]
$PortalConfig
)
Begin {
$ErrorActionPreference = "Stop"
New-Alias -Name Git -Value "git.exe"
if($PortalConfig){
Import-Module -Name (Join-Path -Path $MIMConfigPath -ChildPath "Modules\FIMAutomation\FIMAutomation.psd1")
Import-Module -Name (Join-Path -Path $MIMConfigPath -ChildPath "Modules\LithnetRMA\*\LithnetRMA.psd1")
}
if($SyncCOnfig){
Import-Module -Name "LithnetMIISAutomation"
}
$ConfigFolders = @(
"MIMPortal"
"MIMService"
"MIMService\PolicyConfig"
"MIMService\PortalConfig"
"MIMService\SchemaConfig"
"MIMSync"
"MIMSync\Extensions"
"MIMSync\MA"
"MIMSync\Metaverse"
)
if (-not (Test-Path -PathType Container -Path $MIMConfigPath)) {
New-Item -ItemType Directory -Path $MIMConfigPath | Out-Null
}
# Clean out and re-create the config folders, so that git can see if items have been removed
foreach ($Folder in $ConfigFolders) {
$FolderPath = Join-Path -Path $MIMConfigPath -ChildPath $Folder
if (Test-Path -PathType Container -Path $FolderPath) {
Remove-Item -Recurse -Path $FolderPath
}
New-Item -ItemType Directory -Path $FolderPath | Out-Null
}
Set-Location -Path $MIMConfigPath
}
Process {
##
## MIM Service Config
##
if($PortalConfig){
$MIMServiceConfig = "$((Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\FIMService -Name ImagePath).ImagePath.Trim('"')).config"
Copy-Item -Recurse -Path $MIMServiceConfig -Destination (Join-Path -Path $MIMConfigPath -ChildPath MIMService)
Remove-Item -Path (Join-Path -Path $MIMConfigPath -ChildPath MIMService\PolicyConfig\*)
$PolicyConfig = Export-FIMConfig -Uri $MIMServiceURI -PolicyConfig -AllLocales
$PolicyConfig | ConvertFrom-FIMResource -File (Join-Path -Path $MIMConfigPath -ChildPath MIMService\PolicyConfig.xml)
$PolicyConfig | ForEach-Object -Process {
$UUID = $PSItem.ResourceManagementObject.ObjectIdentifier -replace "^urn:uuid:",""
$PSItem | ConvertFrom-FIMResource -File (Join-Path -Path $MIMConfigPath -ChildPath MIMService\PolicyConfig\$UUID.xml)
}
Remove-Item -Path (Join-Path -Path $MIMConfigPath -ChildPath MIMService\PortalConfig\*)
$PortalConfig = Export-FIMConfig -Uri $MIMServiceURI -PortalConfig -AllLocales
$PortalConfig | ConvertFrom-FIMResource -File (Join-Path -Path $MIMConfigPath -ChildPath MIMService\PortalConfig.xml)
$PortalConfig | ForEach-Object -Process {
$UUID = $PSItem.ResourceManagementObject.ObjectIdentifier -replace "^urn:uuid:",""
$PSItem | ConvertFrom-FIMResource -File (Join-Path -Path $MIMConfigPath -ChildPath MIMService\PortalConfig\$UUID.xml)
}
Remove-Item -Path (Join-Path -Path $MIMConfigPath -ChildPath MIMService\SchemaConfig\*)
# Include '/SynchronizationFilter' as per https://technet.microsoft.com/en-us/library/ff400267(v=ws.10).aspx
$SchemaConfig = Export-FIMConfig -Uri $MIMServiceURI -SchemaConfig -CustomConfig "/SynchronizationFilter" -AllLocales
$SchemaConfig | ConvertFrom-FIMResource -File (Join-Path -Path $MIMConfigPath -ChildPath MIMService\SchemaConfig.xml)
$SchemaConfig | ForEach-Object -Process {
$UUID = $PSItem.ResourceManagementObject.ObjectIdentifier -replace "^urn:uuid:",""
$PSItem | ConvertFrom-FIMResource -File (Join-Path -Path $MIMConfigPath -ChildPath MIMService\SchemaConfig\$UUID.xml)
}
}
##
## MIM Synchronisation Config
##
if($SyncConfig){
$MIMSyncInstallPath = (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\FIMSynchronizationService\Parameters' -Name Path).Path
Remove-Item -Path (Join-Path -Path $MIMConfigPath -ChildPath MIMSync\MA\*)
Remove-Item -Path (Join-Path -Path $MIMConfigPath -ChildPath MIMSync\Metaverse\*)
Remove-Item -Path (Join-Path -Path $MIMConfigPath -ChildPath MIMSync\Extensions\*) -Recurse
Copy-Item -Recurse -Path (Join-Path -Path $MIMSyncInstallPath -ChildPath "Extensions\*") -Destination (Join-Path -Path $MIMConfigPath -ChildPath MIMSync\Extensions)
Export-MetaverseConfiguration -Path (Join-Path -Path $MIMConfigPath -ChildPath MIMSync\Metaverse)
foreach ($MA in (Get-ManagementAgent)) {
Export-ManagementAgent -MA $MA -File (Join-Path -Path $MIMConfigPath -ChildPath "MIMSync\MA\$($MA.Name).xml")
}
}
##
## Regex replace export-date entries in files
##
gci -Path $MIMConfigPath -Recurse -Filter *.XML | ForEach {
(Get-Content $_.FullName | ForEach { $_ -replace "export-date='\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}'", "export-date=''" }) |
Set-Content $_.FullName
}
##
## Commit any changes to the local Git repository
##
Git status
Git add --all
Git commit --message="MIM Config Backup $(Get-Date -Format u)"
Git push
# Do some Git cleanup and optimise the storage (put them into zipped 'pack' files)
Git gc
}
End {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment