Backup and restore ADFS claims using PowerShell

Working with ADFS I find it handy to be able to backup and restore the claim rules of an application. Doing this allows me to confidently make my changes, knowing that I can get back to where I was. Being able to do this without involving anyone else or causing downtime for other applications is key.

It is for these reasons I’ve decided to create some PowerShell scripts. These allow easy creation backups of backups and methods for performing a restore.

Backing up claims

The following script will create a text-file containing the claims of the specified application.

#Variables
$SourceRPTName = 'Your relaying party trust name'
$SaveFolder = 'C:\ADFSBackup\Claims\'

$Date = Get-Date -Format yyyyMMdd
$Count = 0

#Read the RPTs claims
$Claims = (Get-AdfsRelyingPartyTrust -Name $SourceRPTName).IssuanceTransformRules

#Ensure nothing is overwritten
while (Test-Path ($SaveFolder + $SourceRPTName + '-' + $Date + '-' + $Count + '.txt')) {
$Count++
}

#Write the claims to file
$Claims | Out-File ($SaveFolder + $SourceRPTName + '-' + $Date + '-' + $Count + '.txt')
Example of the output file from the backup script.

Restore claims from backup

Below is the simple script I use to restore my claim backups.

$TargetRPTName = 'Your relaying party trust name'
$ClaimBackupPath = 'C:\ADFSBackup\Claims\Application-20200226-0.txt'

#Read the claim backup
[string]$ClaimBackup = Get-Content $ClaimBackupPath

#If import was successfull, overwrite the target RPTs claims
if ($ClaimBackup){
Set-AdfsRelyingPartyTrust -TargetName $TargetRPTName -IssuanceTransformRules $ClaimBackup
}

The scripts are also very useful when moving applications to new ADFS environments or making sure stage and production environments match.

If you need to copy claims between applications on the same server I suggest using this script from the PowerShell gallery.

Leave a Reply

Your email address will not be published. Required fields are marked *