Error Windows Update Installation via PowerShell Script using .msu files

Muhammad Anwar 21 Reputation points
2021-08-07T06:56:33.967+00:00

Hi,

I am trying to update windows using following script via .msu file but get following error, please help me to fix this issue.

Get-HotFix : A positional parameter cannot be found that accepts argument 'kb4562030.msu'.
At line:7 char:19

  • if (Get-Hotfix <<<< -Id -eq $update) {
  • CategoryInfo : InvalidArgument: (:) [Get-HotFix], ParameterBindingException
  • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetHotFixCommand

Get-HotFix : A positional parameter cannot be found that accepts argument 'kb978542.msu'.
At line:7 char:19

  • if (Get-Hotfix <<<< -Id -eq $update) {
  • CategoryInfo : InvalidArgument: (:) [Get-HotFix], ParameterBindingException
  • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetHotFixCommand

============================================================

PowerShell Script:

$UpdatePath = "C:\Updates"
Get-HotFix > "$UpdatePath\Old_HotFix_List.txt"
$Updates = Get-ChildItem -Path $UpdatePath | Where-Object {$_.Name -like "*.msu"}
ForEach ($update in $Updates)
{
$UpdateFilePath = $update.FullName
if (Get-Hotfix -Id -eq $update) {
Write-Host " Windows Update Already Installed on Computer "
}
else
{
write-host "Installing Update on Computer $update"
Start-Process -wait wusa -ArgumentList "/update $UpdateFilePath","/quiet","/norestart"
}
}
Get-HotFix > "$UpdatePath\New_HotFix_List.txt"
timeout 5
shutdown /r

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,906 Reputation points
    2021-08-07T18:27:28.103+00:00

    I haven't run this code but I think it should do what you're trying to accomplish:

    $UpdatePath = "C:\Updates"
    Get-HotFix > "$UpdatePath\Old_HotFix_List.txt"  
    $installed = (Get-HotFix).HotFixID
    Get-ChildItem -Path $UpdatePath -Include *.msu | 
        ForEach-Object{
            if ($_.Name -match "^.+-(kb[^-]+)-"){   # name looks like "windows10.0-kb5003537-x86-ndp48_0f7a496cc7e0dc08f0494e8c9e3c6a83a9175e97.msu"
                $kbid = $matches[1]
                if ($installed -contains $kbid)
                {
                    Write-Host "Windows Update '$kbid' Already Installed on Computer "
                }
                else{
                    $UpdateFilePath = $_.FullName
                    write-host "Installing Update '$kbid' on Computer $update"
                    Start-Process -wait wusa -ArgumentList "/update $UpdateFilePath","/quiet","/norestart"
                }
            }
            else{
                Write-Host "Did not find KB number in the filename '$_'"
            }
        }
    Get-HotFix > "$UpdatePath\New_HotFix_List.txt"
    timeout 5
    shutdown /r
    

    Note that the variable "$update" (which should hold the name of the computer on which to run "wsua") is undefined. You should also check that "wsua" is sufficient to start the process.


  2. Muhammad Anwar 21 Reputation points
    2021-08-10T09:18:42.77+00:00

    Hi Rich,

    now its working, but still have 01 issue as the if condition "Line No: 6 & 8" not working, the installation process starts didn't prompt any massage as mentioned in script if the update is already installed.

    121935-image.png