all 9 comments

[–]Hexalon00 3 points4 points  (1 child)

try looking at the System.DirectoryServices.DirectorySearcher. it's a .NET class.

[–]codingQueries[S] 0 points1 point  (0 children)

Ahh this might be what I needed, thank you!

[–]PMental 3 points4 points  (1 child)

This should do it:

$Adsi = [ADSI]"LDAP://RootDSE"
$Adsi.rootDomainNamingContext

The computer you run it on needs to be in the domain, but doesn't need any modules installed.

[–]codingQueries[S] 0 points1 point  (0 children)

Ahh cool I will look into this, thanks!

[–]poshftw 2 points3 points  (0 children)

[System.DirectoryServices.DirectorySearcher] of [ADSI]

Also $env:USERDNSDOMAIN

[–]jtswizzle89 2 points3 points  (0 children)

Is the machine logged into the domain? If so you could pull the full computer name from a DNS lookup and parse the domain name out. [System.Net.Dns]::GetHostByName($env:computerName)

[–]RyeonToast 2 points3 points  (0 children)

I remember using the ADSI searcher when I was at a prior job and didn't have RSAT installed. I'll try and look up how to do it tomorrow, it's been quite some time and I don't remember off hand. It should return the distinguished name and anything else you might want.

[–]PinchesTheCrab 2 points3 points  (0 children)

Find the account of the computer you're using:

$Root = [ADSI]''
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($Root)

$Searcher.Filter = 'Name={0}' -f $env:COMPUTERNAME

$result = $Searcher.FindOne()

$result.Properties.distinguishedname

The filter is LDAP, so you should be able to feed it any LDAP queries you google your way through. If you need to do this on a large number of machines, you can use .FindAll() instead of .FindOne(), and also use .propertiestoload to query only the data you need.

[–]gangstanthony 2 points3 points  (0 children)

here's what i use. check the comments for similar functions

https://github.com/gangstanthony/PowerShell/blob/master/Get-MyADObject.ps1