Module: Fog::Hyperv::Utils::Powershell

Defined in:
lib/fog/hyperv/utils/powershell.rb

Class Method Summary collapse

Class Method Details

.build_call(cmdlet, args = {}, _ps_version: 5) ⇒ Object

Build a Powershell cmdlet call

Examples:

Running a simple cmdlet

build_call 'Get-VM', { name: 'example VM' }, _ps_version: 7
=> "$Fogwozmqayrgsyd = ConvertFrom-Json -AsHashtable '{"Name": "example VM"}\nGet-VM @Fogwozmqayrgsyd"

Parameters:

  • cmdlet (String)

    the PowerShell cmdlet to call

  • args (Hash) (defaults to: {})

    the argument to call the cmdlet with

  • _ps_version (Integer) (defaults to: 5)

    the major version of PowerShell the call is being made for



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fog/hyperv/utils/powershell.rb', line 41

def self.build_call(cmdlet, args = {}, _ps_version: 5)
  return [cmdlet.gsub('@Args', '')].flatten if args.empty?

  id = (cmdlet.hash ^ args.hash).abs.to_s(36)
  optmap = "Fog#{id}"

  commands = []
  commands += build_optmap(_optmap_name: optmap, _ps_version:, **args)
  commands << if cmdlet.include? '@Args'
                cmdlet.gsub('@Args', "@#{optmap}")
              else
                "#{cmdlet} @#{optmap}"
              end
  commands
end

.build_optmap(_optmap_name: 'FogArgs', _ps_version: 5, _from_json: true, **_options) ⇒ Object

Build a Powershell option map from a set of keyword arguments

Parameters:

  • _optmap_name (String) (defaults to: 'FogArgs')

    the name of the option map to generate

  • _ps_version (Integer) (defaults to: 5)

    the major version of PowerShell to generate the option map for

  • _from_json (Boolean) (defaults to: true)

    should the option map be built from JSON data

  • options (Hash)

    the options to convert into a PowerShell option map



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/hyperv/utils/powershell.rb', line 9

def self.build_optmap(_optmap_name: 'FogArgs', _ps_version: 5, _from_json: true, **_options)
  ps_opts = Fog::Hyperv.camelize(_options)
  if _from_json
    if _ps_version >= 6
      ["$#{_optmap_name} = ConvertFrom-Json -AsHashtable '#{Fog::JSON.encode ps_opts}'"]
    else
      [
        [
          "$FogJsonObject = '#{Fog::JSON.encode ps_opts}'",
          '$FogJsonParameters = ConvertFrom-Json -InputObject $FogJsonObject',
          "$#{_optmap_name} = @{}",
          "$FogJsonParameters.psobject.properties | Foreach { $#{_optmap_name}[$_.Name] = $_.Value }"
        ].join('; ')
      ]
    end
  else
    args = ps_opts.map do |k, v|
      "'#{k}'=#{Fog::Hyperv.shell_quote(v, always: true)}"
    end

    ["$#{_optmap_name} = @{#{args.join ';'}}"]
  end
end

.shell_quote(data, always: false) ⇒ Object

Convert Ruby data to PowerShell equivalents

Parameters:

  • data (Object)

    the Ruby data to convert

  • always (Boolean) (defaults to: false)

    should the data be included even if the equivalent would be a no-op



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fog/hyperv/utils/powershell.rb', line 60

def self.shell_quote(data, always: false)
  case data
  when String
    if !data.start_with?('$') && (data =~ /(^$)|\s/ || always)
      data.gsub('`', '``')
          .gsub(/\0/, '`0')
          .gsub("\n", '`n')
          .gsub("\r", '`r')
          .inspect
          .gsub('\"', '`"')
          .gsub('\\\\', '\\')
    else
      data
    end
  when Hash
    raise 'Hashes need to be run through build_optmap'
  when Array
    "@(#{data.map { |e| shell_quoted(e, always: true) }.join(', ')})"
  when FalseClass
    '$false'
  when TrueClass
    '$true'
  when nil
    '$null'
  else
    shell_quoted data.to_s
  end
end