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

Intune部署PowerShell脚本失败,无法获取Autopilot硬件哈希

问题排查与修复方案

1. 执行上下文与权限限制

Intune Win32应用默认以SYSTEM账户运行,而非本地管理员用户,这会带来两个核心问题:

  • Install-Module/Install-Script默认安装到当前用户模块路径,SYSTEM账户的模块路径与本地管理员不同,后续调用Get-WindowsAutoPilotInfo或Az模块时会找不到命令。
  • 安装模块/脚本时,默认会弹出确认提示(如信任PSGallery仓库),SYSTEM上下文无交互能力,会导致安装卡住。

2. 模块/脚本安装的参数缺失

原脚本未指定安装范围和强制确认参数,需补充调整:

  • 给Install-Module和Install-Script添加-Scope AllUsers,确保所有账户(包括SYSTEM)能访问模块。
  • 添加-AllowClobber和-Force参数,避免版本冲突和交互提示。
  • 提前信任PSGallery仓库,否则安装会失败:Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

3. 文件操作的鲁棒性不足

  • 创建C:\Intune目录时,需添加-Force参数,避免目录已存在时报错中断脚本。
  • 脚本未添加错误处理,任意步骤失败都会导致后续操作终止(包括生成确认文件),需添加-ErrorAction Stop捕获错误,确保失败时能及时终止并暴露问题。

4. Win32应用配置问题

  • 安装命令未添加日志输出,无法排查失败原因,应修改为:
    powershell.exe -ExecutionPolicy Bypass -File .\hashid.ps1 -Verbose > C:\Intune\install.log 2>&1
    
  • 检测规则仅依赖hashidconfirm.txt,无法反映脚本实际执行状态,建议改为检测C:\Intune\install.log存在且日志中包含成功上传的关键词,或结合Azure Blob的文件存在性做验证。

修改后的脚本示例

# 信任PSGallery仓库
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction Stop

# 安装依赖模块与脚本(指定全用户范围)
Install-Module -Name Az.Storage -Force -AllowClobber -Scope AllUsers -ErrorAction Stop
Install-Script -Name Get-WindowsAutoPilotInfo -Force -AllowClobber -Scope AllUsers -ErrorAction Stop

# 定义变量
$StorageAccountName = "XXXXX"
$StorageAccountKey = "XXXXXX"
$ContainerName = "XXXXX"
$Serial = (Get-CimInstance win32_bios -ErrorAction Stop).SerialNumber
$Context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -ErrorAction Stop

# 创建目录(强制覆盖已存在目录)
New-Item -ItemType Directory -Path C:\Intune\ -Force -ErrorAction Stop

# 生成硬件哈希文件
Get-WindowsAutoPilotInfo -Outputfile C:\Intune\$Serial.csv -ErrorAction Stop

# 上传到Blob存储
Set-AzStorageBlobContent -Context $Context -Container $ContainerName -File "C:\Intune\$Serial.csv" -Force -ErrorAction Stop

# 清理本地文件
Remove-Item -Path "C:\Intune\$Serial.csv" -Force -ErrorAction Stop

# 生成确认文件
New-Item -Path "C:\Intune\hashidconfirm.txt" -Force -ErrorAction Stop

额外排查步骤

  1. 在测试设备上手动以SYSTEM账户运行脚本:下载psexec.exe,执行psexec -s powershell.exe,然后运行脚本,观察报错信息。
  2. 检查Azure存储容器的防火墙设置,确保设备所在网络允许访问Blob存储端点。
  3. 验证存储账户密钥的有效性,确保拥有容器的写入权限。

内容的提问来源于stack exchange,提问作者Luís Sebastião

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 11:35:59