Archive

Author Archive

A better approach to formatting in PowerShell

December 12, 2014 Leave a comment

Even without a traditional user interface, it is important to separate the presentation layer from the data processing layer.

For all of its strengths, Windows PowerShell is not without its fair share of weaknesses.  One of the weaknesses in PowerShell that has been with the scripting language since version 1.0 is how it handles formatting.  When it comes to formatting data, you have two options: use one of the core Format-* cmdlets (Format-Table, Format-List, Format-Wide, or Format-Custom), or create a format ps1xml file that defines one or more formats for a specific type in an XML document.  The former only works for the script consumer, because the core Format-* cmdlets actually convert rich object data into format data, not bearing any resemblance to the original objects with properties and methods that you started with.  The latter works for the script author as well as the script consumer, however it is significantly more complicated to implement, and it has its share of limitations as well (for example, once a default format is defined for a type, a script author will not be able to have the results of their script rendered in another format by default without taking some extra steps that shouldn’t be necessary).  The end result of these limitations and complications is a formatting system that is not fully leveraged in the majority of PowerShell scripts that are shared among the community.

I recently found myself looking at this problem and asking myself one more time if I could do better (it is a problem that I had looked at in the past).  The variety of formats that are available is encouraging, and you can generate some pretty useful report-like output when you take the time to leverage the formatting system.  But something would have to be done to fix the user experience.  The end goal I was envisioning was quite clear: a distinct separation of the presentation layer from the data processing layer such that formatting information would never get in the way of data processing.

Fortunately, PowerShell is quite well set up for this type of change.  Every command that you run has its results sent through the Out-Default cmdlet.  Out-Default internally knows what to do with whatever you throw at it, whether that be object data or format data (which really is object data as well, but I’m making a distinction here).  When you return an object from a command to the console, if that object is not format data, Out-Default internally will look at the object type, identify the default format for that type, convert the object into format data and then output that format data to the console.  If Out-Default does receive format data instead because the object was already converted into format data, it simply renders that format data in the console.  Given that is the case, it should be possible to modify the core Format-* cmdlets so that they attach format data to the objects that they format instead of converting object data into format data, and it should also be possible to modify the core Out-Default cmdlet so that it detects format data when it is attached to an object and renders that format data directly to the console instead of looking at the object type to decide what format to use.  That, my friends, is exactly what I did.

FormatPx is a module made up of a nested binary module that defines five proxy cmdlets (Format-Table, Format-List, Format-Wide, Format-Custom, and Out-Default) plus one new cmdlet (Format-Default) and a script module that automatically applies the Force parameter whenever Format-Table, Format-List, or Format-Wide are used.  It changes how the core Format-* cmdlets work as described above, and it also makes it easier to get format information from types that include a custom format as their default.  Here’s a short screencast showing FormatPx in action:

(Note, do not adjust your computer, there is no audio in this screencast.)

Better PowerShell formatting with FormatPx

That’s a decent overview of what you get with the FormatPx module – a separation of the formatting layer from the data processing layer, giving you much more control over the presentation of your script results without ps1xml file complexity while still allowing script consumers to view the results in whatever format they like.  What do you think?

Kirk out.

Categories: PowerShell Tags: , ,

My favorite PowerShell one-liner

November 3, 2014 4 comments

Happy Monday everyone!  I thought it might be a fun way to start the week by sharing my favorite PowerShell one-liner.  One-liners are nostalgic for me, because I learned a lot of cool programming tricks from one-liners in issues of Compute magazine a long time ago.  The PowerShell one-liners that grab my interest the most are those that can do many different things in the simplest pipeline possible.

So far, eight years into using PowerShell, this is my favorite PowerShell one-liner:

image

At first glance, can you tell what this does?  Take a minute to think about it, I’ll wait.

 

 

 

Ok, now that you’ve thought about it a bit, you might have realized that it is not that easy to figure out everything that this does, so let me break it down into parts.

gci –r -force

