<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: James Pogran</title>
    <description>The latest articles on DEV Community by James Pogran (@jpogran).</description>
    <link>https://dev.to/jpogran</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F242773%2F3ef02bcc-c38a-4aa2-a853-08bc2e6ac8f5.jpeg</url>
      <title>DEV Community: James Pogran</title>
      <link>https://dev.to/jpogran</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jpogran"/>
    <language>en</language>
    <item>
      <title>New Puppet Bolt PowerShell Cmdlets</title>
      <dc:creator>James Pogran</dc:creator>
      <pubDate>Wed, 02 Sep 2020 20:57:48 +0000</pubDate>
      <link>https://dev.to/jpogran/new-puppet-bolt-powershell-cmdlets-354f</link>
      <guid>https://dev.to/jpogran/new-puppet-bolt-powershell-cmdlets-354f</guid>
      <description>&lt;p&gt;&lt;a href="https://puppet.com/docs/bolt/latest/bolt.html"&gt;Puppet Bolt&lt;/a&gt; now ships (as of version 2.20.0) with PowerShell native &lt;a href="https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/cmdlet-overview"&gt;cmdlets&lt;/a&gt; that are auto-generated from Bolt source code with correct PowerShell &lt;a href="https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands."&gt;verb-noun&lt;/a&gt; semantics, familiar PowerShell parameter &lt;a href="https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines?view=powershell-7#parameter-design-guidelines-sd03"&gt;conventions&lt;/a&gt;, and identical Bolt &lt;a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comment_based_help"&gt;help documentation&lt;/a&gt;. All of these features work together to surface Bolt commands in a discoverable and understandable manner and enable using powerful PowerShell language features with Bolt.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to do
&lt;/h2&gt;

&lt;p&gt;How do you find out what tool to use, if you don't know what tools are available? One of the first things you learn when using PowerShell is how to get help using &lt;code&gt;Get-Command&lt;/code&gt; and &lt;code&gt;Get-Help&lt;/code&gt;. These are two very powerful cmdlets that enable intuitive discovery of the available tooling and how to use the chosen tool.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Get-Command&lt;/code&gt; is a versatile tool that can find any PowerShell command on the system using a variety of filters, and returns information on both the command and where the command comes from. Previously we only shipped the &lt;code&gt;bolt&lt;/code&gt; command with Bolt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS &amp;gt; Get-Command bolt

CommandType     Name  Version    Source
-----------     ----  -------    ------
Function        bolt  2.23.0     PuppetBolt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This doesn't do much for discoverability and requires you to run the &lt;code&gt;bolt&lt;/code&gt; command to find out what it can do. If we instead start to surface Bolt commands as PowerShell cmdlets, we can start to take advantage of both &lt;code&gt;Get-Command&lt;/code&gt; and PowerShell's cmdlet conventions to increase discoverability:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS &amp;gt; Get-Command *bolt*

CommandType     Name                          Version    Source
-----------     ----                          -------    ------
Function        bolt                          2.23.0     PuppetBolt
Function        Convert-BoltPlan              2.23.0     PuppetBolt
Function        Get-BoltGroup                 2.23.0     PuppetBolt
Function        Get-BoltInventory             2.23.0     PuppetBolt
Function        Get-BoltPlan                  2.23.0     PuppetBolt
Function        Get-BoltPuppetfileModules     2.23.0     PuppetBolt
Function        Get-BoltTask                  2.23.0     PuppetBolt
Function        Install-BoltPuppetfile        2.23.0     PuppetBolt
Function        Invoke-BoltApply              2.23.0     PuppetBolt
Function        Invoke-BoltCommand            2.23.0     PuppetBolt
Function        Invoke-BoltPlan               2.23.0     PuppetBolt
Function        Invoke-BoltScript             2.23.0     PuppetBolt
Function        Invoke-BoltTask               2.23.0     PuppetBolt
Function        New-BoltPlan                  2.23.0     PuppetBolt
Function        New-BoltProject               2.23.0     PuppetBolt
Function        New-BoltSecretKey             2.23.0     PuppetBolt
Function        Protect-BoltSecret            2.23.0     PuppetBolt
Function        Receive-BoltFile              2.23.0     PuppetBolt
Function        Register-BoltPuppetfileTypes  2.23.0     PuppetBolt
Function        Send-BoltFile                 2.23.0     PuppetBolt
Function        Unprotect-BoltSecret          2.23.0     PuppetBolt
Function        Update-BoltProject            2.23.0     PuppetBolt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we can see a large list of commands using PowerShell's verb-noun syntax. Even if you aren't familiar with PowerShell's verb-noun conventions, reading the list of commands gives you an immediate general idea of what's possible with Bolt. There's &lt;code&gt;plans&lt;/code&gt; and &lt;code&gt;scripts&lt;/code&gt;, &lt;code&gt;tasks&lt;/code&gt; and &lt;code&gt;projects&lt;/code&gt;, concepts like invoking commands and getting information from the system. Scanning the list gets you immediate information you can use to choose what tool to complete your task.&lt;/p&gt;

