Is there an easy and fast way to find out if the local computer is joined to a domain with PowerShell?
I find lots of stuff about getting the current workgroup OR domain but no clear indications on how to know if it really is a workgroup or domain. Just want to find this out before calling some Active Directory related stuff that only gives a timeout after a long wait on workgroup computers.
The [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
method takes a long time before failing on workgroup computers and the WMI class 开发者_开发知识库Win32_ComputerSystem
shows the name but not if it is a real domain.
Win32_ComputerSystem
has a PartOfDomain
property that indicates whether the computer is domain joined or not. There is also a workgroup property - that should be blank if the computer is on a domain.
Example:
if ((gwmi win32_computersystem).partofdomain -eq $true) {
write-host -fore green "I am domain joined!"
} else {
write-host -fore red "Ooops, workgroup!"
}
Maybe type 'Systeminfo' in PowerShell and look under 'Domain' ?
This is simple. Works because if a computer is not in a domain, the default userdomain is the local computer
if ($env:computername -eq $env:userdomain) { echo " no AD domain" }
else { echo "must be in AD"}
精彩评论