If your familiar with PowerShell, this part is pretty straightforward.  gci is an alias for Get-ChildItem, and this command tells PowerShell to get a recursive (-r) directory listing of all files and folders, including any hidden files or folders (-force), starting from the current location.

measure -sum PSIsContainer,Length -ea 0

measure is an alias for Measure-Object, a cmdlet that can perform calculations on a collection of objects that it is passed from a pipeline.  In this case, we’re going to aggregate (-sum) two values from properties in this collection: PSIsContainer and Length. We’re also going to hide any errors we get from trying to access folders that we don’t have access to (-ea 0).  How Measure-Object measures the objects it receives is the most interesting part of this pipeline.

You can probably guess why we would want to calculate the sum of the Length property, but why would you want to calculate the sum of the PSIsContainer property?  PSContainer is a boolean value that indicates whether or not an item returned from Get-ChildItem is a container.  A value of $true indicates it is a folder, and a value of $false indicates it is a file.  When you calculate the sum of a bunch of boolean values in PowerShell, PowerShell first converts those boolean values into their integer equivalent.  $false implictly converts into a value of 0, and $true implicitly converts into a value of 1.  Given this is the case, by aggregating the PSIsContainer property, you’re effectively calculating a sum of the number of containers in the result set.

$di,$fi =

PowerShell allows you to assign a multi-valued result set to different variables by separating the variables with a comma.  In such an assignment, the first object returned will be assigned to the first variable, the second assigned to the second variable, and so on until the last variable which will contain all remaining objects.  In this case, we’re assigning two variables, and each will contain a single value.  Since we’re aggregating two properties (PSIsContainer and Length), Measure-Object will return two objects containing measurement information: one for PSIsContainer and one for Length.  The measurement information for PSIsContainer will be assigned to $di and the measurement information for Length will be assigned to $fi.

Also, in addition to calculating the aggregate for the two properties, Measure-Object also counts the number of objects that it processes to perform that calculation and includes that count in the result as well.  In cases where an object does not have a property that is being measured, that object will not be included in the count, so folders, which do not have a Length property, will not influence the values returned in the second measurement information object that is assigned to $fi.

Putting it all together

When I run the aforementioned one-liner on my PowerShell folder, here are the values I get from $di and $fi, respectively:

Count    : 6516
Average  :
Sum      : 2012
Maximum  :
Minimum  :
Property : PSIsContainer

Count    : 4504
Average  :
Sum      : 917710488
Maximum  :
Minimum  :
Property : Length

For the $di object, the Sum property identifies how many directories were found in the search.  This was determined by aggregating the integer value of the PSIsContainer property on the objects that were measured.  The Count property identifies how many items altogether were processed (directories and files, both of which have a PSIsContainer property).

For the $fi object, the Sum property identifies the total size of all files that were found in the search, and the Count property identifies how many files were found in the search.

Put this all together, and you have a PowerShell one-liner that gives you folder stats, identifying the number of files and folders in a folder as well as the total size of all files in that folder.  Neat, huh?  I think the reason why I like this PowerShell one-liner the most is because it demonstrates how a little creativity can be applied to get useful information that is not available in a native cmdlet from several sources in a simple and elegant manner.

What’s your favorite one-liner, and why?

Maybe I should start a meme with this.  Jeffrey Hicks, Shay Levy, Ed Wilson, June Blender, and Hal Rottenberg, tag, you’re it! Smile

Kirk out.

Categories: PowerShell Tags: ,

Raise your PowerShell game with HistoryPx, DebugPx and TypePx

October 31, 2014 Leave a comment

Recently I’ve been taking some of the most useful tricks and tools that I use in PowerShell to help me get my work done more easily and packaging them up in open-source modules.  I find these modules very useful in the work that I do, and wanted to share them with the community.  Each of these modules works on PowerShell 3.0 or later.  Here are the modules I am referring to:

