-1

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

  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Feb 12 '22 at 09:08
  • I appreciate if you can precisely point where did i violate those instructions and what is a correct example in my case, thanks. – MrLinkOwln Feb 12 '22 at 09:20
  • You can't expect us to provide a whole solution, from start to end. What research have you made yourself? What attempts have you made yourself? How did your attempts work or not work? You have to show at least *some* effort at solving it yourself. – Some programmer dude Feb 12 '22 at 09:26
  • Well i searched a lot and all I could get was solutions like `True if domain or workgroup else False` . But what is want is more like `True if domain else False` , I also answered a [question](https://stackoverflow.com/questions/60763666/how-do-i-get-my-pcs-windows-workgroup-name-in-python/71090170#71090170) but what I need is differing Workgroup from Domain, and I cant provide more of my solution cause it will only create divergent , question is simple enough, anyways thanks for sharing , Ill try to edit and explain a bit more about what i did if it helps – MrLinkOwln Feb 12 '22 at 09:39

1 Answers1

0

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
JosefZ
  • 28,460
  • 5
  • 44
  • 83