You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求可将Windows用户SID转换为用户名的WQL查询语句

Sure thing! If you need a WQL query to convert a Windows user SID to a username—whether you're testing with WBEMTest or using a Python WBEM module—here's exactly what you need:

WQL Query to Resolve SID to Username

The Core Query

You can leverage the Win32_UserAccount class (in the root\cimv2 namespace) to map a SID to its associated username. Here's the basic WQL statement:

SELECT Name FROM Win32_UserAccount WHERE SID = 'S-1-5-21-XXXXXXXXX-XXXXXXXXX-XXXXXXXXX-1001'

Just replace the sample SID string with the one you want to resolve. The Name attribute in the results will give you the corresponding username.

Testing with WBEMTest

To verify this query in WBEMTest:

  • Open WBEMTest and connect to the root\cimv2 namespace (local machine is the default if no remote host is specified)
  • Click the Query button in the toolbar
  • Paste the modified query (with your target SID)
  • Click Execute—the results will display an instance where the Name property holds your desired username

Using This in Python (with a WBEM Module)

If you're working with a Python library like pywbem, here's a quick implementation example:

import pywbem

# Connect to the local root\cimv2 namespace using current user credentials
conn = pywbem.WBEMConnection(
    'http://localhost:5985/wsman',
    (None, None),
    'root/cimv2'
)

# Replace with your target SID
target_sid = 'S-1-5-21-XXXXXXXXX-XXXXXXXXX-XXXXXXXXX-1001'

# Build and run the WQL query
query = f"SELECT Name FROM Win32_UserAccount WHERE SID = '{target_sid}'"
results = conn.ExecQuery(query)

# Extract and print the resolved username
for user_instance in results:
    print(f"Resolved Username: {user_instance['Name']}")

Quick Notes

  • Permissions: Ensure you have sufficient privileges to query Win32_UserAccount—domain user resolution may require domain-level access.
  • Filter Local vs. Domain Users: To narrow results to local users only, add a Domain filter:
    SELECT Name FROM Win32_UserAccount WHERE SID = 'S-1-5-21-XXXXXXXXX-XXXXXXXXX-XXXXXXXXX-1001' AND Domain = 'YOUR_LOCAL_MACHINE_NAME'
    

内容的提问来源于stack exchange,提问作者JaminB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:50:57