DebugPx Includes ifdebug (conditional debugging when invoking commands with –Debug) and breakpoint (visual breakpoints in scripts; makes debugging from any host and with any editor much easier) cmdlets. link
HistoryPx Seamlessly upgrades the history capabilities in PowerShell, adding rich extended history information that you don’t get by default. link
TypePx Defines dozens of useful type extensions for common .NET types.  Brings foreach and where method support to PowerShell 3.0.  Includes type acceleration commands. link

The links in the table above will take you to blog posts that provide some more context about what these modules do.  If you’d like to raise your PowerShell game by using some cool new functionality in PowerShell 3.0 or later today, please give these modules a try and let me know what you think!

Happy Halloween!

Kirk out.

Making history more fun with PowerShell

October 30, 2014 3 comments

History is boring.

While I don’t believe that is true (I actually find history quite fascinating), it is certainly how I would have responded if asked about history while taking it in high school.  I’m not the only one who felt this way back then either.  When I asked my wife for her opinion about it, she told me that it was in her history class where she learned to write upside down with her non-dominant hand.

My problem with history is the presentation of it.  It can be fun and include a lot of contextual detail that is presented with zeal and that connects with the audience; or, like my high school history class, it can be presented as a timeline with boring details attached to it.

PowerShell automatically tracks the commands that you invoke in a history table that you can view at any time with the Get-History cmdlet.  If you invoke Get-History (or its alias, h) after you’ve been using PowerShell for a bit, you should see results similar to the following:

native powershell history

That’s not entirely useless, because it shows commands that we ran earlier, but as far as presentation goes it’s pretty boring, right?  Now, this is only the default format for history in PowerShell, and as with most data in PowerShell this screenshot doesn’t paint the full picture.  Each entry also includes other fields that are not shown in the default format, as can be seen here:

native powershell history expanded

The problem is, that’s all you get: a timeline with boring details attached to it.  The ExecutionStatus property tells me whether a command completed or failed, but the usefulness of that information is very limited because a value of Failed only means the command failed with a terminating error; if a command failed with a non-terminating error, then ExecutionStatus will simply indicate that the command completed, which is not very useful knowledge to have.  Start and end execution times are useful, but that means I need to do math to determine how long various commands took, or re-run the commands inside of Measure-Command.  There’s no zeal behind the presentation of this information.

Fortunately, PowerShell is extensible, so I decided I should be able to do better.  Here’s my view of what PowerShell history should be, as provided by HistoryPx:

powershell extended history from HistoryPx

HistoryPx is an open-source PowerShell module that transparently integrates into the PowerShell environment where it is loaded.  You simply load the module by importing it with Import-Module into an environment running PowerShell 3.0 or later, and once it is loaded it will start tracking extended history information for every command you run.  Extended history information includes the command duration, whether or not the command was successful, any output returned by the command, and any errors that were added to the $error log by the command.  When you invoke Get-History, the new default format for extended history information presents most of these details, or you can pipe the results to Format-List * to get all properties where you can see the core history properties as well as the extended information added by HistoryPx.

In addition to providing extended history information in PowerShell, the HistoryPx module includes one other feature that is quite useful.  It defines a double-underscore ($__) variable in the global scope that will always contain the output of the last command that was executed.  When the last command doesn’t have any output, $__ will be assigned a value of $null.  This comes in handy when you invoke a command that takes time to complete but forget to capture the results in a variable.

If you want to give this module a try, head on over to the HistoryPx page on GitHub where you can learn how to install or update it in your environment and read additional details provided in the readme file on GitHub.  If you are concerned about memory usage with this module, your questions are addressed in the readme as well.  This is only the first release of this module, and I am already starting to track features that I want to add to another release.  If you do give it a try, please let me know what you think, either in the comments on this blog post or in GitHub, and please log any issues you find on GitHub as well.

Thanks!

Kirk out.

Categories: PowerShell Tags: ,

Transform repetitive script blocks into invocable snippets with SnippetPx

October 21, 2014 2 comments

The more you write code, the more you notice patterns in the code you write.  This goes for PowerShell, C#, Ruby, and any other programming or scripting language under the sun.  Patterns are opportunities.  Opportunities to generalize them and define them as design patterns.  Opportunities to isolate blocks of reusable code in such a way that they can be reused so that you can follow the DRY principle in your work.  This article is about the latter of those two.

