Wednesday, December 13, 2017

Checking whether the domain is available in PowerShell

One user wanted to know how to run a startup script only if no domain controller was available. My solution was to run a PowerShell script that checks if the domain is reachable. That's easily done by running this line:

[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain() | Out-Null

If it throws an exception, the domain is unavailable. If it proceeds without error, a DC could be contacted. A try/catch block can be used to make decisions based on whether the exception appeared.

No comments:

Post a Comment