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

Time:02-13

I want to check if a computer is in a domain or in a workgroup using python
for example:

def run_check():
    # code here

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

I tried using os.environ['USERDNSDOMAIN'] but it gives either WorkGroupName or DomainName and I cant tell if the name is for a Workgroup or a Domain

Can you help me out?

Thanks in advance

CodePudding user response:

Apply Windows Management Instrumentation (wmi module):

import wmi

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

Note: print(wmi_os) shows all properties (and their values) of the Win32_ComputerSystem class instance.

All properties are 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'])

You could be interested mainly in following ones:

Caption
DNSHostName
Domain
DomainRole
Name
NameFormat
PartOfDomain
Roles
UserName
Workgroup
  • Related