Get a Domain Controller from any domain member

I needed to find an available domain controller in order to remote Powershell and use some AD modules. Didn’t want to start installing them on random machines. So how to find a DC? If you RDP, you can just write the domain name and it finds the first available DC. However that didn’t work for my PSSession, so what to do.

Turns out there was a neet oneliner for that 🙂

[System.Net.Dns]::GetHostAddresses($(gwmi WIN32_ComputerSystem).Domain)[0].IPAddressToString

Returns the IP Adress of the first available DC, which works, but not so good. As it’s impossible to use Kerberos when using IP, well this can be turned into host name also in a one liner:

([system.net.dns]::GetHostByAddress(([System.Net.Dns]::GetHostAddresses($(gwmi WIN32_ComputerSystem).Domain)[0].IPAddressToString))).hostname

Gotta love great colleagues !

Advertisements

3 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s