Skip to content

Commit

Permalink
Add powershell detection
Browse files Browse the repository at this point in the history
Signed-off-by: Miah Johnson <miah@chia-pet.org>
  • Loading branch information
miah committed Oct 8, 2019
1 parent 8926b0a commit 0661a8f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 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,27 @@ 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

0 comments on commit 0661a8f

Please sign in to comment.