Puppet Function: powershell_escape

Defined in:
vendor_modules/stdlib/lib/puppet/functions/powershell_escape.rb
Function type:
Ruby 4.x API

Summary

Escapes a string so that it can be safely used in a PowerShell command line.

Overview

powershell_escape(Any $string)Any

>* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single quotes.

Parameters:

  • string (Any)

    The string to escape

Returns:

  • (Any)

    An escaped string that can be safely used in a PowerShell command line.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'vendor_modules/stdlib/lib/puppet/functions/powershell_escape.rb', line 8

Puppet::Functions.create_function(:powershell_escape) do
  # @param string
  #   The string to escape
  #
  # @return
  #   An escaped string that can be safely used in a PowerShell command line.
  dispatch :powershell_escape do
    param 'Any', :string
  end

  def powershell_escape(string)
    result = ''

    string.to_s.chars.each do |char|
      result += case char
                when ' ', "'", '`', '|', "\n", '$' then "`#{char}"
                when '"' then '\`"'
                else char
                end
    end

    result
  end
end