# How to find different server types in Active Directory with PowerShell

Working as a freelancer is a great thing if you can handle it. Each day, each week something new happens and a new problem shows up on my doorstep. It also means it’s almost never boring at your job and you get to play with new stuff. But there’s one drawback to this. You’re often thrown at the problem, told to fix it but often that’s about as much information as you get. It wasn’t very different today. I was told to switch Office 365 from ADFS to Password Synchronization. While reasons for this are not really important, the important question here is what is the name of AD Connect server that’s responsible for this configuration?

blog

06 Feb 2019 pl

![How to find different server types in Active Directory with PowerShell](/wp-content/uploads/2019/02/img_5c5b0c863deae.png)

[Zobacz obrazy (4)](#pf-content-image-1-0)

Working as a freelancer is a great thing if you can handle it. Each day, each week something new happens, and a new problem shows up on my doorstep. It also means it's rarely dull at your job and you get to play with new stuff. But there's one drawback to this. You're often thrown at the problem, told to fix it, but usually, that's about as much information as you get. It wasn't very different today. I was told to switch **Office 365** from **ADFS** to **Password Synchronization**. The critical question here is what **ADConnect **server is responsible for this configuration?

### 💡 How to find ADConnect

While today it was just **AD Connect** that I had to find, yesterday it was **Microsoft Exchange** server. Tomorrow they will most likely ask me to fix **SQL** so before I tell them I quit I decided that I need some handy **PowerShell** command that will do my work for me. Of course, some companies name their servers after the role they have so it's quite easy to find them (like my home lab), but sometimes it's not that obvious. So what's the best way to find roles of servers without having a clue about them? Ask your **Active D**irectory! Starting with **AD Connect**, I've created small function. It's not foolproof, but it works. If you have a habit of touching your AD, changing descriptions, modifying every bit of what AD Connect does when it's set up this will most likely not work.

```powershell
function Find-ADConnectServer {
    [alias('Find-ADSyncServer')]
    param(

    )
    $Description = Get-ADUser -Filter { Name -like "MSOL*" } -Properties Description | Select-Object Description -ExpandProperty Description

    foreach ($Desc in $Description) {
        $PatternType = "(?
    [CmdletBinding()]
    param(

    )
    $ExchangeServers = Get-ADGroup -Identity "Exchange Servers" | Get-ADGroupMember | Where-Object { $_.objectClass -eq 'computer' }
    foreach ($Server in $ExchangeServers) {
        $Data = Get-ADComputer -Identity $Server.SamAccountName -Properties Name, DNSHostName, OperatingSystem, DistinguishedName, ServicePrincipalName
        [PSCustomObject] @{
            Name              = $Data.Name
            FQDN              = $Data.DNSHostName
            OperatingSystem   = $Data.OperatingSystem
            DistinguishedName = $Data.DistinguishedName
            Enabled           = $Data.Enabled
        }
    }
}
```

```powershell
Find-ExchangeServer | Format-Table -Autosize *
```

[!\[Tabela wynikow PowerShell z Find-ExchangeServer pokazujaca serwery Exchange odnalezione w Active Directory.\](/wp-content/uploads/2019/02/img_5c5b09bc8ca95.png)](/wp-content/uploads/2019/02/img_5c5b09bc8ca95.png)

Simple, but effective.

### 💡 How to find Hyper-V Servers

This little function helps to find **Hyper-V servers** in your **Active Directory domain**. It doesn't detect **Windows 10 Hyper-V** (bummer I know) but it seems to work fine on server environments.

```powershell
function Find-HyperVServer {
    [cmdletbinding()]
    param()
    try {
        $ADObjects = Get-ADObject -Filter 'ObjectClass -eq "serviceConnectionPoint" -and Name -eq "Microsoft Hyper-V"' -ErrorAction Stop
    } catch {
        Write-Error "Error: $_"
    }
    foreach ($Server in $ADObjects) {
        $Temporary = $Server.DistinguishedName.split(",")
        $DistinguishedName = $Temporary[1..$Temporary.Count] -join ","
        $Data = Get-ADComputer -Identity $DistinguishedName -Properties Name, DNSHostName, OperatingSystem, DistinguishedName, ServicePrincipalName
        [PSCustomObject] @{
            Name              = $Data.Name
            FQDN              = $Data.DNSHostName
            OperatingSystem   = $Data.OperatingSystem
            DistinguishedName = $Data.DistinguishedName
            Enabled           = $Data.Enabled
        }
    }
}
```

```powershell
Find-HyperVServer | Format-Table -Autosize *
```

It output similar data as Microsoft Exchange command.

### 💡 How to find all types of servers - Monster Edition

While working on next set I've thought that I will most likely forget all those command names soon enough and I need to have a reliable way to get my server types from Active Directory. That's why I've decided to prepare this little monster. While it may not be the most optimized code in the world it does, it's the job and returns data pretty quickly giving your full overview of servers.

[!\[Find-ServerTypes\](/wp-content/uploads/2019/02/img_5c5b0c863deae.png)](/wp-content/uploads/2019/02/img_5c5b0c863deae.png)

```powershell
function Find-ADConnectServer {
    [alias('Find-ADSyncServer')]
    param(

    )
    $Description = Get-ADUser -Filter { Name -like "MSOL*" } -Properties Description | Select-Object Description -ExpandProperty Description

    foreach ($Desc in $Description) {
        $PatternType = "(?
    [CmdletBinding()]
    param(
        [switch] $TestAvailability,
        [switch] $SkipEmpty
    )
    $Forest = Get-AdForest
    $Servers = foreach ($D in $Forest.Domains) {
        try {
            $DC = Get-ADDomainController -Server $D -Filter *
            foreach ($S in $DC) {
                [PSCustomObject]@{
                    Domain                   = $D
                    HostName                 = $S.HostName
                    Forest                   = $Forest.RootDomain
                    IPV4Address              = $S.IPV4Address
                    IsGlobalCatalog          = $S.IsGlobalCatalog
                    IsReadOnly               = $S.IsReadOnly
                    SchemaMaster             = ($S.OperationMasterRoles -contains 'SchemaMaster')
                    DomainNamingMasterMaster = ($S.OperationMasterRoles -contains 'DomainNamingMaster')
                    PDCEmulator              = ($S.OperationMasterRoles -contains 'PDCEmulator')
                    RIDMaster                = ($S.OperationMasterRoles -contains 'RIDMaster')
                    InfrastructureMaster     = ($S.OperationMasterRoles -contains 'InfrastructureMaster')
                    Comment                  = ''
                }
            }
        } catch {
            [PSCustomObject]@{
                Domain                   = $D
                HostName                 = ''
                Forest                   = $Forest.RootDomain
                IPV4Address              = ''
                IsGlobalCatalog          = ''
                IsReadOnly               = ''
                SchemaMaster             = $false
                DomainNamingMasterMaster = $false
                PDCEmulator              = $false
                RIDMaster                = $false
                InfrastructureMaster     = $false
                Comment                  = $_.Exception.Message
            }
        }
    }
    if ($TestAvailability) {
        foreach ($Server in $Servers) {
            if ($Server.IPV4Address -ne '') {
                $Output = Test-Connection -Count 1 -Server $Server.IPV4Address -Quiet -ErrorAction SilentlyContinue
                Add-Member -InputObject $Server -MemberType NoteProperty -Name 'Pingable' -Value $Output
            } else {
                Add-Member -InputObject $Server -MemberType NoteProperty -Name 'Pingable' -Value $false
            }
        }
    }
    if ($SkipEmpty) {
        return $Servers | Where-Object { $_.HostName -ne '' }
    }
    return $Servers
}
```

```powershell
Get-WinADForestControllers | Format-Table -Autosize *
# Alternatively
Get-WinADDomainControllers
```

[!\[Tabela wynikow PowerShell z Get-WinADForestControllers pokazujaca kontrolery domeny w lesie Active Directory.\](/wp-content/uploads/2019/02/img_5c5b15396971a.png)](/wp-content/uploads/2019/02/img_5c5b15396971a.png)

I've also added an option to verify connectivity to DC's quickly. It's a simple ping but sometimes helpful.

```powershell
Get-WinADForestControllers -TestAvailability | Format-Table -AutoSize *
```

[!\[Tabela wynikow PowerShell z Get-WinADForestControllers -TestAvailability pokazujaca dostepnosc kontrolerow domeny.\](/wp-content/uploads/2019/02/img_5c5b157a0f43a.png)](/wp-content/uploads/2019/02/img_5c5b157a0f43a.png)

In my final words, I wanted to add that functions I've created are based on **Active Directory** and require **RSAT** to work. They are asking different parts of **AD** (**Service Connection Point**, **Service Principal Name** and so on) to **guess server type**. It's possible some of that information is out of date, or that guess is incorrect. If you find this useful, please let me know and spread this knowledge. If I made a mistake also let me know so I can fix it and everyone can benefit.

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:66BDA34F4147AC685DE569A2F80E64E1410C20236AE0B4794301407F9EA8F1C7 -->
