If you're an MSP, chances are you use PowerShell (or any scripting tool) to automate a whole lot of tedious business tasks. While PowerShell is sufficiently versatile to solve a multitude of your MSPs problems, it's still a scripting platform that needs to be programmed at the end of the day. Now, the problem with writing long scripts is that it becomes less readable. This means when you have to replace a line of code (cmdlets) or debug an error, you have no choice but to break your head. Oh, also imagine what would happen if you have to re-visit the code a year later or if there's a new technician in town at your MSP!
Therefore, it helps to have uniformity in the way you write code. Here are 6 PowerShell scripting best practices that could help you write better code.
Write simple code
No, seriously! Keep it simple. Write code that flows uniformly from top to bottom. Use logic that is sequential and linear. If you make people jump shift up and down the script to follow your method, then simply becomes difficult to understand. If you wish to experiment with new techniques, do so to test the output, but always implement the less-fancy version in the final code. Modularize only when it's absolutely required and avoid creating new functions for tasks that can be done with a single line of code. Remember, every time you call functions, you are making it exponentially difficult to read your code. While it could be argued that modularizing code only makes it easier for you to understand the code, we've to consider the readability from a third-person perspective.
Comment wherever required
Technicians tend to use very problem-specific (or use-case-specific) functions at times to arrive at the solution quickly. Not just that, it is also difficult for you to understand your own code if you're coming back at it a year later. Well, including a single line of comment can make all the difference. Use consistent terminologies and include multi-line comments if you have to cover a few lines of the script. Comments in PowerShell are also automatically integrated with the Help function enabling the user to get script parameters with the Get-Help function.
Use proper variable names
Well, PowerShell is not a case-sensitive tool, yes. But that doesn't mean it's okay to define variables like $abc $x $y at will. Use consistent naming conventions, and be as obvious as possible when defining variables (Ex. $device-singular, $devices-plural). It's important to name variable names such that they reveal the context as soon as you read.
Process data within PowerShell as much as possible
Avoid creating unnecessary data exports and retrievals to run the code. In some cases, technicians might try to write (or export) data to a different file (like an excel .CSV) and then call a different function at a later stage to read data from the sheet for further processing. While it's an easy process to follow, it slows down the speed at which the final output is obtained. In doing so, the script doesn't make use of the super-fast computer memory (nanoseconds), instead, PowerShell uses a file read function that takes a few milliseconds to process.
A great alternative is to store data in the form of arrays or other data storage modules within PowerShell and use a call function to process data whenever required.
Technically, you could write your entire PowerShell code in a single line horizontally. While PowerShell as a tool is robust enough to process improperly formatted scripts it still doesn't help with readability if you don't follow certain rules:
- Include indentation wherever necessary (when you invoke loops, conditions, etc.)
- Use a new line to include brackets (open and close)
- Employ capitalization conventions (identifiers for types, members, parameters)
- Define and use a maximum line length (120 characters preferred)
- Single line space around parameters and operators
- Do not leave empty/white space after lines
Use hashtable wherever required
A hashtable is a data structure that uses hash functions to compute an index into an array. Technicians can use hashtables to map keys to values and search elements in an array list. If the line of code requires a lot of parameters to be listed after the cmdlets, it becomes confusing to scroll horizontally and read. In such cases, hashtables can be used to simplify the code (also known as PowerShell Splatting).
Stay up-to-date on all things SuperOps.ai