Supporting -Whatif, -Confirm and -Verbose in *localized* scripts

Way back in February last year, Jeffrey Snover posted an article on the PowerShell blog describing how to support -Whatif, -Confirm and -Verbose in PowerShell functions and scripts.  As he states in that article, this is very important because it shows you how you can give consumers of your function or script the same safeguards that they get in PowerShell cmdlets.  If you haven’t read the article and you are sharing scripts that make system changes with others, you should take a few minutes to read it and strongly consider (read: this is really a good idea) using the Should-Process function it describes in those scripts. You can find that article here.

That said, there is one issue that I do have with the implementation of the Should-Process function as posted in that article — it is full of hard-coded English strings when it doesn’t need to be.  At the time of this writing PowerShell is already localized in 10 languages and is already being used heavily internationally, so it makes sense to provide localized PowerShell scripts whenever possible, right?  That might sound daunting, but since the strings used in the output generated by -Whatif, -Confirm and -Verbose are already stored in the PowerShell resource string tables, we should be able to extract them using PowerShell itself and then use the extracted, localized strings in our scripts.

So how do we go about doing this?  The answer is pretty simple: use Get-PSResourceString.

Get-PSResourceString is part of the CmdletExtensionLibrary.ps1 script that I published a little while ago.  It is used to retrieve localized resource strings from the PowerShell engine and host.  Using that function I was able to convert all of the strings that are used in Jeffrey’s version of the Should-Process function into calls to Get-PSResourceString so that they will be localized.  The end result is a localized1 version of the Should-Process function that I have also included in the most recent version of the CmdletExtensionLibrary.ps1 script.

Both of these functions are quite long and not very blog-friendly, so I’ll leave it up to you to open the CmdletExtensionLibrary.ps1 script file in the editor of your choice and take a look at them.  This script originally started as a small project to dig into dynamic parameters, but I’ve found the functions I created in that work so useful myself that I’ve been adding more and more to it, a trend that will likely continue.  If you only want the Get-PSResourceString and Should-Process functions, you can simply copy them out of that script and use them however you like.  And of course, if you see any issues with the functions in this script, have any questions, or want to share suggestions on how to improve it even more, I’m more than happy to listen.

Kirk out.

1 Note that while I’m trying to provide localized scripts as well as functions to facilitate writing localized scripts to support the international PowerShell community, all documentation for these functions is only in English and there are a few hard-coded strings only in English for variable and alias descriptions and one output message in English for the script file itself.  Aside from those few exceptions (that you’re not likely to see anyway), if you’re using a localized version of PowerShell the functions in the CmdletExtensionLibrary.ps1 file should properly support your localized version.

Technorati Tags: , , , ,

Advertisements

2 thoughts on “Supporting -Whatif, -Confirm and -Verbose in *localized* scripts

  1. Thanks for letting me know, Wes. I tried contacting you directly about this through your blog but you never replied so I don’t know if I reached you or not.

    At any rate, I haven’t been able to reproduce this issue at all. I suspect it might just have been a hiccup. A possible workaround is to go to the home page of the PowerShell Community, click on Scripts, then on the ‘access it here’ link to get to the old script vault, and lastly on the Cmdlet Extension Library link. If anyone gets this error again or if anything else stops you from downloading the file, please let me know.

    Kirk out.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s