如何使用 PowerShell 检查计算机是否已连接到域?

powershellmicrosoft technologiessoftware & coding

要检查计算机是否已连接到任何域,我们可以使用多种方法。在本文中,我们将使用两种方法。一种是使用计算机的系统信息,另一种是使用DirectoryServices .Net 类。

第一种方法使用系统信息,并过滤出名为"Domain"的字符串,该字符串显示计算机是否位于域或工作组中。

systeminfo|findstr"Domain"

输出

如果计算机在工作组中,它将显示工作组名称。例如:

在第二种方法中,我们将使用目录服务 .Net 类方法名 GetComputerDomain()。如果服务器未连接到域,则此命令将抛出错误。

对于已连接到域的服务器,

[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()

输出

Forest                  : labdomain.local
DomainControllers       : {ADDC.labdomain.local}
Children                : {}
DomainMode              :
Parent                  :
PdcRoleOwner            : ADDC.labdomain.local
RidRoleOwner            : ADDC.labdomain.local
InfrastructureRoleOwner : ADDC.labdomain.local
Name                    : labdomain.local

您可以使用 Name 属性过滤域名。

[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain() | Select  Name

输出

Name
----
labdomain.local

如果服务器未连接到域,则会抛出错误,提示本地计算机未加入任何域,如下所示。


相关文章