如何使用 python 检查计算机是在域中还是在工作组中

How to use python to check if a computer is in a domain or is in workgroup

我想使用 python
检查计算机是在域中还是在工作组中 例如:

def run_check():
    # code here

result = run_check()
if result == "Domain":
    print("Domain")
elif result == "Workgroup":
    print("Workgroup")
else:
    print("None")

我尝试使用 os.environ['USERDNSDOMAIN'],但它给出了 WorkGroupNameDomainName,我无法判断该名称是否适用于工作组或域

你能帮帮我吗?

提前致谢

应用Windows Management Instrumentationwmi模块):

import wmi

wmi_os = wmi.WMI().Win32_ComputerSystem()[0]
print( wmi_os.Name, 'PartOfDomain?', wmi_os.PartOfDomain)
MY-PC PartOfDomain? False

注意:print(wmi_os) 显示 Win32_ComputerSystem class 实例的所有属性(及其值)。

所有属性都是print(wmi_os.__dict__['properties'].keys())

dict_keys(['AdminPasswordStatus', 'AutomaticManagedPagefile', 'AutomaticResetBootOption', 'AutomaticResetCapability', 'BootOptionOnLimit', 'BootOptionOnWatchDog', 'BootROMSupported', 'BootStatus', 'BootupState', 'Caption', 'ChassisBootupState', 'ChassisSKUNumber', 'CreationClassName', 'CurrentTimeZone', 'DaylightInEffect', 'Description', 'DNSHostName', 'Domain', 'DomainRole', 'EnableDaylightSavingsTime', 'FrontPanelResetStatus', 'HypervisorPresent', 'InfraredSupported', 'InitialLoadInfo', 'InstallDate', 'KeyboardPasswordStatus', 'LastLoadInfo', 'Manufacturer', 'Model', 'Name', 'NameFormat', 'NetworkServerModeEnabled', 'NumberOfLogicalProcessors', 'NumberOfProcessors', 'OEMLogoBitmap', 'OEMStringArray', 'PartOfDomain', 'PauseAfterReset', 'PCSystemType', 'PCSystemTypeEx', 'PowerManagementCapabilities', 'PowerManagementSupported', 'PowerOnPasswordStatus', 'PowerState', 'PowerSupplyState', 'PrimaryOwnerContact', 'PrimaryOwnerName', 'ResetCapability', 'ResetCount', 'ResetLimit', 'Roles', 'Status', 'SupportContactDescription', 'SystemFamily', 'SystemSKUNumber', 'SystemStartupDelay', 'SystemStartupOptions', 'SystemStartupSetting', 'SystemType', 'ThermalState', 'TotalPhysicalMemory', 'UserName', 'WakeUpType', 'Workgroup'])

您可能主要对以下内容感兴趣:

Caption
DNSHostName
Domain
DomainRole
Name
NameFormat
PartOfDomain
Roles
UserName
Workgroup