More Flashing Fun

talkbubbleI received a lot of interest in my Invoke-Flasher script. One comment I received on Twitter was for a way to use it interactively in a script. In essence, he wanted a flashing Read-Host so I took my original concept and tweaked it until I came up with a Read-Host alternative I simply call Read-Host2. This function will only work in the PowerShell console, NOT the PowerShell ISE.

The main tweak I made was to collect all the typed keys until Enter is pressed. I have a Switch construct to also eliminate the Shift key. The assumption is that you are writing text so this should be the only non alphanumeric key you would use. The message prompt will keep flashing until you start typing. I also emulated echoing text to the screen, including password masking if you use AsSecureString. The last change is a new parameter to allow you to flash the foreground color instead of the background color. There are several examples in the comment-based help.

Here are some screen shots.
Read-Host2-01

Read-Host2-02

I hope you’ll let me know what you think.

Press PowerShell Pause to Continue

talkbubble Everyone once in a while I come across a PowerShell script that behaves like an old-fashioned batch file. Not that there’s anything wrong with that, but often these types of scripts put in a pause at the end of script so you can see that it finished. You might have seen a command like this in a PowerShell script.

Sure, it works but is hardly elegant PowerShell. First, I’m not sure you even need it. If you want to draw attention to the fact that your script ended, consider using Write-Host with a foreground (or background color).

But if you truly need the user to acknowledge that the script has finished you can use Read-Host.

I’m piping the command to Out-Null because I don’t care what the user enters, only that they hit a key.

If you prefer a move visual cue, you can always resort to the Wscript.Shell COM object.

The popup will remain until the user clicks OK. Or you can change the second parameter from 0 to another value to have the popup automatically dismiss after the specified number of seconds.

So if you need a refreshing pause, there are plenty of PowerShell options for you to use.

UPDATE:
After someone posted a comment I realized there is yet another way to do this. Add these two lines to the end of your PowerShell script.

This is very similar behavior to Read-Host but with a bit more flexibility.

Friday Fun – A PowerShell Console Menu

When working in PowerShell, and especially when scripting, you might want to give the user a choice of actions. For example, you might develop a configuration script that another admin or technician will run. Perhaps one of the steps is to configure networking depending on their location so you want to give the person running the script a menu of choices. Here’s one way you might accomplish this, without resorting to graphical tools or WinForms. Continue reading

New Comment Help

If you follow my blog I’m sure you noticed that I post a lot of advanced functions and scripts. While I don’t expect every one to be developing advanced functions, the closer you can get the more powerful your work. With the Scripting Games approaching I thought I’d offer up a little something to help take your scripts to the next level and that is adding comment based help. This is a specially structured comment block that you can insert into your functions and scripts. The comment block is parsed by Get-Help to produce the same type of help output you see with cmdlets. But creating the help comment can be tricky so I wrote a script based wizard. Continue reading