Search code examples
windowspowershellsalt-projectwindows-update

Unable to run Powershell commands in non-interactive mode


I'm trying to run PSWindowsUpdate using salt stack but I can't run the Download-WindowsUpdate command due to the following error:

Download-WindowsUpdate : Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.

How can I make this work in non-interactive mode?

Here is my windows_update.sls file:

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 

Here is the output of state run:

----------
          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

Update: If I try to run the command through cmd then It is prompting for Yes or No.

C:\Windows\system32>powershell.exe "Download-WindowsUpdate -Download -ForceDownload -Silent -Recurse 2"

Confirm
Are you sure you want to perform this action?
Performing the operation "(4/3/2023 4:30:40 AM) 2023-02 Cumulative Update for .NET Framework 3.5, 4.7.2 and 4.8 for
Windows Server 2019 for x64 (KB5022782)[77MB]" on target "PROD1".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

Solution

  • Adding -Confirm:$false seems to have fixed the issue.

    Download_Windows_updates:
      cmd.run:
      - shell: powershell
      - name: Download-WindowsUpdate -Download -ForceDownload -Silent -Confirm:$false