# encoding: ascii
# api: powershell
# title: AD FSMO Roles
# description: This PowerShell function returns a list of the Active Directory FSMO role holders for one or more Active Directory domains and forests. This function depends on the Active Directory module, specifically the Get-ADDomain and Get-ADForest cmdlets. This module can either be installed locally, via the Remote Server Administration Tools or imported via Implicit remoting prior to running this function. More information about this function can be found on my blog site: http://mikefrobbins.com/2013/04/11/use-powershell-to-find-where-the-current-fsmo-roles-are-assigned-in-active-directory/
# version: 0.1
# type: function
# author: Mike F Robbins
# license: CC0
# function: Get-FSMORole
# x-poshcode-id: 4104
# x-archived: 2013-04-15T06:14:03
# x-published: 2013-04-13T14:11:00
#
#
function Get-FSMORole {
<#
.SYNOPSIS
Retrieves the FSMO role holders from one or more Active Directory domains and forests.
.DESCRIPTION
Get-FSMORole uses the Get-ADDomain and Get-ADForest Active Directory cmdlets to determine
which domain controller currently holds each of the Active Directory FSMO roles.
.PARAMETER DomainName
One or more Active Directory domain names.
.EXAMPLE
Get-Content domainnames.txt | Get-FSMORole
.EXAMPLE
Get-FSMORole -DomainName domain1, domain2
#>
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$True)]
        [string[]]$DomainName = $env:USERDOMAIN
    )
    BEGIN {
        Import-Module ActiveDirectory -Cmdlet Get-ADDomain, Get-ADForest -ErrorAction SilentlyContinue
    }
    PROCESS {
        foreach ($domain in $DomainName) {
            Write-Verbose "Querying $domain"
            Try {
            $problem = $false
            $addomain = Get-ADDomain -Identity $domain -ErrorAction Stop
            } Catch { $problem = $true
            Write-Warning $_.Exception.Message
            }
            if (-not $problem) {
                $adforest = Get-ADForest -Identity (($addomain).forest)

                New-Object PSObject -Property @{
                    InfrastructureMaster = $addomain.InfrastructureMaster
                    PDCEmulator = $addomain.PDCEmulator
                    RIDMaster = $addomain.RIDMaster
                    DomainNamingMaster = $adforest.DomainNamingMaster
                    SchemaMaster = $adforest.SchemaMaster
                }
            }
        }
    }
}
