Skip to content

Commit

Permalink
Merge pull request #523 from inspec/mj/ps
Browse files Browse the repository at this point in the history
Add powershell detection
  • Loading branch information
miah authored Oct 9, 2019
2 parents 8926b0a + a4db189 commit 5f5b4b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/train/platforms/detect/helpers/os_windows.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module Train::Platforms::Detect::Helpers
module Windows
def detect_windows
check_cmd || check_powershell
end

def check_cmd
# try to detect windows, use cmd.exe to also support Microsoft OpenSSH
res = @backend.run_command("cmd.exe /c ver")

return false if (res.exit_status != 0) || res.stdout.empty?

# if the ver contains `Windows`, we know its a Windows system
Expand All @@ -13,16 +18,29 @@ def detect_windows

# try to extract release from eg. `Microsoft Windows [Version 6.3.9600]`
release = /\[(?<name>.*)\]/.match(version)
unless release[:name].nil?
if release[:name]
# release is 6.3.9600 now
@platform[:release] = release[:name].downcase.gsub("version", "").strip
# fallback, if we are not able to extract the name from wmic later
@platform[:name] = "Windows #{@platform[:release]}"
end

# try to use wmic, but lets keep it optional
read_wmic
true
end

def check_powershell
command = @backend.run_command(
"Get-WmiObject Win32_OperatingSystem | Select Caption,Version | ConvertTo-Json"
)
return false if (command.exit_status != 0) || command.stdout.empty?

payload = JSON.parse(command.stdout)
@platform[:family] = "windows"
@platform[:release] = payload["Version"]
@platform[:name] = payload["Caption"]

read_wmic
true
end

Expand Down
19 changes: 19 additions & 0 deletions test/unit/platforms/detect/os_windows_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ def initialize
end
end

describe "Windows 10 with Powershell" do
let(:detector) do
detector = OsDetectWindowsTester.new
detector.backend.mock_command("Get-WmiObject Win32_OperatingSystem | Select Caption,Version | ConvertTo-Json", "{\"Caption\":\"Microsoft Windows 10 Pro\", \"Version\": \"10.0.18362\"}", "", 0)
detector.backend.mock_command("wmic os get * /format:list", "\r\r\nBuildNumber=10240\r\r\nCaption=Microsoft Windows 10 Pro\r\r\nOSArchitecture=64-bit\r\r\nVersion=10.0.18362\r\r\n\r\r\n" , "", 0)
detector.backend.mock_command("wmic cpu get architecture /format:list", "\r\r\nArchitecture=9\r\r\n" , "", 0)
detector
end

it "sets the correct family/release for windows" do
detector.detect_windows
detector.platform[:family].must_equal("windows")
detector.platform[:name].must_equal("Windows 10 Pro")
detector.platform[:arch].must_equal("x86_64")
detector.platform[:release].must_equal("10.0.18362")
end

end

describe "windows 98" do
let(:detector) do
detector = OsDetectWindowsTester.new
Expand Down

0 comments on commit 5f5b4b3

Please sign in to comment.