&lt;p&gt;If you're familiar with PowerShell's verb-noun conventions, you can start to drill down and quickly decide what command will do what you want. For example, you may want to find out how to get a list of Bolt tasks on the system. You know that PowerShell's verb-noun conventions dictate that all commands that return information use &lt;code&gt;Get&lt;/code&gt; as the verb, and that the noun should contain the word related to the information returned, &lt;code&gt;Task&lt;/code&gt;. Using this information, you run &lt;code&gt;Get-Command&lt;/code&gt; and tell it to search the &lt;code&gt;PuppetBolt&lt;/code&gt; module for all commands that use the verb &lt;code&gt;Get&lt;/code&gt; and have a noun with the word &lt;code&gt;task&lt;/code&gt; in it using a wildcard:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS &amp;gt; Get-Command -Module PuppetBolt -Verb Get -Noun *task*

CommandType     Name          Version    Source
-----------     ----          -------    ------
Function        Get-BoltTask  2.23.0     PuppetBolt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;Get-Command&lt;/code&gt; we found &lt;code&gt;Get-BoltTask&lt;/code&gt; quickly in one attempt, whereas we would have to call &lt;code&gt;bolt --help&lt;/code&gt;, then &lt;code&gt;bolt task --help&lt;/code&gt; to find the same information. We didn't really need to read documentation to know which command to run, we already knew from PowerShell's conventions that any &lt;code&gt;Get&lt;/code&gt; command would return a one or more items and that a command with a noun &lt;code&gt;BoltTask&lt;/code&gt; returns information about tasks.&lt;/p&gt;

&lt;p&gt;All of this is great for figuring out which command we need to run, but how do we know how to use it ? In the next section we'll explore using &lt;code&gt;Get-Help&lt;/code&gt; for this purpose, but before we do we can use &lt;code&gt;Get-Command&lt;/code&gt; one last time to find out some quick bits of syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS &amp;gt; Get-Command Get-BoltTask -Syntax

Get-BoltTask [[-Name] &amp;lt;string&amp;gt;] [-LogLevel &amp;lt;string&amp;gt;] [-Modulepath &amp;lt;string&amp;gt;] [-Project &amp;lt;string&amp;gt;] [-Configfile &amp;lt;string&amp;gt;] [-Filter &amp;lt;string&amp;gt;] [-Format &amp;lt;string&amp;gt;] [-Version] [&amp;lt;CommonParameters&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The output returned by &lt;code&gt;Get-Command&lt;/code&gt; calls back to the opening paragraphs of this post. We used Bolt source code to generate these PowerShell cmdlets, so each cmdlet is populated with the parameters and help text for each Bolt command. Since they are formatted using PowerShell conventions, commands like &lt;code&gt;Get-Command&lt;/code&gt; and &lt;code&gt;Get-Help&lt;/code&gt; can parse these new Bolt PowerShell cmdlets and return structured information. When we ran &lt;code&gt;Get-Command Get-BoltTask -Syntax&lt;/code&gt;, PowerShell parsed the Bolt cmdlets and returned structured parameter information for us. This is immensely powerful for command discovery.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Help
&lt;/h2&gt;

&lt;p&gt;We now know the command we want to run, but how do we find out how to run it? We can use &lt;code&gt;Get-Help&lt;/code&gt; for that. While we can get far with a few conventions using &lt;code&gt;Get-Command&lt;/code&gt;, the cmdlet &lt;code&gt;Get-Help&lt;/code&gt; really shines in helping us understand how to use them. Since &lt;code&gt;Get-BoltTask&lt;/code&gt; is a PowerShell cmdlet and contains PowerShell comment based help, we can use &lt;code&gt;Get-Help&lt;/code&gt; to read more detailed information about how to use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS &amp;gt; Get-Help Get-BoltTask

NAME
    Get-BoltTask

SYNOPSIS
    bolt task show [task] [options]


SYNTAX
    Get-BoltTask [[-Name] &amp;lt;String&amp;gt;] [-LogLevel &amp;lt;String&amp;gt;] [-Modulepath &amp;lt;String&amp;gt;] [-Project
    &amp;lt;String&amp;gt;] [-Configfile &amp;lt;String&amp;gt;] [-Filter &amp;lt;String&amp;gt;] [-Format &amp;lt;String&amp;gt;] [-Version]
    [&amp;lt;CommonParameters&amp;gt;]


DESCRIPTION
    Show available tasks and task documentation. Omitting the name of a task will display a
    list of tasks available in the Bolt project. Providing the name of a task will display
    detailed documentation for the task, including a list of available parameters.


RELATED LINKS
    https://puppet.com/products/bolt
    https://puppet.com/docs/bolt/latest/bolt_command_reference.html

REMARKS
    To see the examples, type: "get-help Get-BoltTask -examples".
    For more information, type: "get-help Get-BoltTask -detailed".
    For technical information, type: "get-help Get-BoltTask -full".
    For online help, type: "get-help Get-BoltTask -online"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We get formatted help immediatly with one command using &lt;code&gt;Get-Help&lt;/code&gt;. Looking through the output using &lt;code&gt;Get-Help Get-BoltTask -Detailed&lt;/code&gt;, you can see each Bolt PowerShell cmdlet contains all the Bolt help text for each command and parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PARAMETERS
    -Name &amp;lt;String&amp;gt;
        The task to show

    -LogLevel &amp;lt;String&amp;gt;
        Set the log level for the console. Available options are
        debug, info, notice, warn, error, fatal, any.

    -Modulepath &amp;lt;String&amp;gt;
        List of directories containing modules, separated by ';'
        Directories are case-sensitive

    -Project &amp;lt;String&amp;gt;
        Specify what project to load config from (default: autodiscovered from current working
        dir)

    -Configfile &amp;lt;String&amp;gt;
        Specify where to load config from (default: ~/.puppetlabs/bolt/bolt.yaml).
        Directory containing bolt.yaml will be used as the project directory.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This shows that the generated the PowerShell cmdlets have &lt;strong&gt;typed&lt;/strong&gt; parameters. Parameters that accept strings are cast to &lt;code&gt;[string]&lt;/code&gt;, parameters that accept a range of values now have &lt;code&gt;[ValidateSet()]&lt;/code&gt;, and so on. Most of Bolt's parameters translate naturally to PowerShell conventions. We spent a lot of time making sure these translation fit PowerShell semantics. A small list of considerations for parameters follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Required parameters are detected and set as &lt;code&gt;Mandatory&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Detects when Validation attributes can be applied at the PowerShell level&lt;/li&gt;
&lt;li&gt;Detects common parameter sets for commands&lt;/li&gt;
&lt;li&gt;Added new &lt;code&gt;name&lt;/code&gt; parameters to commands like &lt;code&gt;command&lt;/code&gt;, &lt;code&gt;task&lt;/code&gt;, or &lt;code&gt;plan&lt;/code&gt; and set as &lt;code&gt;Mandatory&lt;/code&gt; and &lt;code&gt;positional&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Boolean parameters (&lt;code&gt;--[no]-ssl-verify&lt;/code&gt;) are collapsed into one PowerShell &lt;code&gt;switch&lt;/code&gt; parameter &lt;code&gt;--SslVerify&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of this shows up in the &lt;code&gt;Get-Help&lt;/code&gt; output, using commands you're familiar with, without having to page through several pages of &lt;code&gt;bolt command run --help&lt;/code&gt; to find out what parameter will do what.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using what you have learned
&lt;/h2&gt;

&lt;p&gt;Putting all this effort into making these Bolt commands and parameters have PowerShell types and validation pays dividends not only with the PowerShell help system, but also enables some very powerful ways to invoke Bolt commands.&lt;/p&gt;

&lt;p&gt;We can see how by using what we've covered so far in a real world example. Let's say we want to get the status of the Print Spooler service on a remote target. Using &lt;code&gt;bolt --help&lt;/code&gt;, then &lt;code&gt;bolt task --help&lt;/code&gt;, then &lt;code&gt;bolt task show&lt;/code&gt;, then &lt;code&gt;bolt task show service&lt;/code&gt; we can see that we need to run &lt;code&gt;bolt task run -t localhost service name=spooler action=status&lt;/code&gt; in order to check the status of the Print Spooler.&lt;/p&gt;

&lt;p&gt;That was around 3 commands and alot of reading through pages of parameters to find out what to do. If the params we want to pass to the &lt;code&gt;service&lt;/code&gt; task become complicated, we can use JSON instead but that is yet another line to write.&lt;/p&gt;

&lt;p&gt;If we remember that the new Bolt PowerShell cmdlets follow PowerShell verb-noun conventions, we can use the same discovery process we saw earlier in this post to find the command to use. We know that &lt;code&gt;Invoke&lt;/code&gt; is the PowerShell verb convention for executing something and we know &lt;code&gt;BoltTask&lt;/code&gt; is the noun we need by applying PowerShell noun conventions. Without having to think about it much or run a series of commands we know to use &lt;code&gt;Invoke-BoltTask&lt;/code&gt; to execute tasks. To find out how to use &lt;code&gt;Invoke-BoltTask&lt;/code&gt; we run &lt;code&gt;Get-Help Invoke-BoltTask -detailed&lt;/code&gt; and see the list of parameters needed to run the command and come up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS &amp;gt; Invoke-BoltTask -Targets localhost -Name service -Params @{ name = 'spooler'; action = 'status' }
Started on localhost...
Finished on localhost:
  {
    "status": "running",
    "enabled": "true"
  }
Successful on 1 target: localhost
Ran on 1 target in 9.24 sec
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This shows the power of using PowerShell language constructs to make passing input to Bolt easier. The &lt;code&gt;-params&lt;/code&gt; parameter accepts a PowerShell hash and converts it under the covers to a JSON string to pass to Bolt. This means we can get as simple or as complicated as we want defining it and PowerShell will handle the complexity of sending it to Bolt.&lt;/p&gt;

&lt;p&gt;We can take it one step further and use PowerShell parameter splatting inside a PowerShell script, so that we can reuse the same data for multiple commands in our scripts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS &amp;gt; gc .\getserviceinfo.ps1
$spoolerParams = @{
  Name = 'service'
  Params = @{
    name = 'spooler'
    action = 'status'
  }
  Targets = 'localhost'
}
Invoke-BoltTask @spoolerParams
PS &amp;gt; .\getserviceinfo.ps1
Started on localhost...
Finished on localhost:
  {
    "status": "running",
    "enabled": "true"
  }
Successful on 1 target: localhost
Ran on 1 target in 9.68 sec
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;We think taking the time and effort to surface Bolt commands as native PowerShell cmdlets is proven effective by the ease of use demonstrated above. Using some simple scenarios we have shown how PowerShell cmdlets increase discoverability of our toolset and make it easier to understand how to use it. Then we covered how the PowerShell language also makes it easier to express complicated parameter sets to Bolt and enables powerful scripting scenarios.&lt;/p&gt;

</description>
      <category>puppet</category>
      <category>puppetbolt</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
