Jira (BOLT-1130) Bolt PowerShell wrapper should allow for the use of Convert-ToJson (or similar) when using --params

0 views
Skip to first unread message

Ethan Brown (JIRA)

unread,
Feb 19, 2019, 10:13:04 AM2/19/19
to puppe...@googlegroups.com
Ethan Brown created an issue
 
Puppet Task Runner / Task BOLT-1130
Bolt PowerShell wrapper should allow for the use of Convert-ToJson (or similar) when using --params
Issue Type: Task Task
Affects Versions: BOLT 1.11.0
Assignee: Unassigned
Components: Windows
Created: 2019/02/19 7:12 AM
Labels: windows
Priority: Normal Normal
Reporter: Ethan Brown

BOLT-159 introduced a change to simplify argument passing from PowerShell when dealing with nested quotes.

The change does improve the case mentioned, such that users can now use a simpler invocation like the following (rather than using """ themselves):

bolt command run 'echo "hi from $(hostname)"' --modulepath .  --nodes winrm://localhost -u Administrator -p Qu@lity! --no-ssl

However, there are still cases where using more complex argument passing does not work properly due to not all values being escaped correctly for Ruby.

In an ideal situation, a complex object should be serializable to a JSON string, which can then be passed directly like --params ($myobject | ConvertTo-Json). Note that we will likely need a new PowerShell helper to serialize the values as desired, because of the way ConvertTo-Json handles certain types. For instance, more complex values like RegEx, IO.FileInfo and DateTime do not serialize in a way that's friendly to Bolt, requiring that ToString() be called first.

PS C:\cygwin64\home\Administrator> [DateTime]::Now | ConvertTo-Json
{
    "value":  "\/Date(1550588884351)\/",
    "DateTime":  "Tuesday, February 19, 2019 3:08:04 PM"
}
 
PS C:\cygwin64\home\Administrator> [DateTime]::Now.ToString() | ConvertTo-Json
"2/19/2019 3:08:07 PM"
 
PS C:\cygwin64\home\Administrator> [IO.FileInfo]'c:\windows' | ConvertTo-Json
{
    "Name":  "windows",
    "Length":  null,
    "DirectoryName":  "c:\\",
    "Directory":  {
                      "Name":  "c:\\",
                      "FullName":  "c:\\",
                      "Parent":  null,
                      "Exists":  true,
                      "Root":  {
                                   "Name":  "c:\\",
                                   "FullName":  "c:\\",
                                   "Parent":  null,
                                   "Exists":  true,
                                   "Root":  "c:\\",
                                   "Extension":  "",
                                   "CreationTime":  "\/Date(1536991766317)\/",
                                   "CreationTimeUtc":  "\/Date(1536991766317)\/",
                                   "LastAccessTime":  "\/Date(1550181187575)\/",
                                   "LastAccessTimeUtc":  "\/Date(1550181187575)\/",
                                   "LastWriteTime":  "\/Date(1550181186731)\/",
                                   "LastWriteTimeUtc":  "\/Date(1550181186731)\/",
                                   "Attributes":  22
                               },
                      "Extension":  "",
                      "CreationTime":  "\/Date(1536991766317)\/",
                      "CreationTimeUtc":  "\/Date(1536991766317)\/",
                      "LastAccessTime":  "\/Date(1550181187575)\/",
                      "LastAccessTimeUtc":  "\/Date(1550181187575)\/",
                      "LastWriteTime":  "\/Date(1550181186731)\/",
                      "LastWriteTimeUtc":  "\/Date(1550181186731)\/",
                      "Attributes":  22
                  },
    "IsReadOnly":  false,
    "Exists":  false,
    "FullName":  "c:\\windows",
    "Extension":  "",
    "CreationTime":  "\/Date(1536991766473)\/",
    "CreationTimeUtc":  "\/Date(1536991766473)\/",
    "LastAccessTime":  "\/Date(1550170740568)\/",
    "LastAccessTimeUtc":  "\/Date(1550170740568)\/",
    "LastWriteTime":  "\/Date(1550170740568)\/",
    "LastWriteTimeUtc":  "\/Date(1550170740568)\/",
    "Attributes":  16,
    "Mode":  "d-----",
    "VersionInfo":  null,
    "BaseName":  "windows",
    "Target":  [
 
               ],
    "LinkType":  null
}
 
PS C:\cygwin64\home\Administrator> ([IO.FileInfo]'c:\windows').ToString() | ConvertTo-Json
"c:\\windows"
 
PS C:\cygwin64\home\Administrator> [RegEx]'.*' | ConvertTo-Json
{
    "Options":  0,
    "MatchTimeout":  {
                         "Ticks":  -10000,
                         "Days":  0,
                         "Hours":  0,
                         "Milliseconds":  -1,
                         "Minutes":  0,
                         "Seconds":  0,
                         "TotalDays":  -1.1574074074074074E-08,
                         "TotalHours":  -2.7777777777777776E-07,
                         "TotalMilliseconds":  -1,
                         "TotalMinutes":  -1.6666666666666667E-05,
                         "TotalSeconds":  -0.001
                     },
    "RightToLeft":  false
}
 
PS C:\cygwin64\home\Administrator> ([RegEx]'.*').ToString() | ConvertTo-Json
".*"

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.7.1#77002-sha1:e75ca93)
Atlassian logo

Ethan Brown (JIRA)

unread,
Feb 19, 2019, 10:41:03 AM2/19/19
to puppe...@googlegroups.com
Ethan Brown updated an issue
Change By: Ethan Brown
BOLT-159 introduced a change to simplify argument passing from PowerShell when dealing with nested quotes.

The change does improve the case mentioned, such that users can now use a simpler invocation like the following (rather than using {{"""}} themselves):

