Module: Poise::Utils::Win32

Extended by:
Win32
Included in:
Win32
Defined in:
lib/poise/utils/win32.rb

Overview

Utilities for working with Windows.

Since:

  • 2.7.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.admin_userString

Find the name of the Administrator user, give or take localization.

Returns:

  • (String)

Since:

  • 2.7.0



49
50
51
52
53
54
55
56
57
# File 'lib/poise/utils/win32.rb', line 49

def admin_user
  if defined?(::WIN32OLE)
    wmi_property_from_query(:name, "select * from Win32_UserAccount where sid like 'S-1-5-21-%-500' and LocalAccount=True")
  else
    # Warn except under ChefSpec because it will just annoy people.
    Chef::Log.warn('[Poise::Utils::Win32] Unable to query admin user, WIN32OLE not available') unless defined?(ChefSpec)
    'Administrator'
  end
end

.reparse_command(*args) ⇒ Object

Take a string or array command in the format used by shell_out et al and create something we can use on Windows.

@

Since:

  • 2.7.0



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/poise/utils/win32.rb', line 107

def reparse_command(*args)
  array_mode = !(args.length == 1 && args.first.is_a?(String))
  # At some point when mixlib-shellout groks array commands on Windows,
  # we should support that here.
  parsed_args = array_mode ? args.flatten : Shellwords.split(args.first)
  cmd = parsed_args.map {|s| argv_quote(s) }.join(' ')
  if array_mode
    # This fails on non-Windows because of win32/process.
    require 'mixlib/shellout/windows'
    if Mixlib::ShellOut::Windows::Utils.should_run_under_cmd?(cmd)
      # If we are in array mode, try to make cmd.exe keep its grubby paws
      # off our metacharacters.
      cmd = cmd.each_char.map {|c| '^'+c }.join('')
    end
  end
  cmd
end

Instance Method Details

#admin_userString

Find the name of the Administrator user, give or take localization.

Returns:

  • (String)

Since:

  • 2.7.0



49
50
51
52
53
54
55
56
57
# File 'lib/poise/utils/win32.rb', line 49

def admin_user
  if defined?(::WIN32OLE)
    wmi_property_from_query(:name, "select * from Win32_UserAccount where sid like 'S-1-5-21-%-500' and LocalAccount=True")
  else
    # Warn except under ChefSpec because it will just annoy people.
    Chef::Log.warn('[Poise::Utils::Win32] Unable to query admin user, WIN32OLE not available') unless defined?(ChefSpec)
    'Administrator'
  end
end

#reparse_command(*args) ⇒ Object

Take a string or array command in the format used by shell_out et al and create something we can use on Windows.

@

Since:

  • 2.7.0



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/poise/utils/win32.rb', line 107

def reparse_command(*args)
  array_mode = !(args.length == 1 && args.first.is_a?(String))
  # At some point when mixlib-shellout groks array commands on Windows,
  # we should support that here.
  parsed_args = array_mode ? args.flatten : Shellwords.split(args.first)
  cmd = parsed_args.map {|s| argv_quote(s) }.join(' ')
  if array_mode
    # This fails on non-Windows because of win32/process.
    require 'mixlib/shellout/windows'
    if Mixlib::ShellOut::Windows::Utils.should_run_under_cmd?(cmd)
      # If we are in array mode, try to make cmd.exe keep its grubby paws
      # off our metacharacters.
      cmd = cmd.each_char.map {|c| '^'+c }.join('')
    end
  end
  cmd
end