Skip to content

Instantly share code, notes, and snippets.

@OtterKring
Last active March 18, 2021 14:19
Show Gist options
  • Save OtterKring/04c694ecb92517c85dd98ddc1777ece8 to your computer and use it in GitHub Desktop.
Save OtterKring/04c694ecb92517c85dd98ddc1777ece8 to your computer and use it in GitHub Desktop.
Powershell Self-Modification
#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
@OtterKring
Copy link
Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment