mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2026-10-08 11:39:10 +11:00
* Warn instead of logging tweak completed after a step error Invoke-WinUtilTweaks wrote "Apply tweak completed" unconditionally, even when Invoke-WinUtilScript or another helper had just logged an ERROR for that tweak. The job layer already counts those errors, so snapshot the count after the header line and compare at the end: log a WARN line with the error count when it grew, otherwise the existing completed line. Two Pester cases run the real logger and script runner against a temp log file to pin both outcomes. * Count tweak step errors from the shared log list The job error counter only increments inside a Start-WinUtilJob worker. Toggle switches call Invoke-WinUtilTweaks directly on the UI thread, so a failing toggle still logged "tweak completed" after its ERROR line. Every ERROR line is added to $sync.LoggedErrors from any runspace, and Invoke-WinUtilAutoRun already reads it as a before/after delta, so the tweak runner now does the same. The completion-status tests run without the worker flag and seed an earlier unrelated error, and a new case covers a failing UndoScript. * Count tweak errors on the logging runspace, not the shared list Diffing $sync.LoggedErrors charged a toggle with errors a concurrent job logged from its own runspace. Global scope is per runspace, so the logger now bumps the runspace counter for every headline error and the tweak status diffs that. Job workers still reset the counter at start and end, so job results are unchanged.
94 lines
3.5 KiB
PowerShell
94 lines
3.5 KiB
PowerShell
function Invoke-WinUtilTweaks {
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
Invokes the function associated with each provided checkbox
|
|
|
|
.PARAMETER CheckBox
|
|
The checkbox to invoke
|
|
|
|
.PARAMETER undo
|
|
Indicates whether to undo the operation contained in the checkbox
|
|
|
|
.PARAMETER KeepServiceStartup
|
|
Indicates whether to override the startup of a service with the one given from WinUtil,
|
|
or to keep the startup of said service, if it was changed by the user, or another program, from its default value.
|
|
#>
|
|
|
|
param(
|
|
$CheckBox,
|
|
$undo = $false,
|
|
$KeepServiceStartup = $true
|
|
)
|
|
|
|
$action = if ($undo) { "Undo" } else { "Apply" }
|
|
Write-WinUtilLog -Component "Tweaks" -Message "$action tweak: $CheckBox"
|
|
# The counter lives in this runspace, so an error a concurrent job logs from its own
|
|
# runspace cannot be charged to a toggle flipped on the UI thread
|
|
$errorsBefore = [int]$global:WinUtilJobErrorCount
|
|
|
|
if ($undo) {
|
|
$Values = @{
|
|
Registry = "OriginalValue"
|
|
Service = "OriginalType"
|
|
ScriptType = "UndoScript"
|
|
}
|
|
|
|
} else {
|
|
$Values = @{
|
|
Registry = "Value"
|
|
Service = "StartupType"
|
|
OriginalService = "OriginalType"
|
|
ScriptType = "InvokeScript"
|
|
}
|
|
}
|
|
if ($sync.configs.tweaks.$CheckBox.service) {
|
|
$sync.configs.tweaks.$CheckBox.service | ForEach-Object {
|
|
$changeservice = $true
|
|
|
|
# The check for !($undo) is required, without it the script will throw an error for accessing unavailable member, which's the 'OriginalService' Property
|
|
if ($KeepServiceStartup -AND !($undo)) {
|
|
try {
|
|
# Check if the service exists
|
|
$service = Get-Service -Name $psitem.Name -ErrorAction Stop
|
|
if(!($service.StartType.ToString() -eq $psitem.$($values.OriginalService))) {
|
|
$changeservice = $false
|
|
}
|
|
} catch [System.ServiceProcess.ServiceNotFoundException] {
|
|
Write-Warning "Service $($psitem.Name) was not found."
|
|
}
|
|
}
|
|
|
|
if ($changeservice) {
|
|
Set-WinUtilService -Name $psitem.Name -StartupType $psitem.$($values.Service)
|
|
}
|
|
}
|
|
}
|
|
if ($sync.configs.tweaks.$CheckBox.registry) {
|
|
$sync.configs.tweaks.$CheckBox.registry | Where-Object { -not $psitem.Values } | ForEach-Object {
|
|
Set-WinUtilRegistry -Name $psitem.Name -Path $psitem.Path -Type $psitem.Type -Value $psitem.$($values.registry)
|
|
}
|
|
}
|
|
if ($sync.configs.tweaks.$CheckBox.$($values.ScriptType)) {
|
|
$sync.configs.tweaks.$CheckBox.$($values.ScriptType) | ForEach-Object {
|
|
$Scriptblock = [scriptblock]::Create($psitem)
|
|
Invoke-WinUtilScript -ScriptBlock $scriptblock -Name $CheckBox
|
|
}
|
|
}
|
|
|
|
if (!$undo) {
|
|
if($sync.configs.tweaks.$CheckBox.appx) {
|
|
$sync.configs.tweaks.$CheckBox.appx | ForEach-Object {
|
|
Remove-WinUtilAPPX -Name $psitem
|
|
}
|
|
Remove-WinUtilProvisionedAPPX -PackageList $sync.configs.tweaks.$CheckBox.appx
|
|
}
|
|
}
|
|
$errorCount = [int]$global:WinUtilJobErrorCount - $errorsBefore
|
|
if ($errorCount -gt 0) {
|
|
Write-WinUtilLog -Level "WARN" -Component "Tweaks" -Message "$action tweak finished with $errorCount error(s): $CheckBox"
|
|
} else {
|
|
Write-WinUtilLog -Component "Tweaks" -Message "$action tweak completed: $CheckBox"
|
|
}
|
|
}
|