Skip to content

Commit

Permalink
Fix bug where local plus repository path installs more than expected (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee authored Sep 30, 2022
1 parent c7fb5cf commit 0d8d863
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 53 deletions.
4 changes: 2 additions & 2 deletions private/Invoke-KbCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ function Invoke-KbCommand {

try {
if (-not (Get-PSFConfigValue -Name PSRemoting.Sessions.Enable) -or $computer.IsLocalhost) {
Write-PSFMessage -Level Verbose -Message "Sessions disabled, just using Invoke-Command"
Write-PSFMessage -Level Debug -Message "Sessions disabled or computer is localhost, just using Invoke-Command"
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList
} else {
Write-PSFMessage -Level Verbose -Message "Sessions enabled, using Invoke-PSFCommand"
Write-PSFMessage -Level Debug -Message "Sessions enabled, using Invoke-PSFCommand"
$null = Get-PSSession | Where-Object { $PSItem.Name -eq "kbupdate-$ComputerName" -and $PSItem.State -eq "Broken" } | Remove-PSSession
$session = Get-PSSession | Where-Object Name -eq "kbupdate-$ComputerName"

Expand Down
40 changes: 27 additions & 13 deletions private/Start-DscUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ function Start-DscUpdate {
$InputObject = Get-KbNeededUpdate -ComputerName $ComputerName
}
}

if ($HotfixId) {
Write-PSFMessage -Level Verbose -Message "Hotfix detected, getting info"
$InputObject += Get-KbUpdate -HotfixId $HotfixId -ComputerName $ComputerName
}

if ($FilePath) {
$InputObject += Get-ChildItem -Path $FilePath
}
Expand Down Expand Up @@ -124,10 +130,10 @@ function Start-DscUpdate {
}
if ($IsLocalHost) {
Write-PSFMessage -Level Verbose -Message "Copying xWindowsUpdate to $hostname (local to $programfiles\WindowsPowerShell\Modules\xWindowsUpdate)"
$null = Copy-Item -Path "$script:ModuleRoot\library\xWindowsUpdate" -Destination "$programfiles\WindowsPowerShell\Modules" -Recurse -Force
$null = Copy-Item -Path "$script:ModuleRoot\library\xWindowsUpdate" -Destination "$programfiles\WindowsPowerShell\Modules" -Recurse -Force -ErrorAction Stop
} else {
Write-PSFMessage -Level Verbose -Message "Copying xWindowsUpdate to $hostname (remote to $programfiles\WindowsPowerShell\Modules\xWindowsUpdate)"
$null = Copy-Item -Path "$script:ModuleRoot\library\xWindowsUpdate" -Destination "$programfiles\WindowsPowerShell\Modules" -ToSession $remotesession -Recurse -Force
$null = Copy-Item -Path "$script:ModuleRoot\library\xWindowsUpdate" -Destination "$programfiles\WindowsPowerShell\Modules" -ToSession $remotesession -Recurse -Force -ErrorAction Stop
}

$ProgressPreference = $oldpref
Expand All @@ -151,10 +157,10 @@ function Start-DscUpdate {
}
if ($IsLocalHost) {
Write-PSFMessage -Level Verbose -Message "Copying xPSDesiredStateConfiguration to $hostname (local to $programfiles\WindowsPowerShell\Modules\xPSDesiredStateConfiguration)"
$null = Copy-Item -Path "$script:ModuleRoot\library\xPSDesiredStateConfiguration" -Destination "$programfiles\WindowsPowerShell\Modules" -Recurse -Force
$null = Copy-Item -Path "$script:ModuleRoot\library\xPSDesiredStateConfiguration" -Destination "$programfiles\WindowsPowerShell\Modules" -Recurse -Force -ErrorAction Stop
} else {
Write-PSFMessage -Level Verbose -Message "Copying xPSDesiredStateConfiguration to $hostname (remote)"
$null = Copy-Item -Path "$script:ModuleRoot\library\xPSDesiredStateConfiguration" -Destination "$programfiles\WindowsPowerShell\Modules" -ToSession $remotesession -Recurse -Force
$null = Copy-Item -Path "$script:ModuleRoot\library\xPSDesiredStateConfiguration" -Destination "$programfiles\WindowsPowerShell\Modules" -ToSession $remotesession -Recurse -Force -ErrorAction Stop
}

$ProgressPreference = $oldpref
Expand All @@ -174,18 +180,20 @@ function Start-DscUpdate {
Write-PSFMessage -Level Verbose -Message "Adding $filename to $RepositoryPath"
$repofile = Join-Path -Path $RepositoryPath -ChildPath $filename
if ($remotehome) {
$null = Copy-Item -Path $repofile -Destination "$remotehome\Downloads\$filename" -ToSession $remotesession -Recurse -Force
$null = Copy-Item -Path $repofile -Destination "$remotehome\Downloads\$filename" -ToSession $remotesession -Recurse -Force -ErrorAction Stop
} else {
$null = Copy-Item -Path $repofile -Destination "$home\Downloads" -Recurse -Force
$null = Copy-Item -Path $repofile -Destination "$home\Downloads" -Recurse -Force -ErrorAction Stop
}
} catch {
$hostname = $object.ComputerName
Stop-PSFFunction -Message "Couldn't copy $filename from repo to $hostname." -Continue
}
Write-PSFMessage -Level Verbose -Message "Adding $($FilePath)"
if (-not $hostname) {
$hostname = $object.ComputerName
}
Stop-PSFFunction -Message "Couldn't copy $filename from repo to $hostname." -ErrorRecord $PSItem -Continue
}
}

if ($FilePath) {
Write-PSFMessage -Level Verbose -Message "Adding $($FilePath)"
$remotefileexists = $updatefile = Invoke-KbCommand -ArgumentList $FilePath -ScriptBlock {
Get-ChildItem -Path $args -ErrorAction SilentlyContinue
}
Expand All @@ -203,7 +211,6 @@ function Start-DscUpdate {
}

if (-not $updatefile) {
Write-PSFMessage -Level Verbose -Message "Update file not found, download it for them"
if ($HotfixId) {
$Pattern = $HotfixId
} elseif ($Guid) {
Expand Down Expand Up @@ -558,6 +565,9 @@ function Start-DscUpdate {
{ $message -match "2147942402" } {
throw "System can't find the file specified for some reason."
}
{ $message -match "2149842967" } {
throw "Error 2149842967 - Update is probably not applicable or already installed. $message"
}
default {
throw $message
}
Expand Down Expand Up @@ -586,9 +596,13 @@ function Start-DscUpdate {
$exists = Get-KbInstalledSoftware -ComputerName $ComputerName -Pattern $hotfix.property.id -IncludeHidden

if ($exists.Summary -match "restart") {
$status = "This update requires a restart"
$status = "Install successful. This update requires a restart | $($exists.Summary)"
} else {
$status = "Install successful"
if ($exists.Summary) {
$status = "Install successful"
} else {
$status = "Install successful | $($exists.Summary)"
}
}
if ($HotfixId) {
$id = $HotfixId
Expand Down
12 changes: 10 additions & 2 deletions private/Start-WindowsUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,22 @@ function Start-WindowsUpdate {
$null = Import-Module $path 4>$null
}


if ($HotfixId) {
Write-PSFMessage -Level Verbose -Message "Hotfix detected, getting info"
$InputObject += Get-KbUpdate -HotfixId $HotfixId -ComputerName $ComputerName
}

Write-PSFMessage -Level Verbose -Message "Using the Windows Update method"
$sessiontype = [type]::GetTypeFromProgID("Microsoft.Update.Session")
$session = [activator]::CreateInstance($sessiontype)
$session.ClientApplicationID = "kbupdate installer"

if ($InputObject.UpdateId) {
Write-PSFMessage -Level Verbose -Message "Got an UpdateId"
$searchresult = $session.CreateUpdateSearcher().Search("UpdateId = '$($InputObject.UpdateId)'")
foreach ($uid in $InputObject.UpdateId) {
Write-PSFMessage -Level Verbose -Message "Got an UpdateId"
$searchresult = $session.CreateUpdateSearcher().Search("UpdateId = '$uid'")
}
} else {
Write-PSFMessage -Level Verbose -Message "Build needed updates"
$searchresult = $session.CreateUpdateSearcher().Search("Type='Software' and IsInstalled=0 and IsHidden=0")
Expand Down
33 changes: 7 additions & 26 deletions public/Install-KbUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,14 @@ function Install-KbUpdate {
$hostname = $computer.ComputerName
$null = $completed++
$null = $added++
$method = $null

if ($computer.IsLocalHost -and -not (Test-ElevationRequirement -ComputerName $hostname)) {
Stop-PSFFunction -EnableException:$EnableException -Message "You must be an administrator to run this command on the local host" -Continue
}

Write-Progress -Activity "Installing updates" -Status "Added $($computer.ComputerName) to queue. Processing $added computers..." -PercentComplete ($added / 100 * 100)

Write-PSFMessage -Level Verbose -Message "Processing $($parms.ComputerName)"

if ($computer.IsLocalhost) {
if ((Get-Service wuauserv | Where-Object StartType -ne Disabled)) {
Write-PSFMessage -Level Verbose -Message "Setting method to Windows Update $($parms.ComputerName)"
$method = "WindowsUpdate"
}
}
Write-PSFMessage -Level Verbose -Message "Processing $hostname"

try {
$parms = @{
Expand All @@ -204,24 +196,13 @@ function Install-KbUpdate {
$null = $PSDefaultParameterValues["Start-Job:ArgumentList"] = $parms
$null = $PSDefaultParameterValues["Start-Job:Name"] = $hostname

if ($method -eq "WindowsUpdate" -and -not $FilePath) {
Write-PSFMessage -Level Verbose -Message "Method is WindowsUpdate"
if ($NoMultithreading) {
Write-PSFMessage -Level Verbose -Message "Not using jobs for update to $hostname"
Start-WindowsUpdate @parms
} else {
Write-PSFMessage -Level Verbose -Message "Using jobs for update to $hostname"
$jobs += Start-Job -ScriptBlock $wublock
}
Write-PSFMessage -Level Verbose -Message "Installing using DSC"
if ($NoMultithreading) {
Write-PSFMessage -Level Verbose -Message "Not using jobs for update to $hostname"
Start-DscUpdate @parms -ErrorAction Stop
} else {
Write-PSFMessage -Level Verbose -Message "Method is DSC"
if ($NoMultithreading) {
Write-PSFMessage -Level Verbose -Message "Not using jobs for update to $hostname"
Start-DscUpdate @parms -ErrorAction Stop
} else {
Write-PSFMessage -Level Verbose -Message "Using jobs for update to $hostname"
$jobs += Start-Job -ScriptBlock $dscblock -ErrorAction Stop
}
Write-PSFMessage -Level Verbose -Message "Using jobs for update to $hostname"
$jobs += Start-Job -ScriptBlock $dscblock -ErrorAction Stop
}
} catch {
Stop-PSFFunction -Message "Failure on $hostname" -ErrorRecord $PSItem -EnableException:$EnableException -Continue
Expand Down
23 changes: 13 additions & 10 deletions public/Uninstall-KbUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,13 @@ function Uninstall-KbUpdate {
if (-not $exists) {
Write-PSFMessage -Level Warning -Message "$hotfix is not installed on $computer"
} else {
if ($exists.Summary -match "restart") {
Stop-PSFFunction -EnableException:$EnableException -Message "You must restart before you can uninstall $hotfix on $computer" -Continue
} else {
if ($exists.FastPackageReference -and $exists.FastPackageReference -notin $InputObject.FastPackageReference) {
$InputObject += $exists
} elseif ($exists.PackageObject -and $exists.PackageObject -notin $InputObject.PackageObject) {
$InputObject += $exists
}
# turns out this is not true
# Stop-PSFFunction -EnableException:$EnableException -Message "You must restart before #you can uninstall $hotfix on $computer" -Continue
#} else {
if ($exists.FastPackageReference -and $exists.FastPackageReference -notin $InputObject.FastPackageReference) {
$InputObject += $exists
} elseif ($exists.PackageObject -and $exists.PackageObject -notin $InputObject.PackageObject) {
$InputObject += $exists
}
}
}
Expand Down Expand Up @@ -322,9 +321,13 @@ function Uninstall-KbUpdate {

# I tried to get this working using DSC but in end end, a Start-Process equivalent was it for the convenience of not having to specify a filename, tho that can be added as a backup
if ($ArgumentList -match ".exe") {
$exec = "$program$ArgumentList"
$exec = "$program$ArgumentList".Trim()
} else {
$exec = "$program $ArgumentList"
$exec = "$program $ArgumentList".Trim()
}

if ($exec.Length -lt 4) {
Stop-PSFFunction -Message "Failure on $computer while attempting to uninstall $packagename | Uninstaller cannot be found" -EnableException:$EnableException -Continue
}
if ($PSCmdlet.ShouldProcess($computer, "Uninstalling Hotfix $packagename by executing $exec")) {
try {
Expand Down

0 comments on commit 0d8d863

Please sign in to comment.