A quick guide to executing custom mac scripts via MDM
Want to learn the fundamentals of Mac scripting? Check out this quick guide!
Get fresh insights, pro tips, and thought starters–only the best of posts for you.
Feb 25, 2021
19 min read
We all confront quite a few arduous daily tasks on our devices we wish we could perform with just one click of a button. It’s far more difficult if you’re an IT admin in charge of an entire fleet of work devices. Whether it is refreshing all the systems, converting file formats, pushing some new system configurations, creating backups, or permitting your users to access new functionalities, there will be a plethora of tedious activities that are driving you crazy, most of which are to be done iteratively, like almost every day.
You may literally be looking into anything that could automate these recurring tasks and speed up your work process. And scripting may be one of those convenient options that you might still haven’t got around to trying.
Mastering scripting technologies is pretty easy that even a non-techy IT guy with no prior programming experience can have the basic technical know-how with little to no pain. And when you finally get to grips with Windows scripting, you have that extra option for remotely executing these custom Windows scripts via MDM, further automating the process. Today in this blog, we will be looking into the surprising benefits you can come up with by executing custom Windows scripts via MDM.
There is no systematic literature to explain how challenging it can be to tackle the administrative burden imposed on a company’s IT team. If you’re an IT admin who has ever spent time renaming a whole set of files on your employee devices, you can understand how exhausting it is to deal with such tasks manually over and over again. But, wouldn’t it be great if you could set up your systems to do all this grunt work by themself? This is exactly what you can achieve with scripts in Windows. All you have to do is write up scripts for the tasks you would like to automate and configure your devices to handle them for you. You can just sit back and enjoy the triumph of seeing your machine doing something productive.
Let’s start from scratch.
Windows scripts or scripts generally are small, interpreted programs that collect a series of commands into a file intended to push target devices to accomplish various operations. Scripts are usually written in a text editor and saved into a text file. A script file may contain thousands of individual statements (carried out in sequence), each of which details a specific operation and makes decisions based on the information they find, altogether executing the expected process without any manual intervention.
You just need to figure out the steps involved in performing the task and the conditions under which the task is to be performed; the task is ready to be turned into a script. As scripts are written in ASCII text, a code editor or text editor like Windows Notepad might be the only thing needed.
For an IT admin, the ability to do things quickly will make a huge difference for a task that takes a lot of clicks or one that takes a week to complete, and anything saving time is worth trying!
If you’re still not convinced and doubt whether scripting is well worth the effort, here are a good few comforts that scripts can bring to an IT admin’s life:
Now, it’s clear that scripts are indeed a boon for IT admins who are looking for a faster and more manageable way to address too time-constraining, too irritating and too error-prone device operations. Though writing scripts may require an additional time investment initially, the time scripts save you when run repeatedly would make up for that. That is, in the long run, scripts can make powerful workflows to enhance productivity, raise your quality and bring more value to the organization, effortlessly performing useful and impressive feats of automation.
Few IT admins do not have an understanding of even the basics of scripting as it was not considered a powerful tool a while back. But knowing the basics is really important that without a strong base, a seemingly simple script can startle even a long-tenured programmer.
It’s a long time since automation and scripting have been around the Windows platform. The first Windows-oriented automation tool introduced was the Command Prompt (Command line), which can be considered a successor of MS-DOS (Microsoft Disk Operating System) prompt and is often called as MS-DOS Command Prompt. It was designed to be compatible with the MS-DOS batch scripts for easy scripting. Since then, Command Prompt has been a pivotal component of the Windows ecosystem. Later, another tool called Windows Script Host (WSH) provided a language-independent environment to host more sophisticated command line based scripts via different object models and multiple scripting engines.
By the time Microsoft realized that the users desire a better command line experience, an object-oriented Command Shell called PowerShell was launched. PowerShell came with PowerShell ISE and worked both as a Command line interpreter program and a scripting language to unify Windows scripting and automation into a single console. As PowerShell provided a great extension of Command Prompt’s possibilities, it reinvented the way users interact with Windows. PowerShell is now a key component of any Windows management strategy, though Command Prompt is still in use to date. There is also a Windows feature called Windows Subsystem for Linux (WSL), which enable running Linux command-line tools on Windows. Windows Terminal is the latest Microsoft-approved terminal application to use all these Command line tools and shells within the same tool.
|Legacy command line interpreter of Windows||A task-based command line interpreter and a scripting language|
|Runs only Windows commands (i.e., can interpret only batch commands)||Runs both Windows commands and PowerShell cmdlets (i.e., can interpret both batch commands and PowerShell commands)|
|Much easier to learn and use||Much complex but has more features|
|The output is a stream of texts||The output is a collection of objects|
|Available only for Windows OS||Available on macOS and Linux, though primarily intended for Windows.|
|Command line commands are usually one word.
Command line command to change the directory.
to change the directory.
|PowerShell commands consist of two words usually structured in a verb-noun format.
PowerShell command to change directory
to change the directory
We’ve already taken the just-enough approach to know the basics of Windows scripting, and you’re now comfortable with what scripts are. It’s time to take the next step; let’s fathom out how the basic building blocks of Windows scripts together do the complex stuff.
Scripts are basically built of a sequence of statements, each of which states what action is to do further. The scripts might also contain some conditions under which the action is to be executed. Each of these statements will be interpreted by the host, and the necessary actions will be taken. Sometimes, larger scripts will be broken into components or modules to make them understandable.
The format, structure and flow of scripts may vary according to the language being used. So, instead of digging too deeper into the scripting concepts and terminology, capturing some first impressions of the common scripting languages would go a long way in an attempt to learning the art of scripting the boring stuff.
Scripting languages are computer languages that are interpreted rather than compiled, where instructions are processed while in motion. Scripting languages are uncomplicated (as they don’t require a steep learning curve) yet perfect (as they can fulfill any piece of work). Most scripting languages are so simple that the code can be edited and re-executed with ease.
Windows come with in-built support for multiple scripting languages though some are widely used than others. Learning even a single scripting language is worth the effort. Anyhow, here, we’ll be focusing only on two, namely the Batch and PowerShell that look like the top contenders. Surely, there are some others like VBscript along the way, but for the most part, Batch and PowerShell were the mainstays. Pardon me if you have an entirely different set of languages under consideration!
Batch is a very old scripting language whose birth dates back to the days of MS-DOS. It is not commonly used as a programming language but has great importance in the Windows environment. The series of commands are stored in a plain text file called a batch file and are executed by the Command Prompt that assumes both the role of an interpreter and a run time environment. Batch files are stored either using a .bat or .cmd extension.
|The first extension used by Microsoft for batch files||The new extension described by Windows NT based systems|
|Used by both DOS and Windows||Used by only Windows NT|
|Works in all environments||Won’t work in a 16-bit environment|
|Error level variable changes only if there is error||Error level variable changes when the command gets affected by successful command extensions|
|Can be read or interpreted by both command.com and cmd.exe||Can be read or interpreted only by cmd.exe|
There are two ways by which batch scripts can be executed, either by typing the batch scripts in the Command Prompt or by writing the script in a file and executing it through the Command Prompt. The latter is the recommended method where the script file is created in any script editor with a .txt extension, later renamed using a .bat or .cmd extension and the file is opened later to run the script.
No matter what all scripting languages are out there for Windows, there won’t be anything better than PowerShell which is the de facto choice for scripting in Windows. PowerShell is a high level full-fledged scripting language. It is extremely useful in cases where more programming logic is needed than Batch can provide.
PowerShell is the ideal language to code for the majority of Windows scripting tasks and is the Microsoft recommended way of scripting. PowerShell is based on object-oriented standards. PowerShell scripts are also written in a plain text file that uses a .ps1 extension. Using PowerShell ISE is the preferred way to use PowerShell language as it provides syntax highlighting and auto-filling of commands. To execute an already saved script file, right-click and click on Run with PowerShell.
We are simply amazed at the power that comes out of the synergy of various technologies underlying Windows scripting. Still, many frustrating difficulties surround their execution. Though working with scripts is pretty easy, running your scripts on an entire fleet of devices is not at all an easy task to tackle. You can ignore this fact if you don’t feel script execution a difficult chore; you put your head in the sand at your own risk.
Deploying scripts to a large fleet of devices can be a mammoth task for the IT admin. All the scripts need to be tested before distributing them to make sure that the systems never get damaged with the scripts. You also need to keep the scripts well organized with proper naming in a way that you won’t get confused.
All these concerns might make you a little lost in the world of scripting, but there are some ways to take the edge off these worries and make all the difference in Windows scripting.
Task Scheduler is an administrative tool in Windows that often doesn’t get much attention from IT admins. To make Windows scripts run automatically, the Task Scheduler allows scheduling scripts to run on a regular basis. Scripts can be executed on-demand, on schedule, on startup or when any conditions are met.
Task Scheduler promises full automation of script execution, but this is not a much viable solution for IT admins due to lack of scalability.
If the IT is heavily reliant on a large number of scripts that run often enough, it would be burdensome to execute the scripts manually all the time in all the devices. On top of that, the need for standardization and organization is rapidly apparent as IT admins have a lot of scripts to handle. Hexnode provides the option to get on with these, and no matter how many devices you are managing, Hexnode can handle all of them with ease. The exemplary management features and the centralized management console of Hexnode surely reduce the desperate number of tools that the IT has to rely on for task automation.
The Execute custom scripts action allows admins to push Windows scripts remotely to a bulk of devices, vastly simplifying their execution process. By executing custom Windows scripts via MDM, all your scripts can be kept organized, run either at the device level or the user level, and the execution status can be monitored right from the management portal. And it’s important to note that custom scripts are a great way to add to your endpoint management capabilities and flexibility as many features not included in the MDM protocol stack could be scripted and run on the machines to accomplish advanced configurations.
Though we initially considered script execution a hard knock, it’s now clear that it can definitely be done effortlessly with the right approach. And executing custom Windows scripts via MDM might be the right approach you need to take. When combined with an MDM, scripts can have a lot more possibilities.
We have now just scratched the surface of Windows scripting concepts to get a taste of it still hope that these fundamental measures will take you significantly further in the direction of scripting in Windows. And if you haven’t yet started working on your scripting skills, you’re already behind the curve, and it is high time to catch up with your peers. Take the first step now to unleash the power of Windows scripting and accompany Hexnode to put your scripting skills work right away!