Last active
October 24, 2024 21:46
-
-
Save ninmonkey/08f7b8c9903843baf10531197218210d to your computer and use it in GitHub Desktop.
WinPS: Calls Add-Content with multiple retries. Before failing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is a ps5 + ps7 script | |
| # ps5 writes errors when PBI refreshes, ps7 either never does, or its much, much more rare | |
| $dest = 'C:\Users\cppmo_000\AppData\Local\Temp\cults.csv' | |
| $iter = 0 | |
| while($true) { | |
| $iter++ | |
| $Last = [datetime]::Now | |
| $content = w32tm /stripchart /computer:time.google.com /samples:1 /dataonly | |
| Add-Content -Path $dest -Value ( | |
| ( $content -replace '^\s+', '' -replace '\s+$', '' -join ',' ) ) | |
| Sleep -Milliseconds 5 | |
| $Now = [datetime]::Now | |
| if( $iter -lt 10 ) { continue } | |
| $iter = 0 | |
| '{0:n} ms elapsed' -f @( ($now - $Last).totalMilliseconds ) | | |
| Write-Host -bg 'gray30' -fg 'gray80' | |
| '{0:n4} mb' -f @( (gi $dest).Length / 1mb ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This version sometimes had an error, but did not for most cases | |
| function AddContentAutoRetry { | |
| <# | |
| .SYNOPSIS | |
| Calls Add-Content with multiple retries. Before failing | |
| .DESCRIPTION | |
| Writes errors on failure. Does not throw. | |
| You can enable throwing with -Ea Stop | |
| #> | |
| [CmdletBinding()] | |
| param( | |
| # Content | |
| $Value, | |
| $Path = 'C:\foo\out.csv', | |
| # Milliseconds to wait between writes | |
| [int] $delayMs = 200, | |
| # repeat how many times? | |
| [int] $maxFails = 5 | |
| ) | |
| $fails = 1 | |
| while($true) { | |
| try { | |
| Add-Content -Path $Path -ea 'Stop' -value $value -encoding utf8 | |
| "Success in ${fails}" | Write-Host -fg 'gray60' -bg 'gray30' | |
| break | |
| } catch { | |
| $fails++ | |
| "failed ${fails} of ${maxFails}. Sleeping..." | Write-Warning | |
| Sleep -Milliseconds $delayMS | |
| } | |
| if($fails -gt $maxFails ) { | |
| "Failed writing ${fails} to '${Path}'" | Write-Error | |
| return | |
| } | |
| } | |
| } | |
| $dest = 'C:\Users\cppmo_000\AppData\Local\Temp\cults.csv' | |
| $iter = 0 | |
| while($true) { | |
| $iter++ | |
| $Last = [datetime]::Now | |
| $content = w32tm /stripchart /computer:time.google.com /samples:1 /dataonly | |
| AddContentAutoRetry -Path $Dest -delayMs 10 -maxFails 20 -Value ( | |
| ( $content -replace '^\s+', '' -replace '\s+$', '' -join ',' ) ) | |
| Sleep -Milliseconds 5 | |
| $Now = [datetime]::Now | |
| if( $iter -lt 10 ) { continue } | |
| $iter = 0 | |
| '{0:n} ms elapsed' -f @( ($now - $Last).totalMilliseconds ) | | |
| Write-Host -bg 'gray30' -fg 'gray80' | |
| '{0:n4} mb' -f @( (gi $dest).Length / 1mb ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function AddContentAutoRetry { | |
| <# | |
| .SYNOPSIS | |
| Calls Add-Content with multiple retries. Before failing | |
| .DESCRIPTION | |
| Writes errors on failure. Does not throw. | |
| You can enable throwing with -Ea Stop | |
| #> | |
| [CmdletBinding()] | |
| param( | |
| # Content | |
| $Value, | |
| $Path = 'C:\foo\out.csv', | |
| # Milliseconds to wait between writes | |
| [int] $delayMs = 200, | |
| # repeat how many times? | |
| [int] $maxFails = 5 | |
| ) | |
| $fails = 0 | |
| while($true) { | |
| try { | |
| Add-Content -Path $Path -value $value -encoding utf8 | |
| break | |
| } catch { | |
| $fails++ | |
| "failed ${fails} of ${maxFails}. Sleeping..." | Write-Warning | |
| Sleep -Milliseconds $delayMS | |
| } | |
| if($fails -gt $maxFails ) { | |
| "Failed writing ${fails} to '${Path}'" | Write-Error | |
| return | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment