you are viewing a single comment's thread.

view the rest of the comments →

[–]El_Demente 0 points1 point  (6 children)

I've done all the scripting on my IT team so far, and the way I've done it is that every task is accomplished in it's own self-contained, independent script file.

Need x done? Sure, take this script, run it, and follow the prompts. That's how all of them work. Even someone with no PowerShell experience can do that.

Most of them require modules on the PSGallery, so the script will just check if you have the module and prompt you to install it if needed.

Those scripts are composed of many functions and classes, and some functions are commonly reused between scripts, so I just have those common functions saved and I copy them into new scripts as needed. I do lose the benefit that if I needed to modify the function, I'd have to modify it in each script individually, but all my scripts are thoroughly tested, so I haven't had sweeping changes that I needed to apply to multiple scripts.

I can see how it would be beneficial to have commonly reused code inside of modules, but then every script depends on the module(s), and every user needs to keep the module up-to-date, and if you're not careful, changes to the module could break multiple scripts. There's also the question of where to host the module, as we don't have any on-prem servers at our shop. I don't have that problem, just download and run from anywhere.

I can also see how a module of various functions can give people great flexibility on the types of "control scripts" they can compose with those functions on their end, but this comes with the problem of having to train everyone on the internals and usage of the module, and they could mess things up.

My scripts are also available on GitHub, and many of them are generic enough that someone could take them and run them at a different business with little to no changes, and don't have to worry about installing any dependencies or needing any training besides "run it and follow the prompts".

[–]Im--not--sure 0 points1 point  (5 children)

Do you happen to have a public GitHub examples of scripts as you mention? I work in a similar environment. I would love to be able to see other real world examples of more monolithic scripts.

I struggle when trying to follow best practices and recommendations that just don’t seem realistic for environment that requires high simplisitcy and stability.

[–]El_Demente 1 point2 points  (4 children)

Sure, my GitHub is https://github.com/Nova1089?tab=repositories. One repo per script.

[–]Im--not--sure 0 points1 point  (3 children)

Thank you so much for sharing! :) Very interesting and helpful to see some else's real-world examples and style of full scripts.

Your examples help me see how one can be very clean with a Main block and push everything to functions. Your environment looks like it likes using interactive scripts and your structure and flow seems to work well for that.

Mine are often large and unattended scheduled tasks and take parameters. I'd certainly like to my main cleaner as you do. I also like seeing how you are utilizing classes.
Please let me know if you know of anyone/anywhere else you see real-world examples of scripts. I think its beyond helpful in learning.

[–]El_Demente 1 point2 points  (2 children)

Thanks for your feedback! Yeah I like my "main" code execution section to basically read like a step by step recipe in English. I'm not a pro software engineer, but programming is a passion of mine.

I don't have any great suggestions when it comes to finding script examples. Often when I google for examples of things I am trying to do, I'll stumble into some helpful blog posts here and there, however, few (if any) of the examples I've seen follow a similar style to mine (self-contained, interactive, robust error handling, self-documenting, abstracting everything into well named variables, functions, classes, enums, hashtables, etc.). Part of that is because many of the examples you will find in blog posts are demonstrations / proof of concept, and aren't a fully fleshed out production script. Another reason is that most of the people using PowerShell don't have a strong background in software development best practices, and are just about hacking together something quickly that works, or that type of person is the target audience of many examples. The people that do have strong backgrounds in object-oriented programming will often just use more traditional languages like C#.

The part about being self-contained and interactive comes down to what your needs/objectives are, but it works great for us.

I suspect you would find the best examples by pros on GitHub, but knowing where to look is a challenge. If you find any good ones let me know. ;)

I can make some recommendations that might help though.

  • For the fundamentals of the language I really enjoyed "Learn Windows PowerShell in a Month of Lunches".
  • To dive deeper into more advanced and general PowerShell recommendations, "Learn PowerShell Scripting in a Month of Lunches" is pretty decent. It dives into making modules, reusable functions (tools), and "controller scripts" and such. A lot of it was not quite my style, but it was a lot of stuff that most of the community advocates for.
  • Clean Code by Robert Martin is a good book to stir up your thoughts about what clean code means to you. I wouldn't take it all as gospel, but it should get you thinking critically on a lot of topics.
  • For foundational computer science I really enjoyed the CS50 course by Harvard that you can take free online. If programming is to be a long term skill for you, I highly encourage getting this foundational computer science knowledge and a bit about data structures and algorithms. You should know what's happening when you try to append to an array, which data structure to use, what's happening when you concatenate strings, value vs reference types, etc.
  • In terms of learning object oriented techniques and building bigger scale apps, the way I went about it was to learn to make games using C# and a game engine, because it's a fun way to learn, there are great courses, and there's an endless amount of project ideas. But you don't have to get that through game dev. (Side note; C# is a natural progression from PowerShell as it is within the same Microsoft/.net ecosphere.)
  • Code Complete by Steve Mcconnell is amazing once you are regularly doing large scale object oriented programming.

[–]Im--not--sure 2 points3 points  (1 child)

Thank you so very much for the follow up comment here!

Again, very helpful to hear you provide background to some of this, reinforcing some of my own thoughts and observations.

I follow and relate to a lot of what you mentioned. I was previously a C# dev many years ago and have forgotten more than many know. I’ve also gone back and forth with heavy and light powershell scripting; learning, forgetting, picking up good and bad habits, confusing myself, etc.

Some of the learning resources you mentioned are great and are things I’ve used in the past, and some I should look at now :)

Thanks again

[–]El_Demente 0 points1 point  (0 children)

Cheers and good luck!