Module: Bolt::Shell::Powershell::Snippets

Defined in:
lib/bolt/shell/powershell/snippets.rb

Class Method Summary collapse

Class Method Details

.execute_process(command) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bolt/shell/powershell/snippets.rb', line 8

def execute_process(command)
  "  if ([Console]::InputEncoding -eq [System.Text.Encoding]::UTF8) {\n    [Console]::InputEncoding = New-Object System.Text.UTF8Encoding $False\n  }\n  if ([Console]::OutputEncoding -eq [System.Text.Encoding]::UTF8) {\n    [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding $False\n  }\n  $OutputEncoding = [Console]::OutputEncoding\n  \#{command}\n  if (-not $? -and ($LASTEXITCODE -eq $null)) { exit 1 }\n  exit $LASTEXITCODE\n  PS\nend\n"

.exit_with_code(command) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/bolt/shell/powershell/snippets.rb', line 23

def exit_with_code(command)
  "  \#{command}\n  if (-not $? -and ($LASTEXITCODE -eq $null)) { exit 1 }\n  exit $LASTEXITCODE\n  PS\nend\n"

.make_tmpdir(parent) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/bolt/shell/powershell/snippets.rb', line 31

def make_tmpdir(parent)
  "  $parent = \#{parent}\n  $name = [System.IO.Path]::GetRandomFileName()\n  $path = Join-Path $parent $name -ErrorAction Stop\n  New-Item -ItemType Directory -Path $path -ErrorAction Stop | Out-Null\n  $path\n  PS\nend\n"

.ps_task(path, arguments) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bolt/shell/powershell/snippets.rb', line 70

def ps_task(path, arguments)
  "  $private:tempArgs = Get-ContentAsJson (\n    [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('\#{Base64.encode64(JSON.dump(arguments))}'))\n  )\n  $allowedArgs = (Get-Command \"\#{path}\").Parameters.Keys\n  $private:taskArgs = @{}\n  $private:tempArgs.Keys | ? { $allowedArgs -contains $_ } | % { $private:taskArgs[$_] = $private:tempArgs[$_] }\n  try {\n    & \"\#{path}\" @taskArgs\n  } catch {\n    $Host.UI.WriteErrorLine(\"[$($_.FullyQualifiedErrorId)] Exception $($_.InvocationInfo.PositionMessage).`n$($_.Exception.Message)\");\n    exit 1;\n  }\n  PS\nend\n"

.rmdir(dir) ⇒ Object



41
42
43
44
45
# File 'lib/bolt/shell/powershell/snippets.rb', line 41

def rmdir(dir)
  "  Remove-Item -Force -Recurse -Path \"\#{dir}\"\n  PS\nend\n"

.run_script(arguments, script_path) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bolt/shell/powershell/snippets.rb', line 47

def run_script(arguments, script_path)
  build_arg_list = arguments.map do |a|
    "$invokeArgs.ArgumentList += @'\n#{a}\n'@"
  end.join("\n")
  "  $invokeArgs = @{\n    ScriptBlock = (Get-Command \"\#{script_path}\").ScriptBlock\n    ArgumentList = @()\n  }\n  \#{build_arg_list}\n\n  try\n  {\n    Invoke-Command @invokeArgs\n  }\n  catch\n  {\n    Write-Error $_.Exception\n    exit 1\n  }\n  PS\nend\n"

.shell_initObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bolt/shell/powershell/snippets.rb', line 91

def shell_init
  "  $installRegKey = Get-ItemProperty -Path \"HKLM:\\\\Software\\\\Puppet Labs\\\\Puppet\" -ErrorAction 0\n  if(![string]::IsNullOrEmpty($installRegKey.RememberedInstallDir64)){\n    $boltBaseDir = $installRegKey.RememberedInstallDir64\n  }elseif(![string]::IsNullOrEmpty($installRegKey.RememberedInstallDir)){\n    $boltBaseDir = $installRegKey.RememberedInstallDir\n  }else{\n    $boltBaseDir = \"${ENV:ProgramFiles}\\\\Puppet Labs\\\\Puppet\"\n  }\n\n  $ENV:PATH += \";${boltBaseDir}\\\\bin\\\\;\" +\n  \"${boltBaseDir}\\\\puppet\\\\bin;\" +\n  \"${boltBaseDir}\\\\sys\\\\ruby\\\\bin\\\\\"\n  $ENV:RUBYLIB = \"${boltBaseDir}\\\\puppet\\\\lib;\" +\n  \"${boltBaseDir}\\\\facter\\\\lib;\" +\n  \"${boltBaseDir}\\\\hiera\\\\lib;\" +\n  $ENV:RUBYLIB\n\n  function ConvertFrom-PSCustomObject\n  {\n  PARAM([Parameter(ValueFromPipeline = $true)] $InputObject)\n  PROCESS {\n    if ($null -eq $InputObject) { return $null }\n    if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {\n      $collection = @(\n        foreach ($object in $InputObject) { ConvertFrom-PSCustomObject $object }\n      )\n\n      $collection\n    } elseif ($InputObject -is [System.Management.Automation.PSCustomObject]) {\n      $hash = @{}\n      foreach ($property in $InputObject.PSObject.Properties) {\n        $hash[$property.Name] = ConvertFrom-PSCustomObject $property.Value\n      }\n\n      $hash\n    } else {\n      $InputObject\n    }\n  }\n  }\n\n  function Get-ContentAsJson\n  {\n  [CmdletBinding()]\n  PARAM(\n    [Parameter(Mandatory = $true)] $Text,\n    [Parameter(Mandatory = $false)] [Text.Encoding] $Encoding = [Text.Encoding]::UTF8\n  )\n\n  $Text | ConvertFrom-Json | ConvertFrom-PSCustomObject\n  }\n  PS\nend\n"

.try_catch(command) ⇒ Object



87
88
89
# File 'lib/bolt/shell/powershell/snippets.rb', line 87

def try_catch(command)
  %(try { & "#{command}" } catch { Write-Error $_.Exception; exit 1 })
end