diff --git a/PowerSettingsBackup.json b/PowerSettingsBackup.json new file mode 100644 index 0000000..27c934b --- /dev/null +++ b/PowerSettingsBackup.json @@ -0,0 +1,5 @@ +{ + "OriginalSchemeGuid": "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c", + "HibernateEnabled": false, + "SavedAt": "2026-08-28T12:08:21.4260829+03:00" +} diff --git a/Test-BatteryDrain.ps1 b/Test-BatteryDrain.ps1 index 33e5c20..a390e3e 100644 --- a/Test-BatteryDrain.ps1 +++ b/Test-BatteryDrain.ps1 @@ -196,12 +196,12 @@ Add-Type -Namespace Native -Name Power -MemberDefinition @' public static extern uint SetThreadExecutionState(uint esFlags); '@ -$ES_CONTINUOUS = 0x80000000 -$ES_SYSTEM_REQUIRED = 0x00000001 -$ES_DISPLAY_REQUIRED = 0x00000002 +$ES_CONTINUOUS = [Convert]::ToUInt32('80000000', 16) +$ES_SYSTEM_REQUIRED = [Convert]::ToUInt32('00000001', 16) +$ES_DISPLAY_REQUIRED = [Convert]::ToUInt32('00000002', 16) function Set-StayAwake { - [Native.Power]::SetThreadExecutionState($ES_CONTINUOUS -bor $ES_SYSTEM_REQUIRED -bor $ES_DISPLAY_REQUIRED) | Out-Null + [Native.Power]::SetThreadExecutionState([uint32]($ES_CONTINUOUS -bor $ES_SYSTEM_REQUIRED -bor $ES_DISPLAY_REQUIRED)) | Out-Null } function Clear-StayAwake { @@ -283,6 +283,19 @@ function Get-BatteryInfo { #region Main try { + # Validate/prepare the log path up front so a bad path fails fast, before power settings or + # the CPU load are touched. Falls back to the script folder if the requested location is invalid. + $logDir = Split-Path -Path $LogPath -Parent + if ($logDir -and -not (Test-Path -Path $logDir)) { + try { + New-Item -ItemType Directory -Path $logDir -Force | Out-Null + } catch { + $fallbackPath = Join-Path -Path $PSScriptRoot -ChildPath (Split-Path -Path $LogPath -Leaf) + Write-Warning "Could not create log directory '$logDir' ($($_.Exception.Message)). Falling back to '$fallbackPath'." + $LogPath = $fallbackPath + } + } + if (-not $SkipPowerSettingsChanges) { Set-PowerSettingsForBatteryTest } else { @@ -295,9 +308,6 @@ try { Write-Host "Starting synthetic CPU load targeting ~$TargetLoadPercent% across $cores logical processors..." -ForegroundColor Cyan $loadJobs = Start-CpuLoad -Percent $TargetLoadPercent -Cores $cores - if (-not (Test-Path (Split-Path -Path $LogPath -Parent))) { - New-Item -ItemType Directory -Path (Split-Path -Path $LogPath -Parent) -Force | Out-Null - } "Timestamp,PercentRemaining,Status,Charging,EstimatedRunTimeMinutes" | Out-File -FilePath $LogPath -Encoding UTF8 Write-Host "Logging battery status every $SampleIntervalSeconds s to $LogPath" -ForegroundColor Cyan diff --git a/battery.csv b/battery.csv new file mode 100644 index 0000000..681fd9d --- /dev/null +++ b/battery.csv @@ -0,0 +1,6 @@ +Timestamp,PercentRemaining,Status,Charging,EstimatedRunTimeMinutes +2026-08-28T12:08:25.6435909+03:00,78,Discharging,False,23 +2026-08-28T12:08:55.6829249+03:00,77,Discharging,False,27 +2026-08-28T12:09:25.7024303+03:00,75,Discharging,False,26 +2026-08-28T12:09:55.7546137+03:00,74,Discharging,False,26 +2026-08-28T12:10:25.7804244+03:00,72,Discharging,False,24