无法在非交互模式运行PowerShell命令:PSWindowsUpdate执行问题求助
在Salt Stack中以非交互模式运行PSWindowsUpdate的Download-WindowsUpdate命令
问题描述
使用Salt Stack运行PSWindowsUpdate模块时,执行Download-WindowsUpdate命令遇到以下错误:
Download-WindowsUpdate : Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.
现有Salt配置(windows_update.sls)
Install_PSWindowsUpdate: cmd.run: - shell: powershell - name: Install-Module -Name PSWindowsUpdate -Force Import_PSWindowsUpdate_module: cmd.run: - shell: powershell - name: Import-Module PSWindowsUpdate Download_Windows_updates: cmd.run: - shell: powershell - name: Download-WindowsUpdate -Download -ForceDownload -Silent
状态运行错误输出
---------- ID: Download_Windows_updates Function: cmd.run Name: Download-WindowsUpdate -Download -ForceDownload -Silent -Debuger Result: False Comment: Command "Download-WindowsUpdate -Download -ForceDownload -Silent " run Started: 02:31:11.656532 Duration: 6433.988 ms Changes: ---------- pid: 4764 retcode: 1 stderr: Download-WindowsUpdate : Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available. At line:1 char:1 + Download-WindowsUpdate -Download -ForceDownload -Silent + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WindowsUpdate], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,PSWindowsUpdate.GetWindowsUpdate stdout: Summary for prod1
补充信息
直接通过CMD运行该命令时,会弹出确认提示:
C:\Windows\system32>powershell.exe "Download-WindowsUpdate -Download -ForceDownload -Silent -Recurse 2" 确认 是否确实要执行此操作? 正在目标"PROD1"上执行操作"(4/3/2023 4:30:40 AM) 适用于Windows Server 2019 x64的.NET Framework 3.5、4.7.2和4.8的2023-02累积更新(KB5022782)[77MB]"。 [Y] 是 [A] 全是 [N] 否 [L] 全否 [S] 暂停 [?] 帮助 (默认值为"Y"):
解决方法
问题根源是命令需要用户确认操作,但Salt Stack运行PowerShell时处于非交互模式,无法处理这类提示。只需在Download-WindowsUpdate命令中添加-Confirm:$false参数,强制跳过所有确认步骤即可:
修改后的Download_Windows_updates状态块:
Download_Windows_updates: cmd.run: - shell: powershell - name: Download-WindowsUpdate -Download -ForceDownload -Silent -Confirm:$false
执行修改后的Salt状态,命令就能在非交互模式下正常下载更新,不会再触发确认提示或报错。
内容的提问来源于stack exchange,提问作者Robert
相关产品推荐
相关产品推荐

