Skip to content

Commit a487698

Browse files
committed
Use powershell for retrieving value from registry if fiddle is not available
1 parent 078e723 commit a487698

File tree

1 file changed

+70
-26
lines changed

1 file changed

+70
-26
lines changed

ext/win32/lib/win32/resolv.rb

Lines changed: 70 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,60 @@ def read_s(key)
6767
class << self
6868
private
6969
def get_hosts_dir
70-
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
71-
reg.read_s_expand('DataBasePath')
70+
if defined?(Win32::Registry)
71+
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
72+
reg.read_s_expand('DataBasePath')
73+
end
74+
else
75+
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'DataBasePath' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty DataBasePath"
76+
output, _ = Open3.capture2('powershell', '-Command', cmd)
77+
output.strip
7278
end
7379
end
7480

7581
def get_info
7682
search = nil
7783
nameserver = get_dns_server_list
78-
slist = Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
79-
reg.read_s('SearchList')
80-
rescue Registry::Error
81-
""
84+
85+
slist = if defined?(Win32::Registry)
86+
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
87+
reg.read_s('SearchList')
88+
rescue Registry::Error
89+
""
90+
end
91+
else
92+
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'SearchList' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty SearchList"
93+
output, _ = Open3.capture2('powershell', '-Command', cmd)
94+
output.strip
8295
end
8396
search = slist.split(/,\s*/) unless slist.empty?
8497

8598
if add_search = search.nil?
8699
search = []
87-
nvdom = Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
88-
reg.read_s('NV Domain')
89-
rescue Registry::Error
90-
""
100+
nvdom = if defined?(Win32::Registry)
101+
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
102+
reg.read_s('NV Domain')
103+
rescue Registry::Error
104+
""
105+
end
106+
else
107+
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'NV Domain' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty NV domain"
108+
output, _ = Open3.capture2('powershell', '-Command', cmd)
109+
output.strip
91110
end
92111

93112
unless nvdom.empty?
94113
@search = [ nvdom ]
95-
udmnd = Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
96-
reg.read_i('UseDomainNameDevolution')
97-
rescue Registry::Error
98-
0
114+
udmnd = if defined?(Win32::Registry)
115+
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
116+
reg.read_i('UseDomainNameDevolution')
117+
rescue Registry::Error
118+
0
119+
end
120+
else
121+
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'UseDomainNameDevolution' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty UseDomainNameDevolution"
122+
output, _ = Open3.capture2('powershell', '-Command', cmd)
123+
output.strip.to_i
99124
end
100125

101126
if udmnd != 0
@@ -106,18 +131,31 @@ def get_info
106131
end
107132
end
108133

109-
ifs = Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces') do |reg|
110-
reg.keys
111-
rescue Registry::Error
112-
[]
134+
135+
ifs = if defined?(Win32::Registry)
136+
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces') do |reg|
137+
reg.keys
138+
rescue Registry::Error
139+
[]
140+
end
141+
else
142+
cmd = "Get-ChildItem 'HKLM:\\#{TCPIP_NT}\\Interfaces' | ForEach-Object { $_.PSChildName }"
143+
output, _ = Open3.capture2('powershell', '-Command', cmd)
144+
output.split(/\n+/)
113145
end
114146

115147
ifs.each do |iface|
116148
next unless ns = %w[NameServer DhcpNameServer].find do |key|
117-
ns = Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces' + "\\#{iface}" ) do |regif|
118-
regif.read_s(key)
119-
rescue Registry::Error
120-
""
149+
ns = if defined?(Win32::Registry)
150+
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces' + "\\#{iface}" ) do |regif|
151+
regif.read_s(key)
152+
rescue Registry::Error
153+
""
154+
end
155+
else
156+
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name '#{key}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{key}"
157+
output, _ = Open3.capture2('powershell', '-Command', cmd)
158+
output.strip
121159
end
122160
break ns.split(/[,\s]\s*/) unless ns.empty?
123161
end
@@ -126,10 +164,16 @@ def get_info
126164

127165
if add_search
128166
[ 'Domain', 'DhcpDomain' ].each do |key|
129-
dom = Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces' + "\\#{iface}" ) do |regif|
130-
regif.read_s(key)
131-
rescue Registry::Error
132-
""
167+
dom = if defined?(Win32::Registry)
168+
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces' + "\\#{iface}" ) do |regif|
169+
regif.read_s(key)
170+
rescue Registry::Error
171+
""
172+
end
173+
else
174+
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name '#{key}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{key}"
175+
output, _ = Open3.capture2('powershell', '-Command', cmd)
176+
output.strip
133177
end
134178
unless dom.empty?
135179
search.concat(dom.split(/,\s*/))

0 commit comments

Comments
 (0)