search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Getting Active Directory Information with Powershell
Posted in Hosting & IIS7, Windows Powershell, Windows Server | 1 Comment | 12,169 views | 02/03/2010 12:35

My script works on all Active Directory Infrastructures without any change on script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Function Get-ADInfo
{
    $ADDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    $ADDomainName = $ADDomain.Name
    $Netbios = $ADDomain.Name.Split(".")[0].ToUpper()
    $ADServer = ($ADDomain.InfrastructureRoleOwner.Name.Split(".")[0])
    $FQDN = "DC=" + $ADDomain.Name -Replace("\.",",DC=")
 
    $Results = New-Object Psobject
    $Results | Add-Member Noteproperty Domain $ADDomainName
    $Results | Add-Member Noteproperty FQDN $FQDN
    $Results | Add-Member Noteproperty Server $ADServer
    $Results | Add-Member Noteproperty Netbios $Netbios
    Write-Output $Results
}

Usage is pretty simple:

Get-ADInfo

Thats all! :)

Getting Netbios name:

(Get-ADInfo).Netbios

Getting FQDN:

(Get-ADInfo).FQDN

Getting Active Directory Domain Name:

(Get-ADInfo).$ADDomainName

Getting Active Directory Primary Server Name:

(Get-ADInfo).$ADServer

You can use this in your all scripts. You no longer need any active directory information.


Comments (1)

Cameron Wilson

May 15th, 2012
23:29:36

This is all fine and good except that the NETBIOS name is not necessarily as easy as pulling off the 0th value of the FQDN. In other words, this is not accurate in all situations. A more reliable method would be to use [adsi] to get the nETBIOSName attribute from ActiveDirectory.



Leave a Reply