In recent months I have been working on trying to reduce duplication in my own work by keeping my modules and functions as DRY as possible.  One challenge with keeping code DRY in PowerShell is in deciding which is the most appropriate method to do so.  There are many opportunities to keep your code DRY in PowerShell.  You can create:

  • cmdlets
  • advanced functions
  • basic functions
  • unnamed functions (aka script blocks)
  • script files
  • type extensions for the Extended Type System (ETS)
  • classes with properties and methods, either in a .NET assembly that is imported into PowerShell, or if you’re using PowerShell 5.0 or later, in PowerShell itself
    Despite each of these extension points being available to PowerShell, they don’t always fit the scenarios you need them to, perhaps because they are not appropriate for the intended purpose, because they have some limitation that you can’t work around, or perhaps for some other reason.  For example, I find myself writing all of these, and there are certain pieces of code that I want to set up for easy sharing in many of these types of extensions without being an extension point itself.  When you have logic that you might use anywhere that you could write PowerShell, how do you set that up in such a way that you can consume it in all of those locations easily regardless of the machine you are running on, without taking a dependency on physical file paths?  That last point is important, because standalone ps1 files may be one possible answer to this need, except invoking them requires knowing where they are, and when you invoke them you must decide whether to dot source them or call them with the call operator, which in turn means you must know the implications of such a decision.  Plus, when their use spans all of PowerShell (any script, any module, any function), where do you put them without having to burden the consumer with extra setup work?  And how can you create more of these while making them discoverable, and able to be added to or removed from a system with ease?
    Snippets are a great answer to these questions.  Snippet is a programming term that has been around for many years, and it generally refers to a small region of re-usable code.  They are also a lot more than a fancied-up bit of copy/paste functionality.  They have parameters, or fields, that control how they run.  They can surround text you have selected, or simply insert into the current cursor location.  Most importantly however, for me at least, is that snippets can be invocable, and that, my friends, is key because when you’re trying to maintain a DRY code base, you don’t want to inject boilerplate code blocks in many locations…you want to invoke them.

With snippets being a great solution to this problem, I decided to try to build a snippet-based solution that would allow for discoverable, invocable snippets.  I wanted this solution to be able to find snippets regardless of what computer you were running on, as long as you followed a few simple rules.  I wanted this solution to keep snippet definitions as simple as the creation of a ps1 file.  And I wanted this solution to allow for snippets to be invoked in the current scope by default, and in a child scope as an option.

Enter SnippetPx.  SnippetPx is a module that provides two very simple commands, plus a handful of snippets.  The two commands are Get-Snippet, which retrieves the snippets that are discoverable on the local computer, and Invoke-Snippet, which allows a snippet to be invoked by name, with or without parameters, in the current scope or in a child scope.  With this module, any ps1 file that is stored in a “snippets” folder inside of the current user’s Documents\WindowsPowerShell folder, the system %ProgramFiles%\WindowsPowerShell folder, or as a subfolder under the root of any module that is discoverable via PSModulePath will be discoverable as a snippet.  You can see some examples by looking in the “snippets” folder in the SnippetPx module itself, or by looking in another module that includes some snippets such as the TypePx module.  Also, any snippet that is discoverable by this module is invocable by the Invoke-Snippet cmdlet.

Since creating this module, it has quickly become a core module that my other modules take a dependency on in order to keep my code DRY.  That effort has already paid off for myself because it has allowed me to update a block of code that is defined in a snippet and only have to make that change once, while all other locations where that snippet is invoked simply run with the new code change.  I encourage you to give it a try and see if it helps you remove what might otherwise be repetitive code that is more difficult to maintain than it should be.

