Advertisement
mariussm

Handy ADFS PowerShell cmdlets

Mar 4th, 2015
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .Synopsis
  3.    Copies all claim rules from one RPT to another
  4. .DESCRIPTION
  5.    Copies all claim rules from one RPT to another
  6. .EXAMPLE
  7.    Copy-ADFSClaimRules -SourceRelyingPartyTrustName "Office 365" -DestinationRelyingPartyTrustName "Token testing website - Marius"
  8. #>
  9. function Copy-ADFSClaimRules
  10. {
  11.     [CmdletBinding()]
  12.     Param
  13.     (
  14.         # Param1 help description
  15.         [Parameter(Mandatory=$true,
  16.                    ValueFromPipeline=$false,
  17.                    Position=0)]
  18.         [string] $SourceRelyingPartyTrustName,
  19.  
  20.         [Parameter(Mandatory=$true,
  21.                    ValueFromPipeline=$false,
  22.                    Position=1)]
  23.         [string] $DestinationRelyingPartyTrustName
  24.     )
  25.  
  26.     Begin
  27.     {
  28.     }
  29.     Process
  30.     {
  31.         $SourceRPT = Get-AdfsRelyingPartyTrust -Name $SourceRelyingPartyTrustName
  32.         $DestinationRPT = Get-AdfsRelyingPartyTrust -Name $DestinationRelyingPartyTrustName
  33.  
  34.         if(!$SourceRPT) {
  35.             Write-Error "Could not find $SourceRelyingPartyTrustName"
  36.         } elseif(!$DestinationRPT) {
  37.             Write-Error "Could not find $DestinationRelyingPartyTrustName"
  38.         }
  39.  
  40.         Set-AdfsRelyingPartyTrust -TargetRelyingParty $DestinationRPT -IssuanceTransformRules $SourceRPT.IssuanceTransformRules -IssuanceAuthorizationRules $SourceRPT.IssuanceAuthorizationRules -DelegationAuthorizationRules $SourceRpT.DelegationAuthorizationRules
  41.     }
  42.     End
  43.     {
  44.     }
  45. }
  46.  
  47.  
  48.  
  49.  
  50. <#
  51. .Synopsis
  52.    Returns the thumbprint of the ADFS token signing certificate
  53. .DESCRIPTION
  54.    Returns the thumbprint of the ADFS token signing certificate
  55. .EXAMPLE
  56.    Get-AdfsTokenSigningThumbprint adfs.goodworkaround.com
  57. #>
  58. function Get-AdfsTokenSigningThumbprint
  59. {
  60.     [CmdletBinding()]
  61.     Param
  62.     (
  63.         # Param1 help description
  64.         [Parameter(Mandatory=$true,
  65.                    ValueFromPipelineByPropertyName=$false,
  66.                    Position=0)]
  67.         $ADFS
  68.     )
  69.  
  70.     Begin
  71.     {
  72.     }
  73.     Process
  74.     {
  75.         $metadata = Invoke-RestMethod -Uri ("https://{0}/FederationMetadata/2007-06/FederationMetadata.xml" -f $ADFS)
  76.         $tempfile = "{0}\adfsTempCert.cer" -f $env:temp
  77.         $metadata.EntityDescriptor.Signature.KeyInfo.X509Data.X509Certificate | Set-Content -Path $tempfile
  78.         $cert = (New-Object System.Security.Cryptography.X509Certificates.X509Certificate2)
  79.         $cert.Import($tempfile)
  80.  
  81.         return $cert.Thumbprint
  82.     }
  83.     End
  84.     {
  85.     }
  86. }
  87.  
  88.  
  89. <#
  90. .Synopsis
  91.    Copies relying party trust
  92. .DESCRIPTION
  93.    Copies relying party trust
  94. .EXAMPLE
  95.    Copy-AdfsRelyingPartyTrust "Intranett Test" "Intranett Prod" "urn:sharepoint:prod"
  96. #>
  97. function Copy-AdfsRelyingPartyTrust
  98. {
  99.     [CmdletBinding()]
  100.     [OutputType([int])]
  101.     Param
  102.     (
  103.         [Parameter(Mandatory=$true,
  104.                    ValueFromPipeline=$false,
  105.                    Position=0)]
  106.         $SourceRelyingPartyTrustName,
  107.  
  108.         [Parameter(Mandatory=$true,
  109.                    ValueFromPipeline=$false,
  110.                    Position=1)]
  111.         $NewRelyingPartyTrustName,
  112.  
  113.         [Parameter(Mandatory=$true,
  114.                    ValueFromPipeline=$false,
  115.                    Position=2)]
  116.         $NewRelyingPartyTrustIdentifier
  117.     )
  118.  
  119.     Begin
  120.     {
  121.     }
  122.     Process
  123.     {
  124.         $SourceRelyingPartyTrust  = Get-AdfsRelyingPartyTrust -Name $SourceRelyingPartyTrustName
  125.  
  126.         $exceptedAttributes = @("ConflictWithPublishedPolicy","OrganizationInfo","ProxyEndpointMappings","LastUpdateTime","PublishedThroughProxy","LastMonitoredTime")
  127.         $parameters = @{}
  128.         $SourceRelyingPartyTrust | Get-Member -MemberType Property | where{$_.name -notin $exceptedAttributes} | foreach {
  129.             if($SourceRelyingPartyTrust.($_.Name) -ne $null) {
  130.                 $parameters[$_.Name] = $SourceRelyingPartyTrust.($_.Name)
  131.             }
  132.         }
  133.         $parameters.Name = $NewRelyingPartyTrustName
  134.         $parameters.Identifier = $NewRelyingPartyTrustIdentifier
  135.        
  136.         Add-AdfsRelyingPartyTrust @parameters
  137.     }
  138.     End
  139.     {
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement