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

Python通过PsExec远程读取注册表获取Pilot-ICE缓存路径问题

PsExec环境无法读取目标注册表项的解决方案

核心原因:HKEY_CURRENT_USER(HKCU)是与当前进程关联的用户安全令牌绑定的注册表蜂巢,默认用PsExec启动进程时,会以SYSTEM账户或指定的本地系统账户上下文运行,加载的是对应账户的HKCU蜂巢,而非实际登录使用Pilot-ICE的用户的配置,因此只能读到机器级的安装标记installed键值,看不到用户级的System子项。

可按以下优先级选择解决方法:

  • 最适配自动化场景的方案:放弃直接读取HKCU,改为遍历HKEY_USERS根键下所有用户SID对应的子项,逐个查询SID\SOFTWARE\ASCON\Pilot-ICE Enterprise\System路径下的CurrentCachePath值。只要脚本运行时持有管理员权限,无需匹配登录会话即可读取所有用户的配置,完全不受PsExec启动上下文影响。
  • 若必须读取当前交互式登录用户的HKCU:启动PsExec时添加-i参数指定对应用户的会话ID(可通过远程执行query user命令获取会话ID,本地控制台登录默认ID为1),同时携带目标用户的账号密码参数,确保进程加载对应用户的注册表蜂巢。启动命令参考:
    psexec \\<远程机器IP> -i <会话ID> -u <用户名> -p <密码> C:\Users\<username>\AppData\Local\Programs\Python\Python310\pythonw.exe C:\PilotCleaner\cleaner.py
    
  • 优先使用Python标准库winreg操作注册表,不要通过subprocess调用reg命令:原生Win32 API的权限兼容性更好,无需处理外部命令的编码、输出格式问题,稳定性远高于解析命令行返回值。
稳定提取缓存路径的实现方法

之前通过空格分割字符串的方案存在本质缺陷:缓存路径本身允许包含空格(比如示例路径中的Pilot-ICE Enterprise段),靠空格分割必然出现截断错误。以下两种方案可实现稳定提取:

最优方案:winreg直接读取键值(无解析误差)

直接通过原生API读取目标值,完全跳过字符串解析步骤,同时可直接获取完整路径、按需提取盘符+一级目录:

import winreg
import os

def get_pilot_cache_info(target_sid: str = None) -> dict | None:
    """
    读取Pilot-ICE缓存路径
    :param target_sid: 指定用户SID则读对应用户HKEY_USERS下的配置,不传则读当前用户HKCU
    """
    if target_sid:
        root_key = winreg.HKEY_USERS
        key_path = f"{target_sid}\\SOFTWARE\\ASCON\\Pilot-ICE Enterprise\\System"
    else:
        root_key = winreg.HKEY_CURRENT_USER
        key_path = r"SOFTWARE\ASCON\Pilot-ICE Enterprise\System"
    
    try:
        with winreg.OpenKey(root_key, key_path, 0, winreg.KEY_READ) as reg_key:
            full_cache_path, _ = winreg.QueryValueEx(reg_key, "CurrentCachePath")
            # 提取盘符+一级目录用于路径检测
            drive_letter, path_tail = os.path.splitdrive(full_cache_path)
            first_level_dir = path_tail.strip(os.sep).split(os.sep)[0]
            detect_base_path = os.path.join(drive_letter + os.sep, first_level_dir)
            
            return {
                "full_cache_path": full_cache_path,
                "detect_base_path": detect_base_path
            }
    except FileNotFoundError:
        # 对应路径不存在时返回None
        return None

兼容方案:解析reg命令返回值

如果必须使用subprocess调用reg命令,需注意三个要点:一是查询时直接指定目标键路径和值名,不要加/s递归查询,减少无关返回内容;二是中文Windows系统reg命令默认返回GBK编码,不要用utf-8解码;三是解析时不要按空格分割,通过正则匹配跳过值名、类型字段,行尾剩余内容即为完整路径:

import subprocess
import re
import os

def get_pilot_cache_info_via_cmd() -> dict | None:
    try:
        reg_output = subprocess.check_output(
            r'Reg Query "HKEY_CURRENT_USER\SOFTWARE\ASCON\Pilot-ICE Enterprise\System" /v CurrentCachePath',
            shell=True,
            stderr=subprocess.STDOUT
        ).decode("gbk", errors="ignore")
    except subprocess.CalledProcessError:
        return None
    
    for line in reg_output.splitlines():
        line = line.strip()
        if line.startswith("CurrentCachePath"):
            path_match = re.match(r"CurrentCachePath\s+REG_SZ\s+(.*)", line)
            if path_match:
                full_cache_path = path_match.group(1)
                drive_letter, path_tail = os.path.splitdrive(full_cache_path)
                first_level_dir = path_tail.strip(os.sep).split(os.sep)[0]
                detect_base_path = os.path.join(drive_letter + os.sep, first_level_dir)
                return {
                    "full_cache_path": full_cache_path,
                    "detect_base_path": detect_base_path
                }
    return None

提示:如果需要遍历所有用户的缓存路径,可先通过winreg枚举HKEY_USERS下所有子项的SID,循环调用上述方法传入SID即可,无需依赖PsExec的会话匹配。

内容的提问来源于stack exchange,提问作者Sciti The Candyborn

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 11:39:41