DFS Replication Monitor With History Upgrade 2.6
By far my most popular post is the DFS Replication Monitor With History script, so I like to revisit it every now and then and make sure I’m doing things the best way I know how. Continue on to see about the new upgrade.
This isn’t a big upgrade, but while looking at someone else’s script it suddenly occurred to me that I could be saving my data in XML format instead of CSV. Why is this important? With a CSV every property is stored as a string, but one of the properties of the script very definitely has to be a functioning DateTime variable. This turned into a problem with international users (read more detail here) because Windows can be set to store the date/time in different formats when converted to a string. Not to mention I had to run a little loop after loading the CSV data and convert every date property into a DateTime variable, and then back again to a string value to save it.
XML can solve this problem because it fully supports .NET data types, and would save me from having to specifically code for international date formats.
Turns out, converting the script to use XML was pretty easy too. First I had to take some existing data and convert it to XML in the first place, this code became Convert-DFSDataToXML.ps1 and if you want to start using the new version of DFS Monitor with History and keep your historical data you’ll need to download it and run it once. But the “gist” of the script is you take your array and pipe it into ConvertTo-CLIXML and voila, you have your data perfectly preserved in an XML file. The only downside is that the data file, for me, jumped from 202Kb to 1.4MB.
After that I just had to remove the conversion loops–one at the loading of the CSV where I convert from string to date/time, and one just before saving the CSV where I had to convert back to string in a very specific format. One minor problem was when I ran the script it died trying to add the new data, and this turned out to be a variable type mismatch. When you load XML into an object, it creates a PSCustomObject system array, but when I saved data I was adding new objects to the array as PSObject’s. Simply changing the save portion to PSCustomObject solved that problem.
How do you upgrade?
First download the conversion script here, then download the new DFS Monitor with History script here. I’m going to assume you are running this on the server/workstation where you save your historical data in a path called: c:\scripts\dfsmonitor. Save both scripts to that location.
- Open Powershell and navigate to the dfsmonitor folder.
- Run the conversion script: .\Convert-DFSDataToXML.ps1 -DataLocation C:\Scripts\DFSMonitor
- When completed modify the PARAM portion of DFSMonitorWithHistory.ps1 to match your environment
That’s it. If you already have a scheduled task just make sure it’s pointing to the new script, and if you saved it with the same name it should be.
If you’d like more information about setting up a scheduled task to run Poweshell scripts, you can read the How-To I wrote for Spiceworks.
As always, I want to hear from you, especially if you’re an international user with your funky date/time formats (kidding!). I might not answer you in comments here, but I will always try to get back to you in email.