Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add powershell detection #523

Merged
merged 2 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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