Get domain role and PC type from remote computers

Today I want to show how to get domain role and PC type using PowerShell function. You can get this information easily using 1 liner script but results will be presented as numbers.

Domain role and PC type

Function is based on Get-WmiObject command and it query Win32_computersystem class. To get “PC type” and “Domain Role” using just one command we have to use Select-Object cmdlet after the pipeline:

 
Get-Wmiobject -Class ‘Win32_computersystem’ -ComputerName $env:computername | Select-Object pcsystemtype,domainrole

Unfortunelty this command will return only numbers. We have to use Switch to transle it to more readable value. For example DomainRole property may show that a computer is a “member workstation”.

This property is inherited from CIM_ManagedSystemElement:

– Standalone Workstation (0)
– Member Workstation (1)
– Standalone Server (2)
– Member Server (3)
– Backup Domain Controller (4)
– Primary Domain Controller (5)

For more information you can check MSDN site.

#Example for getting "Domain Role" property for your local host
$Role = Get-Wmiobject -Class ‘Win32_computersystem’

Switch ($Role.domainrole) {
      "0"     { $Val2 = "Stand-alone workstation"}
      "1"     { $Val2 = "Member workstation"}
      "2"     { $Val2 = "Stand-alone server"}
      "3"     { $Val2 = "Member server"}
      "4"     { $Val2 = "Domain controller"}
      "5"     { $Val2 = "Pdc emulator domain controller"}
}
$Val2

How to use it?:

Get-DomainRole -Computername DC01
(GC "c:\users\$env:username\desktop\list.txt") | % {Get-DomainRole -Computername $_}

Final script:

Function Get-DomainRole {
    [CmdletBinding()]        
    param
    (
        [Parameter(Position=0, Mandatory = $True, HelpMessage="Provide computername", ValueFromPipeline = $true)] 
        $Computername
    )
    $Results = @()
    $Role = $Val1 = $Val2 = $Object = $FQDN = $null
    $FQDN = ([System.Net.Dns]::GetHostByName(("$Computername")))
    If (!$FQDN) {
        Write-Warning "$Computername does not exist"
    }
    Else {
        Try {
            $Role = Get-Wmiobject -Class ‘Win32_computersystem’ -ComputerName $Computername -ErrorAction Stop
        }
        Catch {
            $_.Exception.Message
            Continue
        }
        If ($Role) {
            Switch ($Role.pcsystemtype) {
                "1"     { $Val1 = "Desktop"}
                "2"     { $Val1 = "Mobile / Laptop"}
                "3"     { $Val1 = "Workstation"}
                "4"     { $Val1 = "Enterprise Server"}
                "5"     { $Val1 = "Small Office and Home Office (SOHO) Server"}
                "6"     { $Val1 = "Appliance PC"}
                "7"     { $Val1 = "Performance Server"}
                "8"     { $Val1 = "Maximum"}
                default { $Val1 = "Not a known Product Type"}
            }
            Switch ($Role.domainrole) {
                "0"     { $Val2 = "Stand-alone workstation"}
                "1"     { $Val2 = "Member workstation"}
                "2"     { $Val2 = "Stand-alone server"}
                "3"     { $Val2 = "Member server"}
                "4"     { $Val2 = "Domain controller"}
                "5"     { $Val2 = "Pdc emulator domain controller"}
            }
            $Object = New-Object PSObject -Property ([ordered]@{ 
                Computer    = $Computername
                IPAddress   = $FQDN.AddressList[0].IPAddressToString
                PCType      = $Val1
                DomainRole  = $Val2   
            })
            $Results += $Object
        }
    }
    Return $Results
}

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.