r/PowerShell • • Jul 29 '21

What was I thinking?

Have you ever written a sweet piece of code that you were proud of? Have you ever looked at that code 5 years later and wondered what idiot wrote that shit code?

179 Upvotes

78 comments sorted by

128

u/the_alabama_hammer Jul 29 '21

I think you misspelled 5 days.

4

u/jr49 Jul 29 '21

Yep. I started learning PS last year. Sometimes I open saved scripts and take one look and if I see a += or no [pscustomobject] I just cancel and rewrite it.

1

u/tsclapper Jul 29 '21

Oh, crap, I do that too. Come on why are you rebuilding the array EVERY time?

29

u/codylilley Jul 29 '21

This person scripts

4

u/[deleted] Jul 29 '21

I was about to type this response, but you beat me to it. So I guess you get an updoot.

-40

u/tsclapper Jul 29 '21

Sorry, no.

20

u/Weabout Jul 29 '21

I think he meant 1 day.

9

u/Turbo-NZ Jul 29 '21

More I just went to get coffee who wrote this mess..

12

u/LegitimateCrepe Jul 29 '21 edited Jul 27 '23

/u/Spez has sold all that is good in reddit. -- mass edited with redact.dev

6

u/sixfingerdiscount Jul 29 '21

Pretty sure he meant 2 tabs.

1

u/notavalidsource Jul 29 '21

what have we learned <3?

16

u/coldflame563 Jul 29 '21

Im fairly confident an abhorrent piece of power shell code that I wrote is in heavy heavy use by my former company, written almost two years ago. Dear lord if I only could do it now. It’d be so much better it’s absurd

11

u/tsclapper Jul 29 '21

The more you know, the dumber you feel.

8

u/coldflame563 Jul 29 '21

So I’ve moved on to management and I didn’t realize how far I’ve fallen. I looked at a devs code today and was just so lost. Sigh.

6

u/tsclapper Jul 29 '21

Yeah, the down fall of success

4

u/infinite012 Jul 29 '21

Those feels

3

u/Hungry-Display-5216 Jul 29 '21

I relate to this in so many aspects of life.

6

u/Aertheron01 Jul 29 '21

I have code in production at a previous company, it's so horrendous, it's from the time I started my first steps in Powershell.

I've had ex colleagues ask me for help fixing it when it broke, and I gave them some pointers and wished them much luck.

My biggest advice was: trow it out and replace it with something you wrote yourself so you learn Powershell and know how to maintain it :)

4

u/[deleted] Jul 29 '21

I know for a fact many scheduled scripts I've written 5+ years ago are still in use today.

4

u/letmegogooglethat Jul 29 '21

At my last job they were using scripts and such from a person that hadn't been there in like 5 years. They didn't have the expertise or time to figure it out, so they just kept using everything until something stopped working, then abandoned it and did it all manually. It was such a shit show. I don't know how they survived.

29

u/[deleted] Jul 29 '21

I like going back to my old scripts and finding the chunks that I obviously copied and pasted from Stackoverflow without knowing how it worked in the slightest.

13

u/tsclapper Jul 29 '21

That’s how you learn.

10

u/Zatchm0 Jul 29 '21

Haha. Heat of the moment man, just know that you grew from writing it.

9

u/tsclapper Jul 29 '21

Yeah, I actually love seeing my old code, just to see how stupid I was.

7

u/fuzzylumpkinsbc Jul 29 '21

I'll let you know in 4 years! but yeah I am reminiscent of the time I wrote in notepad++ a line for every distribution groups I wanted to get

3

u/tsclapper Jul 29 '21

Oh, Exchange, how I love it!

0

u/[deleted] Jul 29 '21

[deleted]

2

u/fuzzylumpkinsbc Jul 29 '21

I don't know about that, once you're past 3 groups it's just easier to create an array and write the for

61

u/cheffromspace Jul 29 '21

“Omg, who wrote this? What a dumbass…”

CreatedBy: cheffromspace

“Fuck…”

18

u/ApricotPenguin Jul 29 '21

That's why you don't put in the author's name.

Saves you from getting an email from /u/cheffromspace several years later, asking you to support/maintain/explain your code :D

3

