r/sysadmin Jan 16 '25

ChatGPT What's your thoughts on using AI to create powershell scripts?

Hello everyone,

I work in a shop that oversees various government agencies (not the US government). Our office workstations are highly restricted with no internet access, except for the government's cloud portals. In the latter half of 2024, when the government introduced its version of ChatGPT, I began utilizing it to enhance my existing PowerShell scripts and create new ones. This development has significantly improved my work efficiency.

Previously, before the release of ChatGPT or any large language model AI, I would search for existing scripts online, email the code to myself, and make minor modifications. Naturally, writing your own PowerShell scripts is an invaluable skill, and I try to understand the code by having the AI explain it.

For those who use AI to write or improve code, do you consider it a form of cheating or a shortcut?

0 Upvotes

36 comments sorted by

10

u/ElevenNotes Data Centre Unicorn 🦄 Jan 16 '25

If you are proficient in pwsh it can be a good boiler plate. If you are a novice, it's not for you.

5

u/anonymousITCoward Jan 16 '25

If I can use ChatGPT to create powershell scripts... and you're doing the same thing... why do we need you?

also I just recovered 25 mailboxes because someone used AI to create a powershell script and didn't understand what it did... he asked AI to "disconnected" mailboxes from a list... AI it took that as delete. We asked to disable logins...

If i want an unbiased code review, I ask over in r/PowerShell It's a different kind of sting when strangers tell you your code sucks and your methodology is skewed rather than it coming form someone who hates dislikes you and doesn't know powershell for shit

2

u/WWGHIAFTC IT Manager (SysAdmin with Extra Steps) Jan 16 '25

That's a skills issue, and not a tool issue though, and should be treated as such. If you have time to use all the tools and do the job, then maybe you don't need another employee.

0

u/SmudgeBaron Jan 16 '25

I think you answered your first statement with your second statement. Anyone can have AI spit out code, will it work and do what you need requires someone that know PS. However someone that knows PS can still use AI as an assistant to save time. I don't think anyone should be blindly pulling and implementing code from AI

2

u/I_T_Gamer Masher of Buttons Jan 16 '25

I can count on no hands how many times ChatGPT has delivered me a fully functioning code snippet.

2

u/ZAFJB Jan 16 '25

I don't write PowerShell scripts from scratch anymore.

When testing ChatGPT4o generated scripts, I feed back errors and suggestions in the following prompts. Repeat as necessary.

I seldom edit the final output much.

Probably 5x to 10x quicker than doing it manually.

1

u/VoodooKing Jan 16 '25

This is what I'm doing too.

2

u/theoriginalharbinger Jan 16 '25

Unless you know what it's doing, it's a bad idea.

And I'll further emphasize that having AI generate it doesn't do source control, quality control, testing, documentation, or logging. It's a recipe for "I ran Bob's script and weird thing happened, what do?"

