开启64位PowerShell时Intune修正脚本执行失败求助
Winget应用更新脚本64位PowerShell执行失败排查方向
问题描述
我获取了基于Winget的应用更新脚本,说明需以64位PowerShell运行,但开启该选项后脚本执行失败且无详细报错。多个32位脚本可正常运行,推测问题出在配置而非脚本本身,希望了解排查方向。
现有脚本
检测脚本
$app_2upgrade = "7zip.7zip" $Winget = Get-ChildItem -Path (Join-Path -Path (Join-Path -Path $env:ProgramFiles -ChildPath "WindowsApps") -ChildPath "Microsoft.DesktopAppInstaller*_x64*\winget.exe") if ($(&$winget upgrade) -like "* $app_2upgrade *") { Write-Host "Upgrade available for: $app_2upgrade" exit 1 # upgrade available, remediation needed } else { Write-Host "No Upgrade available" exit 0 # no upgraded, no action needed }
修正脚本
$app_2upgrade = "7zip.7zip" try{ $Winget = Get-ChildItem -Path (Join-Path -Path (Join-Path -Path $env:ProgramFiles -ChildPath "WindowsApps") -ChildPath "Microsoft.DesktopAppInstaller*_x64*\winget.exe") &$winget upgrade --exact $app_2upgrade --silent --force --accept-package-agreements --accept-source-agreements exit 0 }catch{ Write-Error "Error while installing upgrade for: $app_2upgrade" exit 1 }
排查方向
Winget路径获取逻辑问题
WindowsApps目录权限严格,64位环境下遍历该目录可能因权限不足导致找不到Winget。建议替换路径获取方式:# 改用Get-Command获取Winget路径,更可靠 $Winget = (Get-Command winget.exe -ErrorAction Stop).Source同时添加空值判断,避免后续执行报错:
if (-not $Winget) { Write-Error "未找到64位Winget程序" exit 1 }执行权限与上下文验证
- 部分应用升级需要管理员权限,在脚本开头添加
#Requires -RunAsAdministrator强制要求管理员权限,同时检查Proactive Remediations的执行账户是否为管理员/系统账户。 - 在检测脚本中添加架构验证代码,确认是否真的运行在64位环境:
Write-Host "当前PowerShell架构:$([Environment]::Is64BitProcess)"
- 部分应用升级需要管理员权限,在脚本开头添加
错误捕获机制补全
- 原修正脚本的
try/catch无法捕获Winget外部程序的执行错误,需通过$LASTEXITCODE判断执行结果:&$winget upgrade --exact $app_2upgrade --silent --force --accept-package-agreements --accept-source-agreements if ($LASTEXITCODE -ne 0) { Write-Error "Winget升级失败,退出码:$LASTEXITCODE" exit 1 } exit 0 - 为所有路径操作添加
-ErrorAction Stop,明确捕获路径访问失败的错误。
- 原修正脚本的
Proactive Remediations配置检查
- 确认"Run script in 64-bit PowerShell"选项已正确开启,有时配置可能因策略同步问题未生效。
- 检查部署的设备组、分配范围是否正确,确保目标设备已接收到策略。
Winget版本与手动验证
- 手动在64位PowerShell中更新Winget:
winget upgrade Microsoft.DesktopAppInstaller,避免旧版本兼容性问题。 - 直接在64位PowerShell中执行脚本中的Winget命令,复现错误并获取详细输出,定位具体问题。
- 手动在64位PowerShell中更新Winget:
内容的提问来源于stack exchange,提问作者Daņiils Poļakovs
相关产品推荐
相关产品推荐

