I'm trying to find my workGroup name in python, does anybody know a function for that?
How do I get my pc's windows Workgroup name in python
718 Views Asked by אביב נח At
2
There are 2 best solutions below
0
On
import wmi
wmi_os = wmi.Win32_ComputerSystem()[0]
print(wmi_os.Workgroup)
You also can go further and take a look at 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'])
Some of them might be useful for you
I was looking for the exact thing and I could not find a direct way through Python. Instead I had to use
subprocessto run aPowerShellcommand:You can also just make it a one-liner by appending
stdout.strip()directly tosubprocess():Result:
I've included the string method
strip()to remove the trailing\ncharacters thatSTDOUTproduces.The PowerShell command
(Get-CimInstance Win32_ComputerSystem).Domainretrieves either the domain name that the computer is joined to, or just the workgroup name if it's not joined to a domain.NOTE:
I saw older suggestions on StackOverflow using the PowerShell command
Get-WmiObject, which only works with PowerShell 5.1. Microsoft recommends using the newerGet-CimInstance, which works with PowerShell 5.1 and newer (ie: PowerShell Core)