Software - PowerShell Junkie

What Is It?

The PowerShell Junkie is an Eclipse plugin that integrates the Microsoft PowerShell into your workspace. It provides two features: first, the ability to open a PowerShell located at the current selection; second, the ability to execute PowerShell scripts operating on the current selection. The latter makes it easy to quickly extend the functionality of Eclipse without writing new plugins.

For more detailed usage information, please refer to the Usage section below. To download the latest version, please access the update site through the Eclipse update manager. For more detailed information about the different releases of PowerShell Junkie, please visit the Release page.

Usage

This section guides you in using the PowerShell Junkie plugin. You may prefer to watch the screencast for a quick overview instead.

Configuration

After installing the plugin, the first action you must take is to configure the plugin for operation. Access the preferences page for the plugin through the standard preferences dialog. On this page, you will find the following options:

  1. PowerShell path: This field defines the location of the PowerShell executable. The path may be relative, for example the default powershell.exe, or it may be absolute. Tabbing off this field after making a change will cause the version field to recompute.
  2. Version: This autocomputed field displays the version of PowerShell detected by the plugin based on the specified path.
  3. Script lookup paths: This is a list of paths that will be searched recursively for PowerShell scripts. Any scripts found will become available for execution from within the workbench.
  4. Presentation style for script menu: This preference lets you choose how scripts are organized within the script menu. Choosing "Tree View" will cause scripts to appear under submenus based on their location. On the other hand, choosing "Flat View" will cause all scripts to appear, sorted alphabetically, directly beneath the script menu. The former option is useful for organizing a large number of scripts, whereby, for example, one might store image manipulation scripts in one directory and file packaging scripts in another. The latter option provides quicker access to a small-to-medium size set of scripts.
  5. Rescan interval (sec): This field defines the number of seconds between rescans of the script lookup path. Script additions and deletions are only discovered during rescans. Note that setting the value too low will have a negative impact on system performance.
  6. Show scan output in console: Selecting this option will causes the plugin to display the results of rescans in PowerShell console.
  7. Show script command line in console: By default, when you execute a script from the workbench, the command line is displayed in the PowerShell console, followed by the script output. Deselecting this option will hide the command line.

Opening a PowerShell

Anytime you select a resource in the workbench that is backed by a file or directory in the local filesystem, selecting "Open PowerShell..." from the context menu will create a new instance of the Windows PowerShell in that location. So for example, if you were to select a Java compilation unit in the "Package Explorer" view, right-click, and select "Open PowerShell..." an instance of the shell would open in the same directory as the compilation unit.

Working with Scripts

Although opening a PowerShell at a certain location is useful, the real utility of the PowerShell Junkie plugin lies in the way it integrates scripts into the workbench. In this section, we'll explore in detail how you can take advantage of these capabilities.

PowerShell Junkie adds a script menu to the toolbar and to the run menu that is populated with the scripts it found by scanning the script lookup path as defined on the preferences page. These scripts are PowerShell scripts, ending with the extension .ps1, that either you or someone else authored. To understand how these scripts are written, let's look at how they are executed by the plugin.

The user's selection within the workbench affects the way a script is executed. If there is no selection at all, the script will be executed in the workspace root with no command-line arguments. (Note: The word "in" refers to the working directory, so "in the workspace root" means "with the working directory set to the workspace root".) If the selection is exactly one directory, the script is executed in that directory with no command-line arguments. Finally, if the selection is a file, or a mixture of files and directories, the script is executed in the workspace root and the absolute pathnames of all selected files and directories are passed as command line arguments.

Therefore, if you want to write a script for the PowerShell Junkie, it will ideally handle two situations:

  1. No command line arguments, meaning that it should operate on the entire working directory (possibly recursively).
  2. One or more file or directory names as command line arguments, meaning that it should operate on those.

At this point, an example may be helpful. Let's extend Eclipse by adding the ability to compute the MD5 hash of a selected file. Take a look at the script below that accomplishes this:

Compute MD5 Sum.ps1
 1  $algorithm = "MD5"
 2  $Algo=[System.Security.Cryptography.HashAlgorithm]::Create($algorithm) 
 3  if ($Algo) {
 4    $files = $args
 5    if ($files.length -eq 0) {
 6      $files = (dir)
 7    }
 8    foreach ($file in $files) {
 9      $Fc = gc $file
10      if ($Fc.length -gt 0) {  
11        $Encoding = New-Object System.Text.ASCIIEncoding 
12        $Bytes = $Encoding.GetBytes($Fc)
13        $Hash = $Algo.ComputeHash($Bytes)
14        $Hashstring = ""
15        foreach ($byte in $hash) {$hashstring += $byte.tostring("x2")}
16        $out = new-object psobject
17        $segs = $file.ToString().Split('\')
18        if ($segs.length -gt 0) {
19          $out | add-member noteproperty File $segs[-1]
20        } else {
21          $out | add-member noteproperty File $file
22        }
23        $out | add-member noteproperty ($algorithm + ' Hash') $Hashstring
24        $results += ,$out
25      }
26    }
27  }
28  $results
After initializing a HashAlgorithm instance, the script checks to see how it was invoked: no command-line arguments causes it to process all files in the current directory (lines 5-7), otherwise, the command-line arguments are treated as a list of filenames to be processed (line 4). The actual hash computation is performed on each file between lines 10 and 24 before the results are returned on line 28.

Future Directions

The next version of PowerShell Junkie will include a bona fide embedded PowerShell console that users will be able to interact with, and that all script invocations will be sent to. Apparently, powershell.exe is not an embeddable ActiveX object, but instead I would need to roll my own.

License

The PowerShell Junkie for Eclipse is available under the terms of the GNU General Public License.

Feedback

As always, I would like you feedback on this tool, so please feel free to send me an email with any comments for improvement. User feedback is one metric that helps determine how much time I spend improving a product.

In addition, if you've developed any cool scripts that you think would be useful to the developer community at large, please send them to me so I can post them up.