u/gonzojester Jul 29 '21

This!

I've told many a newcomer this and they never listened.

Ego alone will drive a man insane because the helpdesk will get their hands on that script and expect you to support all changes to it.

hahahahahahahaha!!

3

u/tsclapper Jul 29 '21

Support it forever!

21

u/cheffromspace Jul 29 '21

I just started signing my name as contact_cheffromspace@$250/hr

4

u/Ytrog Jul 29 '21

You know version control like git is a thing right? 👀

6

u/ApricotPenguin Jul 29 '21

The original file may be under VC, but when the script is copied to whatever server it needs to be running on, you won't see the git history there

4

u/ExceptionEX Jul 29 '21

I tend to put the git repo location in the header, so years later I can find where an updated version is or should go.

And often the sting of looking at something I would bitch at someone for being written by me helps with being sympathetic to my juniors.

5

u/ApricotPenguin Jul 29 '21

That's.... a good idea on adding the git repo location to the script. I might just steal that idea. Thanks!

1

u/Ytrog Jul 29 '21

Fair point

3

u/[deleted] Jul 29 '21

If I even do remember what I wrote, I'm happy to explain briefly for a frustrated future admin. If the company wants my time, my rate is $80/hr.

4

u/jerrymac12 Jul 29 '21

Im afraid to go back and see what kinds of things i wrote...i keep saying ill be proactive and update my old code....i tell myself that at least.

4

u/tsclapper Jul 29 '21

Preach sister\bother!

2

u/stone500 Jul 29 '21

I've been working one a large scale 2008 R2 Server migration project for the last year and a half. For my scope, I'm in charge of migrating all the servers that host file shares, DHCP, and printers for our retail locations.

I've only started learning powershell about two years ago now, so I consider myself a novice. I have a little over a dozen separate scripts that all help automate these migrations. Since I wrote this for this specific scenario and nothing else, they are an absolute mess.

I don't do hardly any error handling. None of them are built into functions (I don't quite know how to do those yet). I literally just learned two weeks ago how to remote using CredSSP so I can solve the second-hop issue. I have scripts in my folders that I don't even use anymore and I haven't made time to clean them up. I'm using VSCode and I don't even know how to use the debugger. I add rows to my arrays like an imbecile using += statements.

I have a loooong way to go before I feel comfortable calling myself "proficient" in Powershell.

1

u/tsclapper Jul 30 '21

We’ve all been there.

2

u/Fallingdamage Jul 29 '21 edited Jul 29 '21

Sometimes its messy, but it still works. In my case I usually come back to an old script and now have a better way to do the same thing. The end result is the same, the script itself might be 1kb smaller when im done with it.

There was an instance a few weeks ago actually where I went to reuse an old script for something different. I made a copy and started hacking at it and got more and more frustrated. Like "How the hell is this even working??"
You know those times where a script starts out well and ends up just being a PS1 full of partial scripts and 1-5 liners that you used to layout pieces of the actual script that you're working on? - Yeah, basically I opened my 'scratch paper' and then spent 20 minutes asking myself why I would ever write something so incoherent.

1

u/tsclapper Jul 30 '21

I remember when I learned how to use a hash table. I increased my code efficiency so much it makes my head spin. Not sure how that’s relevant to your comment, but that’s what popped into my head.

9

u/hbkrules69 Jul 29 '21

I like looking at 10 lines of vbscript that can now be done in a single PowerShell command

9

u/jpochedl Jul 29 '21

Yep, 10 lines of VBScript to one PS command.... and 40 lines of VBScript are replaced by three or four PS commands piped on a single line....

7

u/[deleted] Jul 29 '21

That applies to so many things, try advent of code with PS and you often end up with crazy short solutions compared to other languages.

9

u/landob Jul 29 '21

I haven't really looked at it and wonder why the code was shit, but I have looked at it and go...you know....there was a way easier way of getting that done....

5

u/[deleted] Jul 29 '21

See this as a sign of progress!

4

u/Volore Jul 29 '21

In the beginning I was just so amazed by the idea of automating anything.. I used a lot of x,y coordinate mouse clicks...... those scripts are painful to look at.

4

u/BooBeesRYummy Jul 29 '21

