Class: Pwsh::WindowsPowerShell

Inherits:
Object
  • Object
show all
Defined in:
lib/pwsh/windows_powershell.rb,
lib/pwsh/windows_powershell.rb

Overview

Returns information about the available versions of Windows PowerShell on the node, if any.

Constant Summary collapse

ACCESS_TYPE =

Shorthand constant to reference the registry key access type

Win32::Registry::KEY_READ | 0x100
HKLM =

Shorthand constant to reference the local machine hive

Win32::Registry::HKEY_LOCAL_MACHINE
PS_ONE_REG_PATH =

The path to the original version of the Windows PowerShell Engine’s data in registry

'SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine'
PS_THREE_REG_PATH =

The path to the newer version of the Windows PowerShell Engine’s data in registry

'SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine'
REG_KEY =

The name of the registry key for looking up the latest version of Windows PowerShell for a given engine.

'PowerShellVersion'

Class Method Summary collapse

Class Method Details

.compatible_version?Boolean

Return whether or not the latest version of PowerShell available on the machine is compatible with the implementation of the Manager.

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pwsh/windows_powershell.rb', line 10

def self.compatible_version?
  # If this method isn't defined, we're not on Windows!
  return false if defined?(Pwsh::WindowsPowerShell.version).nil?

  powershell_version = defined?(Pwsh::WindowsPowerShell.version) ? Pwsh::WindowsPowerShell.version : nil

  # If we get nil, something's gone wrong and we're not compatible.
  return false if powershell_version.nil?

  # PowerShell v1 - definitely not good to go. Really the whole library
  # may not even work but I digress
  return false if Gem::Version.new(powershell_version) < Gem::Version.new(2)

  # PowerShell v3+, we are good to go b/c .NET 4+
  # https://msdn.microsoft.com/en-us/powershell/scripting/setup/windows-powershell-system-requirements
  # Look at Microsoft .NET Framwork Requirements section.
  return true if Gem::Version.new(powershell_version) >= Gem::Version.new(3)

  # If we are using PowerShell v2, we need to see what the latest
  # version of .NET is that we have
  # https://msdn.microsoft.com/en-us/library/hh925568.aspx
  value = false
  if Pwsh::Util.on_windows?
    require 'win32/registry'
    begin
      # At this point in the check, PowerShell is using .NET Framework
      # 2.x family, so we only need to verify v3.5 key exists.
      # If we were verifying all compatible types we would look for
      # any of these keys: v3.5, v4.0, v4
      Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5', Win32::Registry::KEY_READ | 0x100) do
        value = true
      end
    rescue Win32::Registry::Error
      value = false
    end
  end

  value
end

.powershell_one_versionString

Returns the latest available version of Windows PowerShell using the older engine as determined by checking the registry.

Returns:

  • (String)

    a version string representing the latest version of Windows PowerShell using the original engine



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pwsh/windows_powershell.rb', line 79

def self.powershell_one_version
  version = nil
  begin
    HKLM.open(PS_ONE_REG_PATH, ACCESS_TYPE) do |reg|
      version = reg[REG_KEY]
    end
  rescue StandardError
    version = nil
  end
  version
end

.powershell_three_versionString

Returns the latest available version of Windows PowerShell as determined by checking the registry.

Returns:

  • (String)

    a version string representing the latest version of Windows PowerShell using the newer engine



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pwsh/windows_powershell.rb', line 95

def self.powershell_three_version
  version = nil
  begin
    HKLM.open(PS_THREE_REG_PATH, ACCESS_TYPE) do |reg|
      version = reg[REG_KEY]
    end
  rescue StandardError
    version = nil
  end
  version
end

.versionString

Returns the latest available version of Windows PowerShell on the machine

Returns:

  • (String)

    a version string representing the latest version of Windows PowerShell available



71
72
73
# File 'lib/pwsh/windows_powershell.rb', line 71

def self.version
  powershell_three_version || powershell_one_version
end