dsh-powershell:讓 DSH Agent 直接跑 PowerShell 命令

前言

在 Windows 上讓 DeepSeek Harness(DSH)裏的 agent 執行系統操作,常見做法是寫 .ps1 再調用。一條簡單的查詢也要先落盤腳本,交互成本高。內置工具有 pwsh,但同名註冊會搶插件、且 elevated: true 會被靜默忽略,提權失敗看起來像普通權限報錯,排查困難。

dsh-powershell 由維護者 a5899 發佈,分類爲 admin-security,面向「命令行優先」:agent 在工具參數裏直接寫 PowerShell,需要管理員時用獨立的提權工具,長任務用輪詢式後臺任務看到增量輸出。下面介紹定位、工具集、安裝與典型用法。

這是什麼

一句話:dsh-powershell 是 DSH 原生插件,在 agent 側用 code 內聯 PowerShell(不必先寫腳本文件),提供普通執行、UAC 提權、長命令輪詢、提權進程終止與狀態查詢,並可在配置中開啓「優先命令而非腳本」的策略提示。

Linux / macOS 上插件可加載,但提權相關能力會返回 unsupported-platform(UAC 爲 Windows 專有)。

爲什麼叫 psh,而不是再註冊 pwsh

DSH 自帶內置工具 pwsh,插件若用同名註冊,本插件其餘工具會出現 unknown tool,整體不生效;內置 pwsh 接受 elevated: true 卻靜默忽略,危險在於「該提權卻像普通失敗」。因此主工具命名爲 psh,提權單獨爲 psh_elevated,不與內置工具撞名。

作者也說明:並非用 MCP 實現提權——另一進程無法在同一架構下可靠承接 UAC 會話與輸出;本插件在 DSH 進程內註冊工具,提權走 Start-Process -Verb RunAs,經臨時目錄文件信箱回收 stdout/stderr。

核心功能:五個工具

工具 作用
psh 主工具。code 內寫 PowerShell(可多行)。普通權限,每次調用新進程。
psh_elevated UAC 提權執行;輸出邊跑邊寫回。用戶取消 UAC(1223)如實上報,不重試。
psh_job 長命令:後臺跑 + 按字節游標 poll,運行中可讀輸出。
psh_kill 通知提權進程自行退出(低權限無法直接終止高權限進程)。
psh_status 查詢是否已提權、當前 shell、腳本塊日誌等,避免猜測。

策略說明(README 原文):「命令行優先」不等於禁止腳本——邏輯長、要複用、計劃任務或版本管理時仍應寫腳本再用 psh 執行;不應把數百行塞進 code。

DSH 工具接口與 psh_job

DSH 工具定義爲單次 execute 返回的 Promise,沒有內置增量流。因此短命令用 psh 即可;耗時命令用 psh_job 拆成多次短調用:

psh_job action=start code="..." timeoutMs=0   → 立刻返回 id
psh_job action=poll  id=... offset=0          → 新輸出 + 下一 offset
psh_job action=poll  id=... offset=<上次返回>  → 直到 done: true

輸出落在 %TEMP%\dsh-powershell-jobs\<id>\ 下的 stdout.txt / stderr.txt;單次 poll 最多 64 KiB並在換行邊界切分;另有 action=list、action=stop 等。短命令不必用 psh_job。

配置項

在 profile 的 cordis.patch.yml 中插入(節選,與 README 一致):

- insert:
    - id: dsh-powershell
      name: './node_modules/dsh-powershell/lib/index.js'
      config:
        enabled: true
        preferCommands: true          # 向系統提示注入「命令行優先」策略
        shell: auto                   # auto | powershell | pwsh
        timeoutMs: 120000             # 普通命令超時
        maxOutputChars: 200000        # 輸出截斷上限
        elevateTimeoutMs: 300000      # 提權總預算(含等待 UAC)

name 必須是相對路徑 ./node_modules/dsh-powershell/lib/index.js,寫裸包名 dsh-powershell 會在冷啓動時被靜默跳過,表現爲 DSH 正常啓動但工具不可用。

安裝與啓用

官方 README 給出的步驟如下(在 Windows / PowerShell 環境按順序做):

# 1. 鏈接進 profile
New-Item -ItemType Junction -Path "$env:USERPROFILE\.dsh\profiles\desktop\node_modules\dsh-powershell" -Target "<本倉庫路徑>"

# 2. 加進 profile 的 bundles
#    ~/.dsh/profiles/desktop/package.json -> dsh.profile.bundles 里加 "dsh-powershell"

# 3. 在 profile 的 cordis.patch.yml 末尾追加(注意 name 是相對路徑!)
#    - insert:
#        - id: dsh-powershell
#          name: './node_modules/dsh-powershell/lib/index.js'
#          config: { enabled: true, preferCommands: true }

# 4. 重啓 DSH

安裝前應在 GitHub 閱讀源碼與 LICENSE,確認你願意以當前 DSH 進程權限加載該插件;junction 安裝時插件需自帶 @deepseek-ai/schemastery 依賴,否則 Config 校驗可能靜默失敗。

加載失敗時看 load.log

插件在 ~/.dsh/plugins/dsh-powershell/load.log 記錄加載階段:無日誌則模塊未 import;停在 import-evaluated 多因 Config 非 schemastery schema;register-failed 會帶工具名與錯誤。不要僅憑「有沒有 pwsh」判斷——內置 pwsh 會與插件混淆觀感。

典型用法示意

普通權限執行(概念示例,實際由 agent 調用工具 API):

psh  code="Get-ComputerInfo | Select-Object CsName, OsName"

需要管理員且接受 UAC 彈窗時使用 psh_elevated,不要用內置 pwsh 的 elevated: true。

長任務(README 模式):

psh_job action=start code="..." timeoutMs=0
psh_job action=poll  id=<返回的 id> offset=0

結束前執行敏感操作前,可先 psh_status 確認 elevation 與 shell 配置。

README 中在 Windows 11 / PowerShell 5.1 上的實測包括:psh 退出碼原樣返回、psh_elevated 下 BitLocker 查詢等需管理員的操作。具體數值以倉庫文檔爲準。

適用場景與注意

適合:在 Windows 上以 DSH agent 做本機管理、安全相關排查(admin-security 分類)、需要明確 UAC 提權邊界、希望默認內聯命令而非隨手寫臨時腳本的 profile。

注意:

  1. 插件工具在 DSH 宿主進程權限下運行;提權僅在使用 psh_elevated 且用戶確認 UAC 後成立。
  2. 提權實現依賴臨時文件信箱、UTF-16LE 的 -EncodedCommand 約束等,詳見 README「設計上必須知道的四件事」。
  3. psh_kill 通過 run 目錄下 kill-request 哨兵讓高權限進程自退,不依賴調高系統策略。
  4. 社區目錄 SkillHub 與 DeepSeek / 幻方無官方從屬關係;DSH 生態理念是「一切皆插件」,安裝來源以你覈對過的倉庫與 patch 爲準。

結尾

dsh-powershell 把 Windows 上的 PowerShell 執行收成一組語義清晰的 DSH 工具:普通 psh、顯式 psh_elevated、長任務 psh_job,並避免與內置 pwsh 撞名導致的靜默提權失敗。更多細節與測試命令見 GitHub README,插件索引見 SkillHub 目錄頁。

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

小夜