Yeap, but mine was written in perl

3

u/EIGRP_OH Jul 29 '21

I see it as a good thing. It means I'm progressing

3

u/dupo24 Jul 29 '21

The next day I feel like this....

3

u/arcadesdude Jul 29 '21

It's like reading your journals...who wrote this crap lol

4

u/akaBrotherNature Jul 29 '21

It works...but I don't know how...or why 🙃

2

u/Solkre Jul 29 '21

It's the tech version of the artist looking back 5 years at their work.

2

u/Soopercow Jul 29 '21

Complete opposite for me, I code less than I used to and scripts I wrote a while ago make me feel like Captain America looking at the helicarrier computer.

2

u/uptimefordays Jul 29 '21

"I've automated another routine task!" me.

"All your code is pretty much the same, isn't that interesting?" also me.

For real though this is totally normal and a good sign you're improving.

2

u/packetheavy Jul 29 '21

When you go to copy paste a function you wrote a month or two ago for a new script and end up rewriting the whole function to make it shorter or more efficient.

Also my own lack of comments: so much ‘what is this?’ or ‘how the heck does that even work?’ when I read back through my stuff.

2

u/get-postanote Jul 29 '21

Heck, one can look at the code they wrote last week... Same reaction. Each earlier year, is well, u know.. ;-}

I still have my old Cobol/Fortran code, and all I can say was, wow! WTH ;-}

2

u/pydredd Jul 29 '21

You have to wait 5 years? Some of us get that reaction in 5 days.

2

u/TheGooOnTheFloor Jul 29 '21

I found out that some code I wrote 22 years ago is still in production. Since then I've changed companies 3 times and learned a couple of new programming languages (and smarter ways to do things). I cringe to think of what that code looks like compared to what I've done since then... (Under the terms of the employment then I couldn't take a copy of the code when I left, but I know I did things the hard way.)

But on the flip side, did you ever look at something you wrote 4 or 5 years ago and think "Wow, how did I come up with that solution?"

2

u/BuriedStPatrick Jul 29 '21

You know, I've never had that exact reaction to my old code. It's not like I don't have bad code out there and routinely run into it. It's just that I then remember what my options were back then in terms of technology and knowledge, and I usually just think to myself: "oh shit, I can improve this bit now that always bothered me!".

Now, if this trait could carry over to my personal life I'd be all set!

2

u/bofh Jul 29 '21

I’d be worried if I (or almost anyone else) didn’t feel that way. You should be learning new things in 5 years…

2

u/williamt31 Jul 29 '21

5 years? Hell I'm doing that now from stuff I wrote 3-6 months ago lol.

But then I only recently had the opportunity to use, learn and practice.

1

u/schuchwun Jul 29 '21

I wrote some python script to generate a BB engineering code to unlock hidden features. How the fuck did Britney Spears lyrics work?

1

u/gerajr51 Aug 14 '21

Everyday

9

u/[deleted] Jul 29 '21

[removed] — view removed comment

7

u/Aertheron01 Jul 29 '21

Yes, I know what you mean.

But it doesn't have to be uglier. I've learned 3 things, so others understand better.

  1. Make functions

  2. Never use aliases

  3. Add comments and comment-based-help

Especially functions help to keep code readable. You often just have to explain what it does, and give it a descriptive name.

0

u/[deleted] Jul 29 '21

[deleted]

1

u/Aertheron01 Jul 30 '21

That's what comments are for :) To explain weird behavior to the uninitiated.

1

u/Lee_Dailey [grin] Jul 31 '21

howdy spikeyfreak,

something like the below, perhaps?

$Test = '123456789'
$ScrambledTest = -join ($Test.ToCharArray() | Get-Random -Count $Test.Length)

$Test
$ScrambledTest

output ...

123456789
294176835

take care,
lee

2

u/[deleted] Jul 29 '21

[removed] — view removed comment

1

u/Aertheron01 Jul 30 '21

True, but if it's a bit obscure, it also helps yourself with a reminder.

1

u/tsclapper Jul 29 '21

Oh heck no. I love writing a sweet piece of code and using it as a teaching aide. I work hard to make sure I don’t use aliases, they just make it unreadable to new coders.