If you would like to give SnippetPx a try, you can download Snippet from GitHub (yes, it is open source, along with the binary module where Invoke-Snippet and Get-Snippet are implemented) or from the PowerShell Resource Gallery (aka the PowerShellGet public repository).  Feedback is more than welcome through any means by which you want to communicate with me: the Issues page for this project on GitHub, social media channels, comments on this blog, etc.  In the meantime, I will continue to identify opportunities to create more snippets that will be useful to others and push them out either as updates to SnippetPx or in the modules where those snippets are used.

One more thing: if you do decide that you want to create some snippets of your own, there are some useful details in the Notes section of the help documentation for the Get-Snippet and Invoke-Snippet cmdlets.  I strongly recommend giving that a read before you dive into creating invocable snippets, as it provides some additional detail on how snippets are discovered as well as recommendations on the naming of your snippet ps1 files.

Thanks for listening!

Kirk out.

PowerShell debugging, amplified

October 20, 2014 2 comments

This article is about the PowerShell module that I am most proud of and that I have personally found more useful than any other module I have created.  I hope that you enjoy it as much as I do.

Every programming language must be designed with debugging in mind, and PowerShell is no exception.  No matter who you are, no matter how well you know the language, you will inevitably come across something that isn’t working like it should.  What you do in these situations depends on your comfort level with programming and debugging.  For some, this means adding lines to the code that produce extra output so that they can get a handle on what is going on.  For others, this means rolling up the sleeves and stepping through the code in a debugger.  And there are others still who aren’t comfortable enough with programming or debugging, so they turn to others for assistance.  Regardless of which of these approaches you would take, I believe I have a module that can help.

DebugPx is a free, open source PowerShell module that was designed to make it easier to troubleshoot problems in PowerShell code.  It comes with two core commands: breakpoint and ifdebug.  It also includes a few helpful utility commands to control how the breakpoint command works.  The core commands are described as follows:

breakpoint The breakpoint command is used to trigger a breakpoint at the current location.  By default, the breakpoint command causes Windows PowerShell to immediately enter the debugger whenever it is invoked in an interactive session.  If it is invoked with its optional ConditionScript parameter, it will only trigger the breakpoint if the script block expression that was provided for the ConditionScript parameter evaluates to true.  It also accepts an optional string Message parameter, and it will write the message provided to this parameter to the current host whenever the breakpoint is triggered.
ifdebug

The ifdebug command is used to identify a block of PowerShell script that you only want to run in one of two scenarios: when you invoke a command with -Debug, or when the $DebugPreference variable is set to anything either than SilentlyContinue or Ignore.

Both of these commands are very powerful and they can make troubleshooting problems in PowerShell scripts a lot easier.  Let me provide some background details so that you understand the problems that these commands solve, problems that still exist in PowerShell 5.0 today.

PowerShell version 1.0 did not come with breakpoint support.  It had a debugger, and that debugger is still useful today, even though a new debugger has been in PowerShell since version 2.0.  It had a -Debug common parameter that was available on every cmdlet and, since version 2.0 of PowerShell, on every advanced function as well.  It had a $DebugPreference built-in variable.  It also had a Write-Debug cmdlet that allowed you to have some level of debugger control over your scripts.  The behaviour of the Debug common parameter, $DebugPreference and how those affect Write-Debug behaviour is quite interesting, and once you understand how those work together, you may see why their implementation leaves some opportunities on the table.

When you invoke a cmdlet or an advanced function with the -Debug common parameter, PowerShell internally sets the $DebugPreference variable value to Inquire within the scope of that command.  As far as PowerShell preference variables go, a value of Inquire means that PowerShell will prompt the user to ask if they want to continue the execution of the associated command, stop the associated command immediately, or enter a the debugger at that point, allowing the user to invoke PowerShell commands to troubleshoot the system.  This behaviour seems like it was designed to fill the void when PowerShell did not have breakpoints, because it allowed scripters to enter the debugger as long as they threw a few Write-Debug commands into their scripts.

