Last active
March 18, 2021 14:19
-
-
Save OtterKring/04c694ecb92517c85dd98ddc1777ece8 to your computer and use it in GitHub Desktop.
Powershell Self-Modification
This file contains 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
#region SAVE_RUNTIME | |
#region SELFMODIFICATION | |
$NewLastRunTime = '18/03/2021 14:51:46' #MODIFY_HERE | |
#region SELFMODIFICATION | |
$LastRunTime = [datetime]::Parse($NewLastRunTime) | |
$Me = $MyInvocation.MyCommand.Source | |
$Myself = Get-Content $Me | |
$Myself ` | |
| Foreach-Object -Process {$_ -creplace '^\$NewLastRunTime.*#MODIFY_HERE$',("`$NewLastRunTime = '{0}' #MODIFY_HERE" -f [datetime]::now.ToString())} ` | |
| Set-Content $Me -Encoding utf8 | |
Remove-Variable -Name Myself,Me | |
#endregion SAVE_RUNTIME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Didn't want to save my last run time in a separate file. Remembered a lesson about self modifying programs from my IBM-360 Assembler times. This way the script holds its own last run time and cannot be lost (as long as you don't loose the script).