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
Changes from 1 commit
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
21 changes: 19 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,28 @@ 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?

@platform[:family] = "windows"
@platform[:release] = command["Version"]
@platform[:name] = command["Caption"]

read_wmic
true
end

Expand Down