The problem with this approach is that it tries to do too many things and mixes up several distinct needs in the process.  The ability to write debug information to the debug stream during script execution, and the ability to enter the debugger on a breakpoint at a specific location in a script are two distinct needs that are mashed together when they shouldn’t be.  Another limitation with this approach is that scripters can’t simply change the way a script is invoked in order to gather additional debug information from an environment where they aren’t able to debug with breakpoints as easily, because using -Debug meant prompting the user every time Write-Debug would be called, and often you’re trying to help a user who is having a problem in this scenario, not confuse them by asking them to continue a bunch of Write-Debug calls while telling you what is happening.  Yet another limitation with this approach is that there is no easy way to include PowerShell commands inside of an advanced function or script that will only execute when you are debugging, allowing a command or script author to include support for generating debug output but only when that command or script is invoked with -Debug.

When breakpoints were later added along with a new debugger in PowerShell 2.0, PowerShell script authors were suddenly able to set breakpoints in their scripts, either visually using PowerShell ISE or for conditional breakpoints, command breakpoints, or variable breakpoints using the Set-PSBreakpoint cmdlet.  This functionality solved the need for breakpoints, and it started the separation of the need to enter the debugger from the need to be able to write debug output; however, the -Debug common parameter behaviour didn’t change, so there still wasn’t a good vehicle for writing information to the debug stream without any requirement for user interaction during the process.  Also, while this breakpoint functionality was useful, it came with its own share of limitations.  If you were working in an environment other than PowerShell ISE that didn’t have visual support for setting PowerShell breakpoints (such as notepad++, sublime text, or some other awesome editor), you simply had no choice but to work with the Set-PSBreakpoint cmdlet, which isn’t a very friendly way to set breakpoints in PowerShell, putting it mildly.  Also, if you were debugging PowerShell code across multiple sessions, you would have to reset your breakpoints every time, either manually or using a profile or some other script, none of which is very practical.

DebugPx was designed to solve almost all of these problems with the breakpoint and ifdebug commands.  With the DebugPx module installed and discoverable via the PSModulePath environment variable, you can trigger a breakpoint at a specific location by simply invoking the breakpoint command (or the bp alias, for short).  These breakpoints are identifiable in any editor because you can see the commands in the files where they are used.  They work in unsaved files.  They even work in an interactive PowerShell prompt, or inside of a block that you run in PowerShell using copy/paste, or inside of a block that you select in ISE and run by using the Run Selection (F8) feature.  They are properly ignored if they are inside of a function that uses the System.Diagnostics.DebuggerHidden attribute.  They are conditional if you invoke them with a condition script block (which would map to the first, ConditionalScript parameter), or unconditional otherwise.  They can be globally enabled or disabled using the Enable-BreakpointCommand or Disable-BreakpointCommand commands.  They will only cause PowerShell to enter the debugger if they are encountered in an interactive session.  And if you want a message to be displayed when the breakpoint activates (for example, as a reminder why you wanted to break when that obscure scenario that you previously couldn’t catch occurs), you can pass a message to the -Message parameter and the breakpoint will output the message to the host when the breakpoint is triggered.

If you are debugging in other scenarios, where you may not be able to use the breakpoint command to enter a debugger because you are not in an interactive session (such as in background jobs, scheduled tasks, Azure Automation or Service Management Automation scripts, or perhaps in a remote customer’s environment), you can include rich debug information inside of an ifdebug command script block, and anything that is output from inside of that script block will automatically be written to the debug stream without prompting the end user.  This includes object data, text strings, or anything else you want to write to the debug stream in order to figure out what is going on when that script runs.  If the command containing ifdebug is not invoked with -Debug, PowerShell will simply skip directly over that script block, avoiding running debug logic when it is not needed, which is better for performance.

Both the breakpoint command and ifdebug can be used in the middle of a pipeline as well.  This is important because it allows for writing debug information to the debug stream or triggering a breakpoint in the middle of a pipeline during the processing of that pipeline.  If you use the breakpoint command in a pipeline and you either have the breakpoint command disabled or you are in a script block with the DebuggerHidden attribute set, or if you use the ifdebug command in a pipeline and you didn’t run the script with -Debug, both the breakpoint and the ifdebug commands will simply pass the pipeline object down to the next stage in the pipeline for additional processing.

