Get-ADFSMetadataFile.psm1

#Requires -Version 3.0

function Get-ADFSMetadataFile {

    <#
        .SYNOPSIS
        Easily retrieves the FederationMetadata.xml from any ADFS server
 
        .DESCRIPTION
        Easily retrieves the ADFS Federation-Metadata url or file from any ADFS server by specifying the base-URL (without HTTPS, ex. adfs.contoso.org)
         
        .PARAMETER ADFSSubDomain
        Base URL of the ADFS server (e.x. "adfs.contoso.org")
 
        .PARAMETER DownloadToFile
        Downloads the metadata file to the current working directory and appends the name of the ADFS server to the file (useful for identification if managing multiple ADFS server farms).
 
        .INPUTS
        Accepts pipline input in [String[]] (String Array) format.
 
        .OUTPUTS
        System.Object. Get-ADFSMetadataFile outputs an object with either metadata_url or file_path properties depending on the inputs.
 
        .EXAMPLE
        PS> Get-ADFSMetadataFile -ADFSSubDomain adfs.contoso.org
        metadata_url
        ------------
        https://adfs.contoso.org/FederationMetadata/2007-06/FederationMetadata.xml
 
        .EXAMPLE
        PS> extension -name "File" -extension "doc"
        file_path
        ---------
        C:\Users\SomeUser\Desktop\FederationMetadata_avhidp01.aspenhospital.org.xml
    #>


[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$ADFSSubDomain,
[Parameter(Mandatory=$false)][switch]$DownloadToFile
)
    Process
    {

    foreach($ADFS in $ADFSSubDomain){

            $ADFS = [regex]::Matches($ADFS,'^(?:https?:\/\/)?(?:[^@\n]+@)?([^:\/\n?]+)').Groups[1].Value

            if ($ADFS -like "@{*"){throw "Expected String or Array but got Object instead!"}
            
            $metadataPath = [String]::Format("https://{0}/FederationMetadata/2007-06/FederationMetadata.xml",$ADFS)
            switch ($DownloadToFile)
            {
                $true
                {
                    $outPath = ".\FederationMetadata_$ADFS.xml"
                    Invoke-WebRequest $metadataPath -OutFile $outPath
                    return [pscustomobject]@{"file_path"=(ls $outPath).FullName}
                }
                
                $false
                {
                    return [pscustomobject]@{"metadata_url"=$metadataPath}
                }
            }
        }
    }
}

Export-ModuleMember Get-ADFSMetadataFile