Friday Fun: Aren’t You Special

crayonsIf you’ve been following my work for any length of time you know I am constantly going on about “objects in the pipeline”. But PowerShell is flexible enough that you should be able to use it as it meets your needs, provided you know the limitations for whatever path you take. Sometimes you really just want to read information on the screen and you want it to be as meaningful as possible. For today’s fun let me show you how to add some special characters that might be just what you need to spice up an on screen display.

First, you need to know what you can display and how. You will be using the [CHAR] class. Don’t worry too much about details. You can use the [CHAR] class to display characters other than the usual alphanumeric and symbol characters on your keyboard. I also need to point out that everything I am showing you will only work in the PowerShell console, not the ISE. Here’s how you can see some of these characters.

This snippet is the same as displaying [char]1 through [char]6.

You’ll also find these ranges of interest.

Or use this one-liner to display them all.

Now that you know what is possible, let’s use some of these characters. How about a visual indicator if a service is running or not?

I’m creating a custom property called Indicator which will display a special character based on the service status. To make it pretty to read I’m formatting as a table and hiding the header since I can tell from the output everything I need to know.

Remember I mentioned limitations? The special characters cannot be sent to a file, converted to HTML or exported. All you can do is run the code and read the screen. But that may be all you require. Well, technically you can direct the output to a file with Out-File. You won’t see the characters in Notepad but you should when you use Get-Content.

Let’s take the service example to another level. Using the Win32_Service class from WMI, we can tell if a service is set to Auto start but is not running. Let’s use the same type of indicator for the service status and an indicator if an Auto start service isn’t running.

I used [CHAR]15 as my indicator. You know what? Since we know we can’t do anything with the output other than read it on the screen (you are allowed to do this), let’s go all out.

This code again uses WMI and the special characters, but then it looks at special character properties I created and determines a color to use for the output. The tricky part was converting the [CHAR] class back into an integer so that I could determine what color to use.

There are probably other ways to achieve the same result, but this works for me.

So have some fun with these special characters. Realize there are limitations to what you can do with the output, but there are probably no limitations on how you might use them. If you come up with something interesting I hope you’ll share.

Let’s have fun out there.

Friday Fun: Creating Sample Files

documentNot too long ago I came across a PowerShell snippet, probably via Twitter, that showed how to use a few classes from the System.IO namespace to create dummy files. Sadly I forgot the record where I saw this first mentioned. What I liked about the code I saw was the ability to create a dummy file of any size. You’re probably wondering why you would ever want to do this. Way, way back in the day when storage was expensive I used to create a few 100MB of dummy files using an old command line utility which I think was called creatfil.exe. The reasoning was that if the server started running out of disk space unexpectedly, I could delete the dummy files and buy myself a little time and breathing room. Fortunately, I think we’ve advanced where that technique is no longer necessary.

But, where you might want some dummy or sample files, is to simulate a user folder with a variety of files. You could use this test directory to hone your PowerShell scripts. Any you may have your own reasons for needing a bunch of sample files. So I took the original code and ran with it, as I usually do. I came up with a function called New-SampleFile.

The function will create a randomly sized file in the specified folder. The default is the current location. As you look through the code you’ll see I’ve added a test to verify the path and to make sure it is a FileSystem path and not something like the registry. Also by default the function will use a randomly generated file name which will be something like rcstwqyg.34e. Personally, I’m going to specify a filename. The function will create the file of a random size between 5 bytes and the value of the MaximumSize parameter, which has a default of 100KB.

The function will not overwrite existing files unless you use –Force. Nor will you see any output unless you use –Passthru. Even those are common parameters, I had to write the PowerShell expressions to implement them. The same is true of –WhatIf. You’ll see in the cmdletbinding attribute that the function supports ShouldProcess. However, the .NET classes I’m calling have no concept of WhatIf. I had to handle that myself

Here’s the function in action:

This also makes it easy to create multiple files.

Or how about this? I want to create a variety of files with different names.

Now I have some test files I can work with

Normally I prefer to use cmdlets wherever possible. But since there is no cmdlet from Microsoft to create dummy files, I have no problem resorting to the .NET Framework. However, I think it is still worth the time to wrap the .NET commands in a user-friendly and easy to use advanced function as I’ve done today.

If you find a way to use this function, I hope you’ll let me know.

Friday Fun: Search Me

magnifying-glass-text-label-search I’ve been working on a few revisions and additions to my ISE Scripting Geek module. Even though what I’m about to show you will eventually be available in a future release of that module, I thought I’d share it with you today. One of the things I wanted to be able to do was search Google (or Bing) from within the PowerShell ISE.

If you research search techniques or APIs for sites like Google and Bing, you’ll quickly get swallowed up in the miasma of JSON, SOAP, XML and mind-numbing examples. Although you might enjoy this sort of thing, I was hoping to find something much, much simpler. After all, I’m not going to be creating complicated search queries. I’m going to want to search for a simple term or phrase like “powershell remoting” or “Invoke-Webrequest”.

For Google, it is this simple to create a URL:

To use Bing, it seems it helps to include your culture which you can retrieve with Get-Culture.

In either case I now have an appropriate URL ready to go into my default browser. I can’t guarantee this will work for everyone, but it is as simple as this command:

I’m actually using the alias for Start-Process but in this case I don’t mind. Windows will switch to my browser and open the URL in a new tab. To make all of this a bit more flexible I put together a simple function.

The function needs a string of text to search for and a search engine. I’ve set a default to Google. You’ll also see that I’ve included parameter validation so that you can only use a value in a pre-defined set. To make the function a bit more interesting I also included Yahoo. I’ve used a Switch statement to determine how to construct the url. You could use this function from either the console or the PowerShell ISE. I suppose in terms of PowerShell this is a “low-tech” approach but it works and meets my needs.

To plug this into the ISE, you need to dot source the function in the ISE. Then if you want, you can add it as a menu item.

This will add a menu shortcut to the Add-Ons menu called “Search Internet”. I’ve also included an optional keyboard shortcut ALT+S, but you can define anything that isn’t already being used. When the menu item is invoked, it will run my Get-SearchResult function and use the currently selected text for the -Text parameter. The net result is a Google search for whatever I have selected in a script file. If you prefer Bing, I’d simply modify the function and set it as the default.

This isn’t rocket science but hopefully makes your PowerShell experience a little more efficient. You are welcome to try this now, or wait for the next version of the ISE Scripting Geek.

Have a great weekend.

Advice, solutions, tips and more for the lonely Windows administrator with too much to do and not enough time.

%d bloggers like this: