Module: Train::Extras::DetectWindows

Included in:
OSCommon
Defined in:
lib/train/extras/os_detect_windows.rb

Constant Summary collapse

WINDOWS_VERSIONS =

See: msdn.microsoft.com/en-us/library/windows/desktop/ms724832%28v=vs.85%29.aspx Product Type: Work Station (1) Domain Controller (2) Server (3)

{
  '0' => '3.1',
  '140' => '95',
  '1410' => '98',
  '1490' => 'ME',
  '1351' => 'NT 3.51',
  '3351' => 'NT 3.51 Server',
  '1240' => 'NT 4.0',
  '3240' => 'NT 4.0 Server',
  '1250' => '2000',
  '1251' => 'XP',
  '3252' => 'Server 2003',
  '1252' => 'Vista',
  '3260' => 'Server 2008',
  '1261' => '7',
  '3261' => 'Server 2008 R2',
  '1262' => '8',
  '3262' => 'Server 2012',
  '1263' => '8.1',
  '3263' => 'Server 2012 R2',
  '12100' => '10',
  '32100' => 'Server 2016',
}

Instance Method Summary collapse

Instance Method Details

#detect_windowsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/train/extras/os_detect_windows.rb', line 56

def detect_windows
  cmd = 'New-Object -Type PSObject | Add-Member -MemberType NoteProperty '\
        '-Name OS -Value (Get-WmiObject -Class Win32_OperatingSystem) '\
        '-PassThru | Add-Member -MemberType NoteProperty -Name OSVersion '\
        '-Value ([Environment]::OSVersion) -PassThru | ConvertTo-Json'

  # wrap the script to ensure we always run it via powershell
  # especially in local mode, we cannot be sure that we get a Powershell
  # we may just get a `cmd`. os detection and powershell command wrapper is
  # not available when this code is executed
  script = WinRM::PowershellScript.new(cmd)
  cmd = "powershell -encodedCommand #{script.encoded}"

  res = @backend.run_command(cmd)

  # TODO: error as this shouldnt be happening at this point
  return false if res.exit_status != 0 or res.stdout.empty?

  json = JSON.parse(res.stdout)
  return false if json.nil? or json.empty?
  version = windows_version(json)

  @platform[:family] = 'windows'
  @platform[:release] = WINDOWS_VERSIONS[version]

  true
end

#windows_version(json) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/train/extras/os_detect_windows.rb', line 45

def windows_version(json)
  producttype = json['OS']['ProductType'].to_s
  # do not distigush between domain controller and server
  producttype = '3' if producttype == '2'
  platform = json['OSVersion']['Platform'].to_s
  major = json['OSVersion']['Version']['Major'].to_s
  minor = json['OSVersion']['Version']['Minor'].to_s
  # construct it
  producttype + platform + major + minor
end