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

Windows 11服务程序调用PSWindowsUpdate返回Null问题求助

问题描述

我有一个以服务形式运行的C++应用,它调用PowerShell命令获取Windows更新信息并写入JSON文件。应用通过VBS脚本安装,脚本会自动导入PSWindowsUpdate模块。在Windows 10及其他系统上一切正常,但在Windows 11机器上,Get-WindowsUpdate命令返回Null。

补充信息:

  • 该命令在非服务应用中或直接在PowerShell控制台执行均正常
  • 服务拥有同等甚至更高权限,其他PowerShell命令均可正常运行
  • 已联系PSWindowsUpdate模块作者但未收到回复

VBS模块导入代码片段:

If Not (oFSO.FolderExists(strModulePath)) Then
    objFile.Write FormatDateTime(Now) & " Installing PSWU" & vbCrLf
    oFSO.CreateFolder strModulePath
    oFSO.CopyFile psdir, strModulePath
    objFile.Write FormatDateTime(Now) & " Création du PSWU - force" & vbCrLf
End If
objFile.Write FormatDateTime(Now) & " Importing the new module" & vbCrLf
objShell.Run("powershell.exe -Command ""Import-Module PSWindowsUpdate""")

正常运行的PowerShell命令及输出:

ConvertTo-Json @(Get-WindowsUpdate  | ForEach-Object { $_ | Select Title, KB })

输出示例:

[
    {
        "Title":  "2023-05 Aperçu de la mise à jour cumulative de .NET Framework 3.5, 4.8 et 4.8.1 Windows 10 Version 22H2 pour x64 (KB5026958)",
        "KB":  "KB5026958"
    },
    {
        "Title":  "AOC International (Europe) GmbH - Monitor, Other hardware - AOC 2475W",
        "KB":  ""
    },
    {
        "Title":  "Intel - Other hardware - Intel(R) Xeon(R) E3 - 1200 v6/7th Gen Intel(R) Core(TM) Host Bridge/DRAM Registers - 590F",
        "KB":  ""
    }
]
可能的解决方向

1. 排查服务权限与会话隔离

服务默认运行在Session 0,和用户交互式会话有环境隔离,Windows 11对这部分控制更严:

  • 确认服务运行账户是Local System或本地管理员账户,别用Network Service这类受限账户
  • 临时开启服务的「允许服务与桌面交互」选项(生产环境不推荐,但能快速排查是否是会话隔离导致)

2. 修正模块导入的上下文问题

VBS里的Import-Module是在单独PowerShell进程执行的,不会同步到C++调用的会话里:

  • 修改C++的PowerShell调用逻辑,在执行Get-WindowsUpdate前显式导入模块并指定完整路径,比如:
    Import-Module "C:\Path\To\PSWindowsUpdate" -Force; ConvertTo-Json @(Get-WindowsUpdate | Select Title, KB)
    
  • 检查模块是否安装到系统级路径(比如C:\Windows\System32\WindowsPowerShell\v1.0\Modules),而不是某个用户的私人模块路径

3. 检查Windows 11的更新服务配置

Windows 11对Windows Update服务的访问规则有调整:

  • 验证服务账户能正常访问wuauserv服务,手动在服务账户下执行wuauclt /detectnow,看更新检测功能是否正常
  • 如果用了WSUS,检查Windows 11的组策略是否正确配置,有没有更新源无法访问的情况

4. 加调试输出抓错误细节

别只看命令的标准输出,把错误输出也捕获到:

  • 修改PowerShell命令,添加2>&1把错误重定向到标准输出,比如:
    powershell.exe -Command "Import-Module PSWindowsUpdate -Force; Get-WindowsUpdate | Select Title, KB | ConvertTo-Json" 2>&1
    
  • 在VBS脚本里记录objShell.Run的返回码(0是成功,非0就是执行失败),确认模块导入环节有没有问题

5. 适配PowerShell版本兼容性

Windows 11默认带PowerShell 7.x和5.1,PSWindowsUpdate对5.1兼容性更好:

  • 明确指定调用powershell.exe(即Windows PowerShell 5.1),别用pwsh.exe(PowerShell 7)
  • 手动更新PSWindowsUpdate到最新版本(如果能获取到的话),确认模块支持Windows 11

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 15:39:53