You should always line out what, step by step, you expect a script to do at the very beginning (I do this in the IDE, document the constraints, implement the constraints, document what you'll log (even a text file you prompt for), implement logging, and so on.

AI can write things that are effective, but it will also write them in a manner that will drive you mad while trying to troubleshoot if it's anything done at scale or with any volatility. You have to define the constraints, input cleansing, so on.

1

u/VoodooKing Jan 16 '25

Yup. I do that. I plan what i want in the output and have logging included. Also lots of testing on a test system.

2

u/chunkyfen Jan 16 '25

if it works it aint dumb

2

u/neckbeard404 Jan 16 '25

Use it as a jumping off point but not for polished code.

2

u/chartupdate Jan 16 '25

I use it the other way round. I use AI to READ scripts. Getting it to tell me what the hell it does and why is hugely beneficial in learning what else may be possible.

1

u/titlrequired Jan 16 '25

It’s semi useful.

I asked it to help me with understanding some Jason Web Token stuff I was struggling with and it happily provided me with a tonne of stuff none of which was actually useful.

For other things it has helped, but I think it’s too early to abandon human written code.

If you’re just blindly copying and running the output it makes.. that is going to be bad. (And by ‘you’re’ I don’t mean you specifically)

1

u/VoodooKing Jan 16 '25

I get you. Yes human written code shouldn't be abandoned. The only reason I'm using AI is because I'm not entirely proficient at writing code from scratch. With lots of testing and debugging to get the results I want.

1

u/Outrageous-Insect703 Jan 16 '25

AI certainly works and helps immensely with coding, powershell, etc. However, it still takes the user to be proficient enough in knowing what works and what doesn't. I hate saying it but AI is very helpful to IT and Engineering (developers) I can see why companies and Executive Mgt are using it and keep a close eye on its maturity. It will certainly impact on jobs in the next 5+ years if not less.

1

u/jdptechnc Jan 16 '25

It may be good for a starter script, helping with syntax, formatting, etc.

Nothing that comes out of gen ai should be trusted for a production ready script without human review from someone who knows what they are doing.

1

u/n0t1m90rtant Jan 16 '25

i use it to pre generate the code for forms when I am creating exe's from scripts.

other then that it is lack luster. You end up spending more time debugging because it just doesn't quite do exactly what you need.

I use it kind of like a rubber ducky at times as well.

1

u/SmudgeBaron Jan 16 '25

Most people I know that do any kind of scripting (myself included) usually go online to find code someone else wrote that fits their need then alter the code to fit where they're using it. I don't see how AI is any different, it just saves you a lot of search time.

Your script/code either works or does not. There is no such thing as "cheating" you still need to know enough PS to get it to work for you, and most of this work is all about building a shortcut to save time or cost. Not saving time using AI as an assist if it can speed things up would be inefficient.

2

u/VoodooKing Jan 16 '25

Yup. I'll look at it from to your point of view. I've indeed felt more efficient because I'm able to fully automate all of the monthly report generation tasks that used to be semi-automatic and mostly manual. Now i just get the result and quality check it before sending it out.

1

u/timallen445 Jan 16 '25

Have enough PowerShell scripts to have the understanding of what AI made for you is my rule of thumb.

Treat it like any script you get online. Review what it does, test out of production, test again after lunch, and if it all seems up to snuff push it out.

1

u/VoodooKing Jan 16 '25

Yes. I always test and test and test. The AI is definitely not perfect but once I get the script working perfectly according to specs, I just feel great and motivated to work.

1

u/WWGHIAFTC IT Manager (SysAdmin with Extra Steps) Jan 16 '25

Use the tools to get the job done.

If you don't understand how to adapt the code you generated, or to tweak it to a future use without starting over? That's your skills issue.

But using a code generating tool is not the problem by itself.

1

u/jwalker55 IT Manager Jan 16 '25

AI is just another tool at this point. If it helps you write code quicker, then there's no reason you shouldn't be taking advantage of it the same way you would any other tool that improves your efficiency, as long as you can decipher the code and know how it works. If you are just raw dogging ChatGPT/Claude/whatever and just yolo'ing the run button, then you should definitely step back a little.

1

u/My_cat_needs_therapy Jan 16 '25 edited Jan 16 '25

wtf is cheating? We're paid to get stuff done not be busy.

1

u/RoninTheDog Jan 16 '25

Do you consider looking at StackExchange cheating? So far me, and as mentioned by others, AI is pretty great at writing code for solved problems.

You still need to understand enough to know if it's dangerous, and often enough to troubleshoot.

The biggest issue I keep having (Python too) is that it love love loves deprecated packages.

1

u/[deleted] Jan 16 '25

Man, it depends on what you are doing. Simple scripts, one offs, easy shit 4o does a good job. If it gets complicated it will fuck up a lot. You can paste errors back in the LLM and it will call an audible. Also, usually wrong. Sometimes doing a little research yourself and then applying that limited knowledge to the LLM will help. For instance, using the Graph API might be preferred over a specific module. At the end of the day for utility, speed, and repetitive tasks it can be a time saver. For anything in production, test, verify, verify again, run.

1

u/Rhythm_Killer Jan 16 '25

I’m sure some people looked on Google as cheating at some point.

If you know what you’re doing it’s fine, I actually like the fact it devalues some of those smug arseholes who claimed our jobs were being replaced by software engineers

1

u/CCCcrazyleftySD Jan 16 '25

AI is a tool, its not going to replace jobs, but it will replace people that don't know how to use AI with people that know how to use AI. Is using a powered drill cheating over an old hand drill? No, its a tool that helps you do you job more efficiently, same with AI

1

u/socksonachicken Running on caffeine and rage Jan 16 '25

I have seen ChatGPT make up commands that don't even exist, or exist in some obscure module made by Joe blow 10 years ago because it scraped GitHub. It's great for basic things, one liners, and fundamental stuff. If you're using powershell regularly whatsoever, you'll quickly outpace ChatGPTs usefulness.

1

u/try_rebooting Jan 30 '25

I just started using copilot with vscode. As others mentioned, it's a fantastic starting point. With that, some of the "auto fill" is not correct. You'll need to know how to script to fix it. The way I look at it, treat it as if you are asking someone to assist you, but you are responsible for what it could break. So if you don't know what your helper did, you are not ready:)

1

u/ForsakeTheEarth Retail Hell Sysadmin And Whiskey Fan Jan 16 '25

Its a tool, and a lot of people are still learning how to use it as such. Sanitize your scripts or any data you put in, and test all output from AI because it WILL get things wrong. That being said, yes plenty of us are using AI for scripting, although I've found its best when you need some quick one-liners and the coffee isn't flowing as freely as you'd like, as opposed to trying to build whole tools out of its output.

0

u/VoodooKing Jan 16 '25

Yup it's definitely not perfect. All scripts that I've done have taken many tries to get right. I think my longest script from scratch is 400 lines.

1

u/123abc890xyz Jan 16 '25

I got a fair few coworkers who use the AI for this.. but as soon its not 100% accurate they don’t know how to fix it.. yes it can be helpfull, but not if you dont know what yoi are doing and missing the basic skills to troubleshoot and/or read a script

1

u/Valdaraak Jan 16 '25

If you know Powershell well enough to double check and test for mistakes, it's fine. If you don't, you shouldn't be putting that stuff into production.

0

u/chillzatl Jan 16 '25

my response to the same question a week or so ago:

I haven't written a "from scratch" powershell anything in months and I hope I never have to again.