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 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  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.

The PowerShell Night Shift

A few days ago I got a question on Twitter on how to push a long running command to the background but would also survive a user logoff. This is a pretty standard practice in the Linux world. In PowerShell we’ve had similar operations using the Start-Job cmdlet. With Start-Job you can run a scriptblock or script in a background PowerShell process and get the results later. However, if you close your PowerShell session, you lose the job so that’s not an option here.

The best solution in this case is to take advantage of disconnected sessions. This feature was added in PowerShell 3.0 where Microsoft moved the maintaining the remoting session from the client to the server. This meant that if you had a long running command you wanted to run on a remote server, you could kick it off with Invoke-Command.

The session gets created remotely and the command kicks off. On the client you can logoff, shut down, go home, or even go to another computer. You can reconnect the session as long as you use the same credentials. One thing you can’t do though is be in the middle of an interactive session and decide to disconnect.

It turns out this same idea works locally as well. Here’s how I can kick it off

Kicking off a disconnected commandKicking off a disconnected command (Image Credit: Jeff Hicks)

Remember this is on my machine. Essentially I’ve created a remote session to myself. I can now close the PowerShell session. I can even log off and go home. BUT, I can’t shut down the computer. If I do that, then I will lose the remoting session. For the sake of my demo, I’ll log out, re-login and open a new PowerShell session.

Once back in I need to get the disconnected session. You might think to simply run Get-PSSession.

Attempting to reconnect the sessionAttempting to reconnect the session (Image Credit: Jeff Hicks)

But this is only trying to show connected sessions. Because the disconnected session is technically remote, I need to specify the computername.

Viewing the disconnected sessionViewing the disconnected session (Image Credit: Jeff Hicks)

There it is. In order to use it I need to connect to it.

Re-connecting the sessionRe-connecting the session (Image Credit: Jeff Hicks)

See the availability state of Busy? Even though I am connected to the session I can’t get anything out of it until it stops being busy.

Session must be opened and availableSession must be opened and available (Image Credit: Jeff Hicks)

During the course of my testing, and I don’t know if this is something related to running this in basically a loopback, but the session never seems to stop being busy, even though if it is simply running my command it should be finished. But if I enter the session interactive, that seems to be enough to give it a bump and get things moving.

Re-entering the disconnected sessionRe-entering the disconnected session (Image Credit: Jeff Hicks)

But I’m good with this. I can kick off a long running PowerShell task, logout, and get the results the next time I log in. I can re-use the session for as long as it is open.
An open loopback remoting session (Image Credit: Jeff Hicks)

The session will exist until I delete it. Or I can disconnect it again.

disconnecting again
disconnecting again (Image Credit: Jeff Hicks)

If you find this little trick useful, I hope you’ll let me know. And if you want to learn more about PowerShell remoting, keep an eye on the Pluralsight course catalog as I am currently working on a PowerShell Remoting Fundamentals course.

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