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.

A Spooky PowerShell Halloween

I shared some code yesterday on Twitter and Facebook, but you may have missed it and I wanted to have a more permanent record so this is it. In the spirit of the holiday I thought it would spooky to have a little fun with the PowerShell ISE.

A spooky Halloween theme for the ISEA spooky Halloween theme for the ISE (Image Credit: Jeff Hicks)

How did I get this? Well first, I recommend you save your existing settings. Run this command in the PowerShell ISE.

This will save current values for settings you are about to change to a CSV file. Then run these commands to change ISE options.

Scary easy, right? These settings will remain even if you restart the PowerShell ISE. To restore your original settings and sanity you can import the saved CSV:

Or go to Tools – Options – Manage Themes and select the default, or whatever you are using.

Restoring the ISE themeRestoring the ISE theme (Image Credit: Jeff Hicks)

Happy Trick or Treating!

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