Tag Archives: PowerShell

Prompting for the Holidays

This should wait for a Friday Fun post but since it is December 1st I decided not to wait. It is that time of year again and my PowerShell prompt is colorful and sparkly.

My holiday themed PowerShell prompt (Image Credit: Jeff Hicks)

In my profile I have this code to use a new Prompt function.

I’ve posted the custom prompt before but now that I’m using GitHub a bit more, I’ve posted it as a Gist which you can find at https://gist.github.com/jdhitsolutions/f85eb9474c04d40c79ec#file-pschristmasprompt-ps1.

You can’t really pass parameters to a Prompt function so if you want to customize it you need to edit the file. One things I’ve thought of is changing the text so that instead of “Christmas in…” it might say “Santa Comes”

Santa Prompt
Santa Prompt (Image Credit: Jeff Hicks)

Or maybe this:

Another holiday promptAnother holiday prompt (Image Credit: Jeff Hicks)

And it doesn’t take much effort to support other holidays.

A Hanukkah versionA Hanukkah version (Image Credit: Jeff Hicks)

You can find this version of the prompt function at https://gist.github.com/jdhitsolutions/c26c0a4fb2112ca6e74e#file-pshanukkahprompt-ps1.

Whatever your sentiment I hope you have a happy and healthy holiday season.

Friday Fun: Holiday Shopping with PowerShell

Once again, the holiday shopping season is upon us. But perhaps PowerShell can make it a little easier or at least a bit more fun. I’m sure many of you have shopped at NewEgg.com. Perhaps you plan to do so again this year for friends, family or even yourself. So why not let PowerShell make this a bit easier.

NewEgg is savvy enough to publish RSS feeds for a number of their sales categories. You can find a master list at http://www.newegg.com/RSS/Index.aspx.  Let’s take their Deal of the Day feed.

Using Invoke-RestMethod, it is very easy to retrieve items.

But it is still a bit of a jumble, so let’s get a bit more selective.

I also created a new property called Published which takes the original PubDate and treats it as a date which makes the data easier to sort or filter.

Here’s a sample of what I retrieved.

A NewEgg Deal of Day itemA NewEgg Deal of Day item (Image Credit: Jeff Hicks)

With this data, I can use Out-Gridview as an object-picker.

Links in Out-GridviewLinks in Out-Gridview (Image Credit: Jeff Hicks)

I can select multiple entries, click OK and each link should open up in my browser.

The online dealThe online deal (Image Credit: Jeff Hicks)

But let’s make things a bit more interesting. The Title property includes the price and description. I have no idea what these links look like in other parts of the world so you may have to adjust the following examples.

First, I’m going to define a regular expression pattern to use named captures to get the currency, in my case $, the price and the item description.

I’m going to re-download the data skipping any entry that doesn’t have what looks like a price in the title field.

My goal is to use Out-Gridview again with separate properties for the currency and price. I need the price to be numeric so that I can sort on it. I next get the currency symbol using the regex expression.

Then I can process the rest of the RSS data, using the regex object to parse out the price and description.

If you notice, I used the Currency value as a property name. Now when I use Out-Gridview I have a more flexible display.

Reformatted DealsReformatted Deals (Image Credit: Jeff Hicks)

If I’m shopping for a new laptop, I can select multiple entries, click OK and review them in my browser.

Viewing my choicesViewing my choices (Image Credit: Jeff Hicks)

I can repeat the process by changing the RSS feed, say to their Shell Shocker

If I repeat the previous steps, this will fail, which brings up something to keep in mind with regular expressions: know your data. You have to know what you are processing and that it follows a predictable pattern. At least if you want to keep your regular expression patterns relatively simple. The problem here is that there is only a single item. So my code to get the currency figure fails, because I don’t have an array. In this situation I could do this:

Although it might make more sense to come up with code that I can re-use.

But from here the code is the same.

Shell Shocker itemShell Shocker item (Image Credit: Jeff Hicks)

That might be something I want to look into, although sometimes the RSS feeds are bit behind the site. Sadly, in this case, the link works, but the product is something else. But you get the idea.

Enjoy your holiday weekend and be careful out there!

Historically Speaking

So I’ve recently had a need to begin using Slack. I started out using a web browser, but since there is a Windows client I decided to give it a go. This article isn’t about Slack as much as what I was curious about and how I decided to tackle the problem.

I had read a few comments about the performance of the Slack client, although they may have been in reference to using a web browser, but it actually doesn’t matter. Instead I thought, “How can I get some historical data?” You probably have similar needs for things like processes or disk space. Since I was interested in memory utilization I could have used performance counters, and perhaps I’ll write about that eventually, but for now I had a simple one line command that gave me the pieces of information I wanted to track.

Getting process dataGetting process data (Image Credit: Jeff Hicks)

What I wanted was a way to take that snapshot repeatedly throughout the day and record the results. I decided to use a PowerShell scheduled job.

I wanted a repeating task and in my case I decided to only have it repeat for an 8 hour time span. I used a hashtable of parameters to splat to New-JobTrigger.

I elected to kick off the job one minute from the time I created the trigger. I could have used a scriptfile for my command which I expanded to export the results to a CSV file with a little additional information.

All that remains is to create the scheduled job with Register-ScheduledJob.

Throughout the day, PowerShell added data to my CSV file.

The data csv fileThe data csv file (Image Credit: Jeff Hicks)

If you noticed, I suppressed type information. This was so I could easily import the CSV into Excel and do some charting.

Charting the data in ExcelCharting the data in Excel (Image Credit: Jeff Hicks)

I formatted a few columns to make a nicer graph. Although I certainly could have formatted values like the WS sum in MB in PowerShell to begin with.

My techniques here are admittedly a little ad-hoc and perhaps not completely ready for an enterprise wide data gathering system, certainly of the “poor man’s” flavor. But I hope you see how easy it is to combine PowerShell and scheduled tasks to get the job done.

You probably have questions, so feel free to ask in the comments.