listening to the PowerScripting Podcast - Episode 41 RoboCopy was mentioned, and I remembered some Robocopy stuff I still had laying around :
And I will use it as a series about using / wrapping a native tool from PowerShell, I start out with a bit of Text Processing,
############################################################################################# ## Make RoboCopy Help Object #############################################################################################
This peice of PowerShell code basically just starts Robocopy /? , parses the resulting help text into an Custom object, this gives us a Robocopy help object in the variable $RoboCopyHelpObject and we can check it like this :
You can see that as I have the robocopy help in object form now I can do for example grouping and counting of the Help topics,
I did a series about TextScraping where you can find more examples of text to object translations, so I won't go into much detail here but we will use this object we created later in this series.
but for now you can change the directories to valid ones for testing out robocopy command (I picked a directory where I was sure some files would be in use, guess why ;-) ), and we will first go on with the output of the example robocpoy command, in the next post we will also parse the output of robocopy into an Object so that we can do some analyzing on it from PowerShell (and also process saved Robocopy Logs) , after that I go on about how I created the Form above.
In this 3rd part of this series we go on with the output of a Test copy, and will start to parse the robocopy log, I just made a sTest1 directory with some test files and started the command as explained in last post,
You can see in the output that there are 3 distinct parts in the Robocopy output, Start, Details, Stop information divided by a line, so the first thing is dividing the output of robocopy in the 3 parts and process them separately, we do that by this line :
You can see above that we can calculate the running time now and see the count of files, be sure to take a look at the code to parse the Stop information, as you can find some nifty tricks as overriding the ToString() method on the Files and Directory collections to make them more clear in default output:
The following is the complete code for the wrapper example, and I did put it on PoSHcode.org , the PowerShell code repository, :
As you can see below you can also use the Code in the Repository on your blog by using the provided link (Update try to fix codeexample output)and there are Cmdlets to retrieve the PowerShell code
And now you can dig into the details session like this :
I tried to create a in use error by opening a file with EXCEL but seems 2007 does not lock it anymore ;-), but you can create a good test log by copying your userprofile.
at the end of the code are some more examples of using the $RoboStatus object for analizing the log.
If you find this code usefull and have some improve them feel free to post and updated version of it on PoSHcode.org .
In the next post I will add a PowerShell GUI wrapper to create Robocopy commands from a form, and a small forms lib .
When I made the RoboGUI script, I will show in the next post in the robocopy series, I was experimenting with a Forms Library to make it easier to work with Forms in PowerShell, as The RoboGUI.ps1 uses it, I will post it here on my blog, It is not complete or under active development (and probably will not be anymore ) but it still might give you some ideas, hence I decided to post it outside of the series.
The goal was to make it easier, and more clear using forms code in PowerShell scripts, the library contains also a function the "read out" current controls making it possible to copy over existing settings, it is not used but I left it in for reference again.
This lib makes it possible to write code like this in a PowerShell script ;
you can see you can use parameters and hashTable format to describe control settings, what makes it look much nicer in code.
And the comple Library. (I will also post it on http://PoSHcode.org , but as you could see in last post I still have some problem embedding the code in my blog)
As indicated this was a test project I did about a year ago for testing this way of working, it should not be seen as best practice but as something I was test out at the time and might contain some ideas that you could use.
more examples of using this library in a script can be found in the script in the next blogpost about RoboGUI.PS1 , for the generating part (get-controlformat / get-formControls) your on your own, but note that you can retrieve extra properties also, and the Format of Set-Controlformat, is exactly the same as the Tostring() when you list Control properties from PowerShell ;-).
I this part I will cover the code for the "PowerShell Robocopy GUI" as shown in PowerShell and Robocopy part 2 , before I will put everything together with some usage examples in the next and final post in this series.
What happens in this PowerShell script ?
First, I convert the Robocopy help object that we created in PowerShell and Robocopy part 1 (for convenience also added to code below) into 2 datatables, one for Switches and one for Parameters (I make that distinction here as Parameters need an inputbox next to a checkbox, hence I will display them in different DataGrids)
Then I load the FormsLib I discussed in last post PowerShell FormsLib example that contains helper functions to configure the form controls and make this script more readable, to create a form for displaying the possible options for robocopy with the accompanying help, and update button is provided to "generate" the robocopy command for the options selected in the GUI,
*Update* to this to work in PowerShell V1 you need to have the forms libary loaded (thanks to tojo2000 for pointing me to that in the comments)
Now you have all the code and as said in the next and last part of this series, I will show how to put it all the parts we created together and show how to put it all in practice by giving some more usage examples