Posted in
Hosting & IIS7,
Windows Powershell,
Windows Server |
1 Comment | 12,167 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
} |
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:
Thats all! :)
Getting Netbios name:
Getting FQDN:
Getting Active Directory Domain Name:
(Get-ADInfo).$ADDomainName |
(Get-ADInfo).$ADDomainName
Getting Active Directory Primary Server Name:
You can use this in your all scripts. You no longer need any active directory information.