At this point, you’re probably getting a feeling for the kind for the power that these commands provide.  Let me show you a few simple examples that demonstrate how you might use them in practice.  You can try these examples at the command prompt in the PowerShell host of your choice, or in a script file, or in functions you write, or script module files, or workflows, etc.

Enter the debugger in the middle of a series of commands

$services = Get-Service wuauserv,bits
breakpoint
Restart-Service -InputObject $services -WhatIf

Enter the debugger conditionally in the middle of a pipeline (using the bp alias)

gsv audiosrv,bits,wuauserv | bp {$_.Name -eq ‘bits’} | spsv -WhatIf

Display a message as you enter the debugger reminding you why you are doing so

Get-Process -Name Idle,PowerShell,Explorer |
   
breakpoint {$_.Id -eq 0} -Message ‘Process ID is zero???’ |
   
Format-Table

Disable the breakpoint command so that you can run without debugging

Disable-BreakpointCommand
gsv audiosrv,bits,wuauserv | breakpoint {$_.Name -eq ‘bits’} | spsv -WhatIf
‘See, the breakpoint command did not cause PowerShell to enter the debugger!’

Re-enable the breakpoint command so that it functions normally again

Enable-BreakpointCommand

Skip over breakpoints in a script block by using the DebuggerHidden attribute

& {
    [System.Diagnostics.DebuggerHidden()]
    param()
    breakpoint
    ‘The breakpoint command is effectively disabled in the current scope.’
}
‘But it still works in scopes that do not use the DebuggerHidden attribute.’
breakpoint

Create a function that generates some useful debug information

function Test-IfDebug {
    [CmdletBinding()]
    param()
    ifdebug {
        # This may be useful when debugging, but I wouldn’t want to gather
        # this information in everyday use. It is an exaggerated example of
        # what you might want to collect when debugging something.

        ‘*************** Current Operating System ***************’
        Get-WmiObject -Class Win32_OperatingSystem | Format-List *
        ‘*************** Running Services ***************’
        Get-Service | Format-List *
        ‘*************** Running Processes ***************’
        Get-Process | Format-List *
    }
    Get-Service w*
}

Invoke that function without debugging

Test-IfDebug

Invoke that function with debug output turned on

Test-IfDebug -Debug

Compare the performance of the two

Measure-Command {Test-IfDebug}
Measure-Command {Test-IfDebug –Debug}

I think those examples provide a good demonstration of how these commands work.  As I hinted at earlier in this article, I am super-excited about this module and I’m thrilled that I can share it with you now.  If you want to give it a try, install the latest version in your environment by following the instructions on the DebugPx project page on GitHub.  I have a few more features planned for this debugging toolkit, and I would be very happy to entertain your ideas as well, so please let me know what you think!  Have fun debugging!

Kirk out.

Make working with types in PowerShell easier with TypePx

October 8, 2014 1 comment

It is often the little things in life that make a big difference.

1.week.ago

Isn’t that a beautiful programming example? That wasn’t a heading you just read, it was a piece of code. It’s simple, it’s very expressive, it’s self-documenting, and it’s pretty hard to misunderstand the intent when you look at it.  That’s not PowerShell code though…it’s Ruby.

Over the past year or so I have taken some MOOCs offered by Coursera and edX.  After playing games that I created for my assignments in Python, I started learning more about Ruby.  Ruby is a wonderful, expressive, dynamic programming language with a vibrant community that has a lot in common with PowerShell.  It also offers more functionality than PowerShell does in some areas, and I started missing some of that expressive syntax that I was able to use in Ruby when I would switch back to working in PowerShell.

Here’s how you can accomplish the same task natively in PowerShell:

(Get-Date).AddDays(-7)

It doesn’t offer quite the same punch, does it?  Between all of the brackets, the need to call out to Get-Date, and adding negative days to be able to get a timestamp representing 1 week ago, it just isn’t quite as palatable to me.

