Config File Iteration Backup – Change Checking Config Files
Reading Time: 2 minutesIn a lot of environments that have developers that use a lot of config files, sometimes it would be nice to keep older versions of those files. Fortunately Microsoft has graced us with shadow copies so we can have “Previous Versions”. The only issue with that, is you can only can’t turn on shadow copies (as far as I know) for specific files. So what I did was write a powershell script to take care of that, in a round-about way.
What this script does, is wait until the file has been modified then copy it to an “archive” location and time stamp it so you can review older copies.
At the beginning of this script there are two arrays that include variables of full paths to the files. “$OriginalPath” is the array that holds the full path to each file you want to watch. In the script here the two files I’m watching are “C:\configs\config1.txt” and “C:\configs\config2.txt”. Then the second array is where you want to archive the files to. In the script here it’s “C:\archive_configs\config1.txt” and “C:\archive_configs\config2.txt”.
What’s done after the arrays are initialize, is the time -1 minute and compares the last write value of the file in question to the current time -1 minute. If it has been modified, it copies to the archive location then modifies the name with a time stamp. Then loops back through if there are more files being checked in the array.
What I’ve done is put this in Task Scheduler to run every 1 minute. If you want to modify that, take the line:
$1MinAgo = (get-date).AddMinutes(-1)
and you the “Minutes” portion and the “(-1)” portion can both be modified.
https://gallery.technet.microsoft.com/Config-File-Iteration-ab2a69df
I hope I’ve made your day a little bit easier!