{code}
bolt command run 'echo "hi from $(hostname)"' --modulepath .  --nodes winrm://localhost -u Administrator -p Qu@lity! --no-ssl
{code}

However, there are still cases where using more complex argument passing does not work properly due to not all values being escaped correctly for Ruby.

In an ideal situation, a complex object should be serializable to a JSON string, which can then be passed directly like {{--params ($myobject | ConvertTo-Json)}}. Note that we will likely need a new PowerShell helper to serialize the values as desired, because of the way {{ConvertTo-Json}} handles certain types.  For instance, more complex values like {{RegEx}}, {{IO.FileInfo}} and {{DateTime}} do not serialize in a way that's friendly to Bolt, requiring that {{ToString()}} be called first.

{code}
{code}


A command that should work when this change is completed is

{code}
bolt command run 'Get-Counter "\Processor(_Total)\% Processor Time"' --modulepath .  --nodes winrm://localhost -u Administrator -p *redacted* --no-ssl
{code}

A variant of the following as a task which accepts JSON parameters should also be acceptable (something like the following)

{code}
$names = @(

'\memory\% committed bytes in use',
'\Processor(_Total)\% Processor Time',
'\Processor(_Total)\% User Time'
)
$counters = @{"names" = $names}

bolt task run foo::windows --modulepath .  --nodes winrm://localhost -u Administrator -p
Qu@lity! *redacted* --no-ssl --params ($counters | ConvertTo-Json -Compress)
{code}


Ethan Brown (JIRA)

unread,
Feb 19, 2019, 10:41:04 AM2/19/19
to puppe...@googlegroups.com
bolt task run foo::windows --modulepath .  --nodes winrm://localhost -u Administrator -p Qu@lity! --no-ssl --params ($counters | ConvertTo-Json -Compress)
{code}

Ethan Brown (JIRA)

unread,
Feb 19, 2019, 12:27:03 PM2/19/19
to puppe...@googlegroups.com
Ethan Brown updated an issue
Change By: Ethan Brown
Release Notes Summary: The PowerShell cmdlet ConvertTo-Json typically does not produce JSON output consumable by the --params Bolt argument. Some values may need additional escaping to be properly passed to Bolt, while other types of values may not serialize in an easily consumable way. The PowerShell stop-parsing symbol --% may be used in some cases to address the escaping problem. A more comprehensive general solution will be addressed later.
Release Notes: Known Issue

Melissa Amos (JIRA)

unread,
Feb 21, 2019, 2:36:03 PM2/21/19
to puppe...@googlegroups.com
Melissa Amos updated an issue
Change By: Melissa Amos
Release Notes Summary: The PowerShell cmdlet ConvertTo When passing complex arguments to tasks with - Json typically does not produce -params, JSON output consumable by strings (typically created with the ConvertTo - -params Bolt argument. Some values may need Json cmdlet) might require additional escaping to be properly passed to Bolt, while other types of values may not serialize in an easily consumable way . The PowerShell In some cases, you can use the stop - parsing symbol --% may be used in some cases to address the escaping problem as a workaround . A more comprehensive general solution will be addressed later.

Melissa Amos (JIRA)

unread,
Feb 21, 2019, 2:41:05 PM2/21/19
to puppe...@googlegroups.com

Yasmin Rajabi (JIRA)

unread,
Aug 27, 2019, 2:59:03 PM8/27/19
to puppe...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages