Alma
Evans

Scripting best practices every IT admin must follow

Alma Evans

Jun 10, 2021

9 min read

Scripting can sometimes be as much complex as in-depth programming. With the same chaos being hidden behind the several hundred lines of codes and the same buzzwords – ‘smart’, ‘quick’ and ‘bug-free’ defining the script’s worthiness, the same level of planning, the same level of mastery, and the same level of efforts are needed in scripting too. Without the right approach, even a bona fide coder can remain riddled with a seemingly simple script, and there are no golden rules to scripting. However, a few well-known scripting best practices can help you do the stacked-up dozen things more efficiently than usual.

The right way to get down to scripting

Scripts are a sequence of commands that help to make complicated daily chores a lot less trouble. Scripting is a powerful tool for anyone handling repetitive and boring processes, especially for IT teams looking to automate administration tasks. But as with any other tool, having a good understanding is important to create useful scripts that can streamline your work.

Scripting can be a piece of cake or a hard nut to crack; it all depends upon the way you get through it. If you have never built a script before or if you’re not yet comfortable with scripting, there are a few good things to take into consideration before you get started.

Selecting your scripting language

Best practices while selecting the scripting language
Best practices while selecting the scripting language
 

A scripting language is basically a programming language that is interpreted without being compiled. Scripting languages being built to integrate with other languages often work in conjunction with some other programming languages. The instructions written using scripting languages are made for a runtime environment.

Choosing a language to write your scripts is one of the first steps, but you need to recheck your objectives before picking one. There are no generalizations here as the scripting universe is a bit vast and is constantly expanding. What you need to choose solely depends on what you actually need to achieve with the script but a few factors to consider include:

  • The target OS: Consider the OS platform of the device to which the script is to be pushed. There are many scripting languages, but the primary ones supported by one OS will be different from the other. For instance, in Windows, Batch and PowerShell are the major scripting languages, but for Macs, AppleScript and JavaScript are the commonly used ones. So, you should choose a scripting language that help you build better scripts on the targeted platform.
  • Ease of use: Before deciding on a scripting language, be sure to check its flexibility to take in new functionality, ease to extend and fix bugs. Choose a scripting language you’re comfortable working with and is easy to incorporate new things.
  • Community support: There are many community forums, tutorials and articles out there to support coders. Select a scripting language only if it has enough resources available and has active forums through which you can seek help if needed.
  • Processing time: The time it takes to write a script depends on the difficulty of learning and understanding the scripting language. So, consider taking a scripting language that is easy to follow and is less time consuming.

Picking up an editor

You need a flexible platform that makes you more efficient in writing down your script. Back in the old days, normal text editors like Notepad were the only choice, but now there are many tools to assist you with this. Many of us still stick with the text editors we already know, but it is important to realize that there are many features a basic text editor can’t provide, easy customizations, plugin support, and auto-completion feature, to name a few. To get these additional features, a modern code editor tool is needed in the first place. The commonly used ones include Apple’s Xcode and Microsoft’s Visual Studio Code.

Writing down your script

Best practices while writing down the script
Best practices while writing down the script
 

There are hundreds to thousands of guidelines for you here depending on the scripting language you’ve chosen (hard to cover in a single blog). However, the general best practices other the usual advice to keep the code as simple as possible include:

  • Give proper variable naming: Strictly follow the naming conventions of the selected scripting language. Keep them short and use proper capitalizations and other characters to make the names understandable. The value they store should be reflected by the variable’s name itself.
  • Show consistency in code formatting: The codes within your script should be written in a clean format with proper spacing and alignment, which is important in making the script more readable. Whatever format you use, be sure to follow the same formatting throughout your scripts so that it would be easy for you as well as others to understand and distinguish between different sections of the script easily.
  • Leave explanations of the codes via comments: Code comments are really important as they give the reader an easy understanding of what each line of code is meant for. It can even help the writer of the script at a later time to quickly recollect the logic behind the script. It is good to start the script with a comment indicating what the script actually achieves and what parameters does it use.
  • Don’t embed data directly: Generally known by the term hard coding, embedding sensitive data directly into the code can cause serious security risks. Please avoid inserting usernames, passwords, any such credential or any sort of confidential data inside scripts. Instead of directly adding the data, you can make use of parameters and share the data only when needed, which will keep the readers away from knowing or accessing your sensitive information.

Testing your script

Needless to say, proper testing is an important step in making your script error-free. There are even applications to review your code and highlight the sections where the general scripting rules and syntax are violated. Performing actual testing would be a better option than using such error-checking apps. However, while testing your final scripts, be careful of:

  • Checking each line of your script.
  • Dividing the script into small sections and test out the functionality of a small part first rather than checking the entire script at once which sometimes results in spending hours only to find out that the script was wrong.
  • Using, as much as possible, a non-productive but similar environment for testing.
  • Testing manually before executing the script in your production environment.
  • Making sure to check the expected behavior by testing on devices for which you don’t have admin rights.

If you find errors with your scripts and are doing corrections multiple times, be sure to properly organize different versions of your scripts so that you get a proper revision history that will be of use at a later time. It is also a good practice to share your scripts in communities to get suggestions and corrections from scripting experts.

Executing your script

Best practices while executing your scripts
Best practices while executing your scripts
 

Many methods are out there, starting from the most tedious manual methods to the quick and hands-free methods, all to execute your script on the target devices. You can, of course, choose one as you wish from the list below but it is imperative to go for the most scalable and all-encompassing method that could handle any of your use cases.

  • Executing scripts on-demand using the terminal application
    This is the most generic method, which involves individually running the script by opening the terminal app on each of the target devices and executing the script using specific commands.
  • Scheduling script execution
    There are many options to set the script to run on a scheduled time or repeatedly run after particular intervals. There are even scheduling tools like Task scheduler for Windows and Crontab for macOS to automate script execution. However, scalability is still a matter of concern. For IT admins handling a large fleet of devices, they need to spare some time to access each device and schedule the script execution.
  • Executing scripts remotely using UEM
    Running scripts is a great automation option for enterprise IT, and the ability to execute them remotely would be even greater. This is what you can achieve with a UEM like Hexnode. All your custom scripts can be uploaded to the Hexnode management portal and push remotely to your target devices individually on in bulk as and when needed. Hexnode now allows pushing custom scripts to Windows and macOS devices.

Benefits of executing custom scripts via UEM:

  • Scalability: The scripts can be pushed to a large number of devices at once.
  • Remote execution: This may be the most important perk of executing scripts via UEM, especially for IT admins who need to handle employee devices at remote locations.
  • Orchestration of scripts: All your scripts can be neatly arranged and kept well organized to easily identify and trigger particular scripts on demand on a user device.
  • Execution based on scope: Scripts can be executed to device groups or user groups according to their scope.
  • Conditional execution: UEM allows to create criteria-based dynamic groups and execute scripts to a specific category of devices only when they meet or violate the set conditions.
  • Ease of tracking: The status of script execution shown in the UEM portal makes it easy for the IT admin to monitor the script execution status and take necessary actions as and when needed.


Nonetheless to mention, executing your custom scripts using a UEM solution is the scripting best practice just because of the benefits we’ve discussed so far.

I hope you now have some ideas on using your skills well in writing robust and reliable scripts. Following these scripting best practices is sure to minimize your downtime and will definitely help you provide better business values.
Share
  •  
  •  
  •  
  •  
  •  

Alma Evans

Product Evangelist @ Hexnode. Already lost up in the whole crazy world of tech... Looking to codify my thoughts for now...

Share your thoughts