Get AD System info remotely using PowerShell function

In this post you will find out how to get basic AD system information using PowerShell function. There are multiple articles on our blog describing plenty of Active Directory aspects. This time I want to show you another great function which helps discover useful data from remote systems.

Get AD System

Using below function you can get the following information:

– Computername
– DomainDNSName
– DomainShortName
– ForestDNSName
– IsNativeMode
– PDCRoleOwner
– SchemaRoleOwner
– SiteName
– UserName

Below you can find example of script usage against remote machines:

# Single property
$ADInfo = "ForestDNSName"
$Computername = "DC01","DC02"
Invoke-Command -ComputerName $Computername -ArgumentList $ADInfo -ScriptBlock ${Function:Get-ADSystemInfo} | Select-Object * -ExcludeProperty runspaceid | Out-GridView -Title "ADSystemInfo"

# All properties
$Computername = "DC01","DC02"
Invoke-Command -ComputerName $Computername -ScriptBlock ${Function:Get-ADSystemInfo} | Select-Object * -ExcludeProperty pscomputername,runspaceid | Out-GridView -Title "ADSystemInfo"

Script allows you to specify also single parameter. (By default it displays all of them):

Final script:

Function Get-ADSystemInfo {
    <#
        .SYSNOPSIS
            Performs query for ADSystemInfo

        .DESCRIPTION
            Performs query for ADSystemInfo

        .PARAMETER ADSystemInfo
            List of all ADSystemInfo properties or single one

        .NOTES
            Name: Get-ADSystemInfo
            Author: Pawel Janowicz
            TechnetLink: https://msdn.microsoft.com/en-us/library/aa705962(v=vs.85).aspx
            Version History:
                Version 1.0 -- 12 Jan 2018
                    -Initial creation

        .EXAMPLE
            Get-ADSystemInfo

            Description
            -----------
            Lists all ADSystemInfo properties

        .EXAMPLE
            Get-ADSystemInfo -ADSystemInfo "DomainDNSName"

            Description
            -----------
            Lists one ADSystemInfo property
    #>
    [cmdletbinding()]
    Param (
        [Parameter(Position=0, Mandatory=$false, HelpMessage="Provide ADSystemInfo property", ValueFromPipeline=$false)] 
        [ValidateSet('Computername','DomainDNSName','DomainShortName','ForestDNSName','IsNativeMode','PDCRoleOwner','SchemaRoleOwner','SiteName','UserName')]
        $ADSysteInfo = "All"
    )
    Begin 
    {
        $SysInfo = New-Object -ComObject "ADSystemInfo"
        $Report = @()

        If($ADSysteInfo -eq "All")
        {
            $Checks = "Computername",
                      "DomainDNSName", 
                      "DomainShortName",
                      "ForestDNSName",
                      "IsNativeMode",
                      "PDCRoleOwner", 
                      "SchemaRoleOwner",
                      "SiteName",
                      "UserName"
        }
        Else
        {
            $Checks = $ADSysteInfo
        }
    }
    Process 
    {
            Try 
            {
                $Object = " " | Select $Checks
                ForEach ( $Property in $Checks )
                {
                    $Result = $SysInfo.GetType().InvokeMember("$Property", "GetProperty", $Null, $SysInfo, $Null)
                    $Object."$Property" = $Result

                    Remove-Variable Result  
                }
            }
            Catch
            {
                Write-Warning "[$($Computer)] $($Property): $("failure")"
                $Object."$Property" = "(null)"
                Continue
            }

            $report += $Object
    }
    End 
    {
        Return $report
    }
}

Leave a Reply

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