This is a topic I have been meaning to write about for a long time. I was recently working with this scenario and thought it would be a good example. First I am going to get a little nostalgic to provide some context. Back in the late 80’s and early 90’s in my first days of computing working in the DOS world, a favorite utility of mine was a file manager called Norton Commander. This was a very feature rich text based dual pane file manager. Here is a screenshot, I hope it brings back some good memories. If this does not look familiar then hopefully there is some historic or comic value.
When Windows 3.0 was introduced and the primary interface became the program manager I could not believe it. Who wanted to run a computer using pictures, how absurd.J So I surrendered my beloved Norton Commander and was forced to use the wonderful Windows File Manager. Here is a screenshot so you too can relive the feature deficits.
Of course this became Windows Explorer, which we all know and settle on using. I always wanted a file manager with that familiar feel of the Norton Commander dual pane interface, but always settled for Windows Explorer. When I started working with PowerShell several years ago my need for a better file manager became apparent. I searched and found an application xplorer2 which had the dual pane look and feel I was looking for with a lot of customizability. It turned out to be an excellent complement to PowerShell. OK, so there’s the point of the nostalgia.
I am going to talk about a few different topics in this post, but my goal is to provide a real world example of using PowerShell with xplorer2. Here is the xplorer2 interface in dual pane configuration as I use it. It can be customized extensively and I will not go into many of the features and options. This is not meant to be an xplorer2 advertisement; I am just a satisfied customer. Check it out here http://zabkat.com.
This application can be used to enhance the navigation and launching of scripts and PowerShell is a great example. The application has the ability to create bookmarks with keyboard shortcuts, custom columns, folder groupings and other various helpful file and folder stuff. IMHO, the best features of the application which complement PowerShell are user commands coupled with keyboard shortcuts and $-tokens. This allows a powerful way to launch PowerShell scripts and feed data into the scripts.
Here is an example. I have some ESXTOP CSV performance files that I need to merge. There are certainly several ways to do this and it could be done by manipulating the text files. The ESXTOP file is a standard PDH format .csv file and can be read and manipulated by many tools including a windows command line tool called relog.exe. This tool is found on Windows XP systems and above and is used to manipulate any standard PDH format performance files. This tool can be used to do a variety tasks to the files. Here is the help text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
PS C:Usersdmuegge> relog /? Microsoft ® Relog.exe (6.1.7601.17514) Relog creates new performance logs from data in existing performance logs by changing the sampling rate and/or converting the file format. Supports all performance log formats, including Windows NT 4.0 compressed logs. Usage: C:Windowssystem32relog.exe <filename [filename="" ...]=""> [options] Parameters: <filename [filename="" ...]=""> Performance file to relog. Options: -? Displays context sensitive help. -a Append output to the existing binary file. -c <path [path="" ...]=""> Counters to filter from the input log. -cf <filename> File listing performance counters to filter from the input log. Default is all counters in the original log file. -f <csv|tsv|bin|sql> Output file format. -t <value> Only write every nth record into the output file. Default is to write every record. -o Output file path or SQL database. -b <m d="" yyyy="" h:mm:ss[am|pm]=""> Begin time for the first record to write into the output file. -e <m d="" yyyy="" h:mm:ss[am|pm]=""> End time for the last record to write into the output file. -config <filename> Settings file containing command options. -q List performance counters in the input file. -y Answer yes to all questions without prompting. Examples: relog logfile.csv -c "Processor(_Total)% Processor Time" -o logfile.blg relog logfile.blg -cf counters.txt -f bin relog logfile.blg -f csv -o logfile.csv -t 2 relog logfile.blg -q -o counters.txt </filename></m></m></value></csv|tsv|bin|sql></filename></path></filename></filename> |
This command will be used in PowerShell scripts to create an easy tool for converting and merging performance logs. The first step to make this work in the xplorer2 environment is to setup the user commands. The screenshot below shows the user commands menu and functionality of the application.
The organize dialog lets you create and customize the commands and define keyboard shortcuts.
Here are some examples of commands I use all the time.
C:windowssystem32WindowsPowerShellv1.0powershell.exe -noexit $F
The above command runs the currently selected PowerShell script. The $F is a token in the xplorer2 environment which represents the currently selected file on the left pane. Simply select a PowerShell script and use the alt-0 keyboard shortcut.
C:Elevationelevate.cmd C:windowssystem32WindowsPowerShellv1.0powershell.exe -noexit $F $R
The above command runs the currently selected PowerShell script with the right visible directory path as an argument –The $R is a token in the xplorer2 environment which represents the right side visible directory path. The command also uses the old elevate VBScript to get an admin window. I welcome someone to clue me in on a better way to do this.
C:Elevationelevate.cmd C:windowssystem32WindowsPowerShellv1.0powershell.exe -noexit $F $G
The above command runs the currently selected PowerShell script on the left with the inactive highlighted file on the right as the argument.
C:Elevationelevate.cmd C:windowssystem32WindowsPowerShellv1.0powershell.exe -noexit $G $A
The above command runs the inactive highlighted PowerShell script on the left with the currently selected files on the right as an argument.
I will go back to our ESXTOP example to help make things more clear. In the example below I have multiple esxtop files from a host I would like to merge. The relog application will merge binary logs very easy so our first step is to convert to binary.
The screenshot above shows we have the PowerShell Script to do the conversion highlighted on the left and the files to be converted selected on the right. We just press the alt-4 keyboard shortcut which launches the script and it converts our files for us using relog.
Here is an example of the script and the output.
1 2 3 4 5 6 7 |
if(!($Args[0] -eq $null)){ foreach($file in $Args){ $LogFile = Get-Item -Path $file $newlog = $LogFile.DirectoryName + "" + $LogFile.Basename + ".blg" relog $LogFile -f BIN -o $newlog } } |
Here are all of our converted files ready to be merged. The files are sorted by extension and the binary files are selected to be run against the merge script highlighted on the left.
The alt-4 keyboard shortcut is selected to run the script; relog merges the files and outputs in CSV format ready for further analysis.
1 2 3 4 5 |
if(!($Args[0] -eq $null)){ $LogFile = Get-Item -Path $Args[0] $newlog = $LogFile.DirectoryName + "" + $LogFile.Basename + "_Combined.csv" relog $Args -f CSV -o $newlog } |
We now have a merged file ready for further analysis in Windows perfmon or other tools.
One item which is worth mentioning is the use of the $Args variable. In most cases it would be recommended to use PowerShell parameters rather than $Args. Although, in this case it provides a simple method to utilize the $-tokens functionality of xplorer2.
I have found PowerShell and xplorer2 used together to be a very useful combination. I hope others will find this concept useful.
Regards,
Dave