0

I am using the following perform a WMI query on a windows endpoint which returns the results in a list. I want to now convert that list to a dictionary key:value so that I can search for all keys with "Name" as the name which will return: "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'}
4

2 に答える 2

0

の構造が次の場合output:

[{'Status': 'OK', 'Domain': 'localhost', 'Description': 'Account used for running the ASP.NET worker process (aspnet_wp.exe                      )', ...,  'Name': 'ASPNET'},
 {'Status': 'Degraded', 'Domain': 'localhost', 'Description': 'Built-in account for guest access to the computer/domain', ..., 'Name': 'Guest'},
 {'Status': 'OK', 'Domain': 'localhost', 'Description': 'Built-in account for administering the computer/domain',  ..., 'Name': 'Admin'}]

辞書にあるキーをキーにして、リストの辞書に繰り返します。

output = wmic.query("Select * from Win32_UserAccount Where LocalAccount = True")

new_dict = {}
for key in output[0]:
    new_dict[key] = [old_dict[key] for old_dict in output]

これは、2 つのリスト内包表記とdict組み込み関数を使用して 1 行で実行することもできます。

new_dict = dict([(key, [old_dict[key] for old_dict in output])
                 for key in output[0]])

また、辞書内包表記がある場合は、次のいずれかを使用できます。

new_dict = {key: [old_dict[key] for old_dict in output]
            for key in output[0]}

これらのそれぞれは、 の各結果辞書と同様にキー付けされた辞書を提供しoutputます。のディクショナリのすべての値がoutput、元のディクショナリで見つかったキーのリストに表示されます。に登場した順に並べていoutputます。

{'Status': ['OK', 'Degraded', 'OK'], 
 'Domain': ['localhost', 'localhost', 'localhost'],
 'Description': ['Account used for running the ASP.NET worker process (aspnet_wp.exe)', 
                 'Built-in account for guest access to the computer/domain',
                 'Built-in account for administering the computer/domain'],
 'InstallDate': [None, None, None],
 'Caption': ['localhost\\ASPNET', 'localhost\\Guest', 'localhost\\sol2112'],
 'Disabled': [False, True, False],
 'PasswordChangeable': [False, False, True],
 'Lockout': [False, False, False],
 'AccountType': ['512', '512', '512'],
 'SID': ['45474748484848-1002',
         '3645747474747858-501',
         '834668384636846843-500'], 
 'LocalAccount': [True, True, True],
 'FullName': ['ASP.NET Machine Account', '', ''], 
 'SIDType': ['1', '1', '1'], 
 'PasswordRequired': [False, False, True],
 'PasswordExpires': [False, False, False],
 'Name': ['ASPNET', 'Guest', 'Admin']}
于 2013-08-26T15:37:18.520 に答える
0

あなたはただすることができます

>>> dict1 = {'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'}
>>> print dict1['Name']
ASPNET
于 2013-08-26T14:59:54.577 に答える