我正在使用以下命令对windows终结点执行WMI查询,该查询将返回列表中的结果。我现在想把那张单子改成字典关键:值so我可以搜索以“Name”作为名称的所有密钥,它将返回:“ASPNET”“Guest”“Admin”。在
import wmi_client_wrapper as wmi
wmic = wmi.WmiClientWrapper(
username="corp.testdomain.com/Administrator",
password="fakepassword",
host="192.168.1.100",
)
output = wmic.query("Select * from Win32_UserAccount Where LocalAccount = True")
{'Status': 'OK', 'Domain': 'localhost', 'Description': 'Account used for running the ASP.NET worker process (aspnet_wp.exe )', 'InstallDate': None, 'Caption': 'localhost\\ASPNET', 'Disabled': False, 'PasswordChangeable': False, 'Lockout': False, 'AccountType': '512', 'SID': '45474748484848-1002', 'LocalAccount': True, 'FullName': 'ASP.NET Ma chine Account', 'SIDType': '1', 'PasswordRequired': False, 'PasswordExpires': False, 'Name': 'ASPNET'}
{'Status': 'Degraded', 'Domain': 'localhost', 'Description': 'Built-in account for guest access to the computer/domain', ' InstallDate': None, 'Caption': 'localhost\\Guest', 'Disabled': True, 'PasswordChangeable': False, 'Lockout': False, 'Accou ntType': '512', 'SID': '3645747474747858-501', 'LocalAccount': True, 'FullName': '', 'SIDType': '1', 'PasswordRequired': False, 'PasswordExpires': False, 'Name': 'Guest'}
{'Status': 'OK', 'Domain': 'localhost', 'Description': 'Built-in account for administering the computer/domain', 'InstallD ate': None, 'Caption': 'localhost\\sol2112', 'Disabled': False, 'PasswordChangeable': True, 'Lockout': False, 'AccountType ': '512', 'SID': '834668384636846843-500, 'LocalAccount': True, 'FullName': '', 'SIDType': '1', 'Pass wordRequired': True, 'PasswordExpires': False, 'Name': 'Admin'}
你能做到的
如果
output
的结构是:我会将它迭代到一个列表字典中,由字典中的键进行键控:
^{pr2}$这也可以通过两个列表理解和
dict
内置函数在一行中完成:如果你有字典理解能力,你可以使用其中一种:
其中的每一个都将给出一个字典,其键控方式与
output
中的每个结果字典都相同。output
中字典中的所有值都存在于其原始字典中的键的列表中。它们是按它们在output
中出现的顺序排列的。在相关问题 更多 >
编程相关推荐