Skip to content

Commit

Permalink
Bug fix: Fix FilePath issue and some progress bar issues (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee authored Nov 4, 2022
1 parent 4bde074 commit e04cb75
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 24 deletions.
2 changes: 1 addition & 1 deletion private/Invoke-KbCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function Invoke-KbCommand {
$PSDefaultParameterValues['*:EnableException'] = $false
}
if (-not $computer.IsLocalhost) {
Write-PSFMessage -Level Verbose -Message "Computer is not localhost, adding $ComputerName to PSDefaultParameterValues"
Write-PSFMessage -Level Debug -Message "Computer is not localhost, adding $ComputerName to PSDefaultParameterValues"
$PSDefaultParameterValues['Invoke-Command:ComputerName'] = $ComputerName
}
if ($Credential) {
Expand Down
11 changes: 7 additions & 4 deletions private/Start-BitsJobProcess.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Start-BitsJobProcess {
}
end {
$totalfiles = $jobs.FileList.Count
$localnames = $jobs.FileList.LocalName
$bs = $jobs | Where-Object BytesTotal -ne 18446744073709551615
$bytestotal = ($bs.BytesTotal | Measure-Object -Sum).Sum
$bstotal = [math]::Round(($bytestotal / 1MB),2)
Expand All @@ -34,10 +35,11 @@ function Start-BitsJobProcess {
if ($percentcomplete -gt 100 -or $percentcomplete -lt 0) {
$percentcomplete = 0
}

$progressparms = @{
Activity = "Downloaded $mbjtotal MB of at least $bstotal MB total from $($bs.count) files in this batch."
Status = "$completed of $totalfiles files completed"
PercentComplete = 100 - $(($currentcount / $totalfiles) * 100)
PercentComplete = $percentcomplete
}
if ($oldmbjtotal -ne $mbjtotal) {
Write-PSFMessage -Level Debug -Message "Current count: $currentcount files"
Expand All @@ -57,8 +59,8 @@ function Start-BitsJobProcess {
$title = $bitsjob.Description.Replace("kbupdate - ", "")
switch ($bitsjob.JobState) {
"Transferred" {
$null = Complete-BitsTransfer -BitsJob $bitsjob
foreach ($filename in $bitsjob.FileList.LocalName) {
$null = Complete-BitsTransfer -BitsJob $bitsjob
Write-PSFMessage -Level Verbose -Message "Sweet, $filename is done."
do {
Start-Sleep -Milliseconds 200
Expand All @@ -74,17 +76,18 @@ function Start-BitsJobProcess {
}
"Error" {
foreach ($file in $bitsjob.FileList.LocalName) {
Write-PSFMessage -Level Verbose -Message "Oh no, $filename has errored."
Write-PSFMessage -Level Verbose -Message "Oh no, $file has errored."
Stop-PSFFunction -Message "Failure downloading $title (file) | $($bitsjob.ErrorDescription)" -Continue
}
$null = $bitsjob | Complete-BitsTransfer -ErrorAction Ignore
}
}
} catch {
Stop-PSFFunction -Message "Failure on $hostname" -Continue
Stop-PSFFunction -Message "Failure for $title | $PSItem" -Continue
}
}
}
Write-Progress -Activity "Downloading $totalfiles total files" -Completed
Get-ChildItem -Path $localnames -ErrorAction Ignore
}
}
56 changes: 49 additions & 7 deletions private/Start-DscUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ function Start-DscUpdate {
}
}
process {
if ($FilePath -and -not $InputObject) {
Write-PSFMessage -Level Verbose -Message "Setting InputObject to $FilePath"
$InputObject = $FilePath
}
if (-not $InputObject) {
Write-PSFMessage -Level Verbose -Message "Nothing to install on $hostname, moving on"
}
Expand Down Expand Up @@ -206,13 +210,6 @@ function Start-DscUpdate {
}
}

if ($FilePath) {
Write-PSFMessage -Level Verbose -Message "Adding $($FilePath)"
$remotefileexists = $updatefile = Invoke-KbCommand -ArgumentList $FilePath -ScriptBlock {
Get-ChildItem -Path $args -ErrorAction SilentlyContinue
}
}

if (-not $remotefileexists) {
if ($FilePath) {
# try really hard to find it locally
Expand Down Expand Up @@ -783,6 +780,7 @@ function Start-DscUpdate {
} else {
$status = "Install successful"
}

if ($HotfixId) {
$id = $HotfixId
} else {
Expand All @@ -794,6 +792,8 @@ function Start-DscUpdate {

if ($object.Title) {
$filetitle = $object.Title
} elseif ($exists.Title) {
$filetitle = $exists.Title
} else {
$filetitle = $updatefile.VersionInfo.ProductName
}
Expand Down Expand Up @@ -835,6 +835,48 @@ function Start-DscUpdate {
$id = $null
}

if ($object.Title) {
$filetitle = $object.Title
} elseif ($exists.Title) {
$filetitle = $exists.Title
} else {
$filetitle = $updatefile.VersionInfo.ProductName
}

if (-not $filetitle) {
$filetitle = $Title
}

[pscustomobject]@{
ComputerName = $hostname
Title = $filetitle
ID = $id
Status = $Status
FileName = $updatefile.Name
}
} elseif ("$PSItem" -match "find message text") {
Write-PSFMessage -Level Verbose -Message "The system cannot find message text for message number 0x%1 in the message file for %2. Checking to see if it was actually installed."

if ($hotfix.property.id) {
$exists = Get-KbInstalledSoftware -ComputerName $ComputerName -Pattern $hotfix.property.id -IncludeHidden
}

if (-not $exists) {
Stop-PSFFunction -Message "Failure on $hostname" -ErrorRecord $PSitem -Continue -EnableException:$EnableException
} else {
$status = "This update requires a restart"
}

if ($HotfixId) {
$id = $HotfixId
} else {
$id = $guid
}

if ($id -eq "DAADB00F-DAAD-B00F-B00F-DAADB00FB00F") {
$id = $null
}

if ($object.Title) {
$filetitle = $object.Title
} else {
Expand Down
14 changes: 12 additions & 2 deletions private/Start-JobProcess.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ function Start-JobProcess {
$added = 0
}
$added++
$percentcomplete = ($added / 100 * 100)
if ($percentcomplete -lt 0 -or $percentcomplete -gt 100) {
$percentcomplete = 0
}
$progressparms = @{
Activity = $Activity
Status = "Still $Status on $($kbjobs.Name -join ', '). Please enjoy the progress bar."
PercentComplete = ($added / 100 * 100)
PercentComplete = $percentcomplete
}
Write-Progress @progressparms
foreach ($item in $kbjobs) {
Expand Down Expand Up @@ -126,10 +130,16 @@ function Start-JobProcess {
if (-not $done) {
$done = $kbjobs
}

$percentcomplete = ($added / 100 * 100)
if ($percentcomplete -lt 0 -or $percentcomplete -gt 100) {
$percentcomplete = 0
}

$progressparms = @{
Activity = $Activity
Status = "Still $Status on $($done.Name -join ', '). Please enjoy the progress bar."
PercentComplete = ($added / 100 * 100)
PercentComplete = $percentcomplete
}

Write-Progress @progressparms
Expand Down
2 changes: 1 addition & 1 deletion public/Install-KbUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function Install-KbUpdate {
)
begin {
# create code blocks for jobs
$wublock = [scriptblock]::Create($((Get-Command Start-WindowsUpdate).Definition))
# $wublock = [scriptblock]::Create($((Get-Command Start-WindowsUpdate).Definition))
$dscblock = [scriptblock]::Create($((Get-Command Start-DscUpdate).Definition))
# cleanup
$null = Get-Job -ChildJobState Completed | Where-Object Name -in $ComputerName.ComputerName | Remove-Job -Force
Expand Down
46 changes: 37 additions & 9 deletions public/Save-KbUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,26 @@ function Save-KbUpdate {
[switch]$EnableException
)
begin {
$jobs = @()
$jobs = $inputobjects = $uniquelinks = @()
$count = 0
}
process {
if ($InputObject) {
$inputobjects += $InputObject
}

if ($Link) {
$uniquelinks += $Link
}
}
end {
if ($uniquelinks) {
$uniquelinks = $uniquelinks | Select-Object -Unique
}
switch ($PSCmdlet.ParameterSetName) {
'link' {
$Link | Foreach-Object {
Write-PSFMessage -Level Verbose -Message "Processing link parameter set"
$uniquelinks | Foreach-Object {
$hyperlinklol = $PSItem
$fileName = Split-Path $hyperlinklol -Leaf
if ($FilePath) {
Expand All @@ -126,12 +139,25 @@ function Save-KbUpdate {
Get-ChildItem -Path $file
continue
}
if (-not $filePath) {
$FilePath = $file
}

Write-PSFMessage -Level Verbose -Message "Link: $PSItem"
Write-PSFMessage -Level Verbose -Message "FilePath: $FilePath"
Write-PSFMessage -Level Verbose -Message "File: $file"

# just show any progress since piping won't allow calculation of the total
$percentcomplete = $(($count / 300) * 100)

if ($percentcomplete -lt 0 -or $percentcomplete -gt 100) {
$percentcomplete = 0
}

$progressparms = @{
Activity = "Queuing up downloads"
Status = "Adding files to download queue"
PercentComplete = $(($count / 300) * 100)
PercentComplete = $percentcomplete
}

Write-Progress @progressparms
Expand Down Expand Up @@ -163,6 +189,7 @@ function Save-KbUpdate {
}

default {
Write-PSFMessage -Level Verbose -Message "Processing default parameter set"
if ($Pattern.Count -gt 1 -and $PSBoundParameters.FilePath) {
Stop-PSFFunction -EnableException:$EnableException -Message "You can only specify one KB when using FilePath"
return
Expand Down Expand Up @@ -201,12 +228,12 @@ function Save-KbUpdate {
$params.Source = $Source
}

$InputObject += Get-KbUpdate @params
$inputobjects += Get-KbUpdate @params
}

$InputObject = $InputObject | Sort-Object -Unique
$inputobjects = $inputobjects | Sort-Object -Unique

foreach ($object in $InputObject) {
foreach ($object in $inputobjects) {
if ($Architecture) {
$templinks = @()
foreach ($arch in $Architecture) {
Expand Down Expand Up @@ -234,7 +261,9 @@ function Save-KbUpdate {
if (-not $PSBoundParameters.FilePath) {
$FilePath = Split-Path -Path $hyperlinklol -Leaf
} else {
$Path = Split-Path -Path $FilePath
if (-not $Path) {
$Path = Split-Path -Path $FilePath
}
}

$file = Join-Path -Path $Path -ChildPath $FilePath
Expand Down Expand Up @@ -276,8 +305,7 @@ function Save-KbUpdate {
}
}
}
}
end {

if ($jobs) {
Write-PSFMessage -Level Verbose -Message "Starting job process"
$jobs | Start-BitsJobProcess
Expand Down

0 comments on commit e04cb75

Please sign in to comment.