Automate IT and infrastructure, manage complex workflows, and mitigate risk at scale.
Try the full-featured Puppet Enterprise for free on 10 nodes.
Find and prevent compliance failures
Continuous Delivery for Puppet Enterprise
Build, test, and deploy infrastructure as code faster and easier
Compliance Enforcement Modules
Remediate to stay in compliance
Content & Modules
Pre-built scripts to automate common tasks
Get Puppet Enterprise
First 10 nodes are free!
Try it now
Request a demo
Find thousands of component modules built by the community and guidance on using them in your own infrastructure.
Visit Puppet Forge >>
Open Source Puppet
Perfect for individuals and small infrastructure
Automate tasks in orchestration workflows
See all open source projects >>
Contribute to open source projects >>
Using PowerShell for automation is simple and empowering with Bolt. If you’re not familiar with Bolt, it’s our agentless multi-platform open-source tool that allows you to automate tasks on an as-needed basis or as part of a greater orchestration workflow. You can use Bolt to patch and update systems, troubleshoot servers, deploy applications, or stop and restart services.
In this blog post we’ll share some of the benefits of using PowerShell and Bolt together, and then walk you through restarting a service with a PowerShell script and Bolt.
Table of Contents
PowerShell is a task automation tool and configuration management framework from Microsoft that is built on the .NET Common Language Runtime (CLR). IT teams can use PowerShell to create automation scripts for common or repetitive tasks and configure systems for repeatable testing and deployments.
If you are a PowerShell user working in a typical Windows domain environment, then much of what you can do with Bolt, you can do with PowerShell. But if you need to perform tasks in a mixed Windows and Linux environment, or if you need to orchestrate various parts of a complex workflow, Bolt can be a powerful addition to your regular scripting.
With Bolt, you have the ability to mix PowerShell tasks with tasks in other languages on Linux, all in the same workflow — one that is version controlled and easily distributable. Even if you don't have a mixed environment, Bolt allows you to easily share small reusable tasks written in PowerShell, instead of your team members having to re-implement common functionality in their scripts.
Additionally, Bolt plans — which go beyond the scope of the example in this blog post — allow you to add Puppet module code via Apply manifest blocks, giving you the ability to mix imperative tasks with declarative state configuration, using Puppet-supported modules or PowerShell DSC via the dsc_lite module.
Related: Explore the PowerShell Gallery, which makes PowerShell DSC easier to use.
To see Bolt in action, we’ll walk you through a simple example that involves running a script with Bolt, converting the script to a Bolt task, and then executing the task. The example script we’ll use, called restart_service.ps1, performs a common task of restarting a service on demand.
First, we’ll use Bolt to run the script as-is on a single target node.
Create a Bolt project directory to work in, called bolt-guide. Copy the restart_service.ps1 script into bolt-guide. In the bolt-guide directory, run the restart_service.ps1 script with the following command:
bolt script run .\restart_service.ps1 service=W32Time --nodes winrm://uia27aaq2e10fa6 -u Administrator -p
Note: The -p option prompts you to enter a password.
To have Bolt securely prompt for a password, use the --password or -p flag without supplying any value. Bolt then prompts for the password, so that it does not appear in a process listing or on the console. Alternatively, you can use the prompt plugin to set configuration values via a prompt.
You now have an inventory file to store information about your nodes.
You can also configure a variety of options for Bolt in bolt.yaml file, including global and transport options.
To convert the restart_service.ps1 script to a task, giving you the ability to reuse and share it, you create a task metadata file. Task metadata files describe task parameters, validate input, and control how the task runner executes the task.
Note: This guide shows you how to convert the script to a task by manually creating the .ps1 file in a directory called tasks. Alternatively, you can use Puppet Development Kit (PDK), to create a new task by using the pdk new task command. If you’re going to be creating a lot of tasks, PDK is worth getting to know. For more information, see the PDK documentation.
"description": "Stop or restart a service or list of services on a node.",
"description": "The name of the service, or a list of service names to stop.",
"description": "Immediately restart the services after start.",
You now have two files in the gsg module’s tasks directory: restart_service.ps1 and restart_service.json -- the script is officially converted to a Bolt task. Now that it’s converted, you no longer need to specify the file extension when you call it from a Bolt command.
bolt task show gsg::restart_service
groups: - name: windows nodes: - - config: transport: winrm winrm: user: Administrator Password: config: transport: winrm winrm: user: Administrator Password:
Congratulations! You’ve successfully converted the restart_service.ps1 script to a Bolt task.
To execute your new task, run the following command:
bolt task run gsg::restart_service service=W32Time --nodes windows
Note: --nodes windows refers to the name of the group of target nodes that you specified in your inventory file.
That’s it! You now have an automated workflow for restarting a service on-demand on multiple target nodes. You can use this same workflow to run your other PowerShell scripts on an inventory of remote nodes.
See the Bolt command reference for more information about the commands you used in this guide.
As mentioned at the start of this blog post, if you want to take this automation framework further, you can combine multiple tasks into a Bolt plan. Plans give you the ability to create multi-server workflows involving multiple tasks and Puppet code.
Bolt is a great place to start your automation journey, but if you have a large organization spanning multiple teams, Puppet Enterprise gives you more control over and insight into the automation of your infrastructure and provides a flexible combined agentless and agent-based approach.
Ready to try out Puppet Enterprise? Get started with a free trial today!
Start My Trial
Associate Technical Writer, Puppet by Perforce