Skip to content

Commit 720e250

Browse files
committed
Added get_item_property and use it for Win32::Registry and Get-ItemProperty
1 parent 288ffaf commit 720e250

File tree

1 file changed

+19
-57
lines changed

1 file changed

+19
-57
lines changed

ext/win32/resolv/lib/resolv.rb

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -82,47 +82,16 @@ def get_info
8282
search = nil
8383
nameserver = get_dns_server_list
8484

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
95-
end
85+
slist = get_item_property(TCPIP_NT, 'SearchList')
9686
search = slist.split(/,\s*/) unless slist.empty?
9787

9888
if add_search = search.nil?
9989
search = []
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
110-
end
90+
nvdom = get_item_property(TCPIP_NT, 'NV Domain')
11191

11292
unless nvdom.empty?
11393
@search = [ nvdom ]
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
124-
end
125-
94+
udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution').to_i
12695
if udmnd != 0
12796
if /^\w+\./ =~ nvdom
12897
devo = $'
@@ -131,7 +100,6 @@ def get_info
131100
end
132101
end
133102

134-
135103
ifs = if defined?(Win32::Registry)
136104
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces') do |reg|
137105
reg.keys
@@ -146,35 +114,15 @@ def get_info
146114

147115
ifs.each do |iface|
148116
next unless ns = %w[NameServer DhcpNameServer].find do |key|
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
159-
end
117+
ns = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
160118
break ns.split(/[,\s]\s*/) unless ns.empty?
161119
end
162120

163121
next if (nameserver & ns).empty?
164122

165123
if add_search
166124
[ 'Domain', 'DhcpDomain' ].each do |key|
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
177-
end
125+
dom = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
178126
unless dom.empty?
179127
search.concat(dom.split(/,\s*/))
180128
break
@@ -185,6 +133,20 @@ def get_info
185133
search << devo if add_search and devo
186134
[ search.uniq, nameserver.uniq ]
187135
end
136+
137+
def get_item_property(path, name)
138+
if defined?(Win32::Registry)
139+
Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
140+
reg.read_s(key)
141+
rescue Registry::Error
142+
""
143+
end
144+
else
145+
cmd = "Get-ItemProperty -Path 'HKLM:\\#{path}' -Name '#{name}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{name}"
146+
output, _ = Open3.capture2('powershell', '-Command', cmd)
147+
output.strip
148+
end
149+
end
188150
end
189151
end
190152
end

0 commit comments

Comments
 (0)