Hey sysadmins! Do you want to automate more, but get bogged down in the time and complexity of bulletproof PowerShell scripting? After a period of skepticism and reading a lot of negative opinions on Copilot, I finally decided to try it out for myself. I discovered that you can’t simply allow it to go off on its own. However, the positives greatly outweigh the negatives. Even though my title seems “click-baity”, I genuinely believe that you can supercharge Powershell with Github Copilot. I will demonstrate my experience in this article.
Caveat and Counterbalance
The screenshots in this article come from a Visual Studio Code environment with the GitHub Copilot extension. Both of these are free. However, GitHub Copilot itself is not. The webpage for Copilot claims that it boost development by 55%. For folks that primarily write code for a living, I have no reason to doubt that claim. However, for the typical sysadmin who scripts sometimes, I predict a much higher improvement. You won’t spend as much time researching things, for one. For another, Copilot will make suggestions that spark insights. Even when it does neither of those things, it frequently suggests code more or less as you would have written it anyway. Give the trial a spin, and I doubt you’ll have difficulty getting your employer to provide the $20 for a monthly subscription.
Example: PowerShell Scripting with Github Copilot
I was working on a script that needed to know when Windows Update was scheduled to run so that I could avoid colliding with another automation routine. I did the normal things that I do when I script things like this: I researched what I needed to know and built up a general plan of how to get it.
Warning: The script as shown was early, raw, and required multiple adjustments to put into production. For one thing, it does not account for the difference in day numbering between System.DateTime and the Windows Update registry keys. It attempts to do illegal things like set read-only fields. Read this article for the Copilot parts. Do not use this code in production. It will fail.
Let’s start with a screenshot:
At this point, everything outside the screenshot was basic boilerplate. I set up the environment and opened the process{} block. I had not written any comments or other variables. I knew that I wanted to offset from the Windows Update schedule, and you know that because I told you in the opening paragraph, but I had not suggested anything to Copilot. It guessed where I was headed, and it happened to guess correctly.
What it could not know, and what I did not tell you, is that I planned to have the other automation tool run at precisely the same time as the Windows Update schedule. That process takes a couple of seconds and immediately reboots. Windows Update has a random offset from its scheduled time and will recover from a missed timeslot. This allowed me to take advantage of a single, pre-ordained downtime window to handle both reboots. Note: these systems all have Windows Update set to run in whatever week the WSUS server approves their patches, so they have weekly downtime windows with a general expectation of monthly reboots. That’s why this plan works.
So, where Copilot suggested waiting one day (which would have fit the predictable criteria), I needed it to go out to the next week. I changed that. After some thought, I realized that if the automated process encountered a system that had a pending reboot, it would fail. So, I added in some other code to figure that out. I wanted it to use that to wait another week before running the automated process:
Even before I confirmed the variable that I was querying, Copilot figured out where I was headed. Again, it got the number of days wrong. But, it had the right idea.
A Note on My Github Copilot Experience
The above interaction occurred after I’d had a few weeks to get acquainted with Copilot. That wasn’t even close to the most impressive thing that Copilot did. It was just when I first decided to share. I generally don’t like plugging things that cost money (especially when I don’t get a cut), but I genuinely believe this tool will help a lot of sysadmins in their day-to-day duties.
Do Not Allow Github Copilot to Work Unsupervised
Sometimes, you will think that Copilot is clairvoyant. Other times, you will think that it’s just throwing out random characters to look busy. Learn quickly that you must always review everything that Copilot does. Even code that looks good at a glance might turn out nonsensical. Sometimes, it’s things that it could not have known, like the number of days that I wanted added above (although you’d think that “SkipWeek” was a clue, but AI is still progressing).
Copilot will not even try to stop you from writing bad code. It didn’t even pause when I tried to assign values to read-only fields.
Sometimes, it makes really obviously bad decisions:
Honestly, the worst are the ones that look right. Be on guard for those.
Don’t Neglect Copilot Chat!
I have had ample opportunities to use Copilot Chat, but I always forget. However, a colleague has used AI chat to great effect in scripting. Look at the position of the mouse in the following screenshot for where to access Github Copilot Chat in Visual Studio Code:
Most importantly: remember that the word Chat is not just a marketing or branding thing. I have watched a lot of seasoned admins use Copilot in Bing and then complain that it’s not better than regular search. However, they use it the way we have always used search: they type something, get results they don’t want, then refine the same query, and repeat until they get what they want or give up in frustration. Do not do that in any chat-based AI, including Github Copilot Chat. Use it as a chat. It “remembers” what you have already talked about. If it misses on the first try, don’t type the same thing differently. Tell it where it went wrong.
Assuming that I ever remember to do that myself (it would have saved me a lot of headaches on the displayed script), I will screenshot it and update this post.
Try Out Github Copilot
In the meantime, sign up for the trial and see if you get the same kind of results from Github Copilot that I did. I hope it boosts your scripting and automation tasks!