Intune Win32应用PowerShell脚本带参数执行失败求助
解决方案
1. 修复脚本路径定位问题
Intune执行命令时的默认工作目录并非脚本解压后的目录,直接用.\\powershell.ps1会导致文件找不到。改用%~dp0(CMD环境变量,代表当前脚本所在目录)来指定绝对路径:
- 用PowerShell直接调用:
powershell -ExecutionPolicy Bypass -File "%~dp0powershell.ps1" -TargetPaths "1","2","3" - 若通过CMD包裹调用:
cmd /c "powershell -ExecutionPolicy Bypass -File ""%~dp0powershell.ps1"" -TargetPaths ""1"",""2"",""3"""
2. 调整数组参数传递格式
Intune命令行对PowerShell原生数组语法@('1','2')的解析存在兼容性问题,改用逗号分隔的字符串直接传递,脚本无需修改参数定义:
powershell -ExecutionPolicy Bypass -File "%~dp0powershell.ps1" -TargetPaths "1","2","3"
PowerShell会自动将逗号分隔的参数转换为数组,匹配脚本中$TargetPaths=@()的定义。
3. 优先使用-File参数替代-Command
-Command在处理复杂参数时容易出现解析错误,-File模式下参数传递更直接可靠:
powershell -ExecutionPolicy Bypass -File "%~dp0powershell.ps1" -TargetPaths "1","2","3"
4. 添加强制日志排查执行状态
在脚本开头添加日志输出到SYSTEM账户可访问的路径(如C:\Windows\Temp),确认脚本是否启动:
Param([Parameter(Mandatory = $true)] $TargetPaths=@()) $logPath = "C:\Windows\Temp\intune_script_log.txt" "脚本启动时间: $(Get-Date)" | Out-File -FilePath $logPath -Append "接收的参数列表: $($TargetPaths -join '; ')" | Out-File -FilePath $logPath -Append
执行后可查看该日志文件,判断脚本是否被调用、参数是否正确接收。
5. 确认SYSTEM账户权限
Intune以SYSTEM账户执行脚本,确保脚本内的所有操作(如文件写入、注册表修改)都具备SYSTEM权限,避免因权限不足导致脚本静默失败。
内容的提问来源于stack exchange,提问作者ScriptoPoco
相关产品推荐
相关产品推荐

