Last active
June 20, 2018 12:03
-
-
Save Froosh/bd17ff4675f945dc7dc3bbb6bbda036d to your computer and use it in GitHub Desktop.
Backup MIM Config (Synchronisation and Service/Portal) and create diffs/history with git
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
#Requires -Version 5 | |
[CmdletBinding()] | |
Param ( | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$MIMBackupPath = "C:\MIMBackup" | |
, | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$MIMServiceURI = "http://localhost:5725" | |
) | |
Begin { | |
$ErrorActionPreference = "Stop" | |
New-Alias -Name Git -Value (Join-Path -Path $MIMBackupPath -ChildPath "PortableGit\cmd\git.exe") | |
Import-Module -Name (Join-Path -Path $MIMBackupPath -ChildPath "Modules\FIMAutomation\FIMAutomation.psd1") | |
Import-Module -Name (Join-Path -Path $MIMBackupPath -ChildPath "Modules\LithnetRMA\*\LithnetRMA.psd1") | |
Import-Module -Name (Join-Path -Path $MIMBackupPath -ChildPath "Modules\LithnetMiisAutomation\*\LithnetMiisAutomation.psd1") | |
$MIMConfigPath = Join-Path -Path $MIMBackupPath -ChildPath "MIMConfig" | |
$ConfigFolders = @( | |
"MIMPortal" | |
"MIMService" | |
"MIMService\PolicyConfig" | |
"MIMService\PortalConfig" | |
"MIMService\SchemaConfig" | |
"MIMSync" | |
"MIMSync\Extensions" | |
"MIMSync\MA" | |
"MIMSync\MAData" | |
"MIMSync\Metaverse" | |
) | |
if (-not (Test-Path -PathType Container -Path $MIMBackupPath)) { | |
New-Item -ItemType Directory -Path $MIMBackupPath | Out-Null | |
} | |
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 $MIMBackupPath | |
} | |
Process { | |
## | |
## MIM Service Config | |
## | |
$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 | |
## | |
$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\MAData\*) | |
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 "MAData\*") -Destination (Join-Path -Path $MIMConfigPath -ChildPath MIMSync\MAData) | |
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") | |
} | |
## | |
## Commit any changes to the local Git repository | |
## | |
Git status | |
Git add --all | |
Git commit --message="MIM Config Backup $(Get-Date -Format u)" | |
# 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