The beauty of a more mature language like Ruby is that it’s not just about the availability of a week method for numeric classes, nor the availability of an ago method for a time span class.  It’s the availability of years, months, weeks, days, hours, minutes, seconds and milliseconds, all in singular or plural.  It’s the availability of ago to refer to a date in the past, or fromnow to refer to a date in the future.  Want UTC instead of local time?  There’s a inutc method too, as well as so many other cool things.  Small details matter.

Fortunately, like Ruby, PowerShell is also a dynamic language that has all sorts of extensibility built-in, allowing me to continue to enjoy expressive, Ruby-like syntax, even while working in PowerShell.  I know PowerShell’s extensibility quite well, and have been leveraging it since version 1.  These Ruby features inspired me to create a new PowerShell module, bringing some of the method simplicity, elegance, and usefulness from Ruby into PowerShell for the rest of the PowerShell community to enjoy.  I called it TypePx.  It’s open source, and it defines useful type extensions and simplifies type acceleration to reduce much of the typing that would otherwise be required when working with types in PowerShell.  Also, it works on PowerShell 3.0 or later, and it’s available now.

Here are examples of some of the beautiful things you can do with TypePx:

# Get a timespan in a human readable way
(10).years
(2).months
(12).hours

# Use relative dates, as mentioned above
(1).week.ago
(30).days.ago.inutc

# How about using relative dates in a practical way with event logs?
Get-EventLog -LogName System -After (2).Days.Ago
# You can use it when monitoring file age too
dir *.ps*1 | where LastWriteTime -lt (1).year.ago

# Compact (remove null values) from an array
$a = 1,$null,2,$null,3
$a.Compact()
# Return unique elements in an array
$a = 1,1,2,3,3,4,4,5,6
$a.Unique()
# Reverse an array
$a = 1,2,3,4
$a.Reverse()
# Slice an array into chunks
$a = 1,2,3,4,5,6
$a.Slice(3)[1]
# Flatten a multi-dimensional array
$a = (1,2),(3,4),(5,6)
$a.Flatten() 

These are only a few examples of what you can do with TypePx.  Have you ever worked with hashtables and noticed how annoying collection management is when it comes to hashtable values?  June Blender was looking for a solution to this the other day, and TypePx solves that problem.

$ht = @{}
$ht.Add(‘A’,’This is not a collection’)
$ht.AddArrayItem(‘B’,’This is a collection’)
$ht.AddArrayItem(‘B’,’This adds to the collection’)
$ht

By now you have probably seen the cool foreach and where “magic” methods that were added to PowerShell as part of version 4.0.  What about version 3.0 though?  TypePx solves that problem too by adding foreach and where methods to PowerShell 3.0 with all of the bells and whistles that are available in PowerShell 4.0.

Here are a handful of other problems that TypePx makes easier:

  • comparing an array to multiple search strings at the same time in a single call;
  • comparing a string to multiple search strings at the same time in a single call;
  • calculating the sum of an array or of a property on a collection of items;
  • accessing the value hidden inside of a SecureString;

TypePx isn’t just about type extensions either.  It’s also about type acceleration.  In fact, it was originally released as my old TypeAccelerator module that I had previously shared on PowerShell Magazine and posted in the PowerShell Resource Gallery, but then I renamed it and included full documentation for the *-TypeAccelerator commands as well as all of the type extension goodness I described above.  Type acceleration support is a great way to improve the quality of your PowerShell code when you are working with long type names.  Rather than explain it here though, you might want to give my article on PowerShell Magazine a read if you haven’t already since it explains what type acceleration is in more detail and gives examples that show why you might want to know more about it.

To download the latest version of TypePx, please refer to the installation instructions in the readme documentation on GitHub.

That wraps up this article.  Please let me know what you think in the comments!

Now if only PowerShell would properly recognize numeric literals as objects so that you could invoke methods and access properties on them without having to use brackets…

Kirk out.

Follow

Get every new post delivered to your Inbox.

Join 282 other followers

%d bloggers like this: