Added get_item_property and use it for Win32::Registry and Get-ItemProperty
This commit is contained in:
parent
a487698cc7
commit
333bc26d82
Notes:
git
2025-02-04 22:37:19 +00:00
@ -82,47 +82,16 @@ module Win32
|
|||||||
search = nil
|
search = nil
|
||||||
nameserver = get_dns_server_list
|
nameserver = get_dns_server_list
|
||||||
|
|
||||||
slist = if defined?(Win32::Registry)
|
slist = get_item_property(TCPIP_NT, 'SearchList')
|
||||||
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
|
|
||||||
reg.read_s('SearchList')
|
|
||||||
rescue Registry::Error
|
|
||||||
""
|
|
||||||
end
|
|
||||||
else
|
|
||||||
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'SearchList' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty SearchList"
|
|
||||||
output, _ = Open3.capture2('powershell', '-Command', cmd)
|
|
||||||
output.strip
|
|
||||||
end
|
|
||||||
search = slist.split(/,\s*/) unless slist.empty?
|
search = slist.split(/,\s*/) unless slist.empty?
|
||||||
|
|
||||||
if add_search = search.nil?
|
if add_search = search.nil?
|
||||||
search = []
|
search = []
|
||||||
nvdom = if defined?(Win32::Registry)
|
nvdom = get_item_property(TCPIP_NT, 'NV Domain')
|
||||||
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
|
|
||||||
reg.read_s('NV Domain')
|
|
||||||
rescue Registry::Error
|
|
||||||
""
|
|
||||||
end
|
|
||||||
else
|
|
||||||
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'NV Domain' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty NV domain"
|
|
||||||
output, _ = Open3.capture2('powershell', '-Command', cmd)
|
|
||||||
output.strip
|
|
||||||
end
|
|
||||||
|
|
||||||
unless nvdom.empty?
|
unless nvdom.empty?
|
||||||
@search = [ nvdom ]
|
@search = [ nvdom ]
|
||||||
udmnd = if defined?(Win32::Registry)
|
udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution').to_i
|
||||||
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
|
|
||||||
reg.read_i('UseDomainNameDevolution')
|
|
||||||
rescue Registry::Error
|
|
||||||
0
|
|
||||||
end
|
|
||||||
else
|
|
||||||
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'UseDomainNameDevolution' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty UseDomainNameDevolution"
|
|
||||||
output, _ = Open3.capture2('powershell', '-Command', cmd)
|
|
||||||
output.strip.to_i
|
|
||||||
end
|
|
||||||
|
|
||||||
if udmnd != 0
|
if udmnd != 0
|
||||||
if /^\w+\./ =~ nvdom
|
if /^\w+\./ =~ nvdom
|
||||||
devo = $'
|
devo = $'
|
||||||
@ -131,7 +100,6 @@ module Win32
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
ifs = if defined?(Win32::Registry)
|
ifs = if defined?(Win32::Registry)
|
||||||
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces') do |reg|
|
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces') do |reg|
|
||||||
reg.keys
|
reg.keys
|
||||||
@ -146,17 +114,7 @@ module Win32
|
|||||||
|
|
||||||
ifs.each do |iface|
|
ifs.each do |iface|
|
||||||
next unless ns = %w[NameServer DhcpNameServer].find do |key|
|
next unless ns = %w[NameServer DhcpNameServer].find do |key|
|
||||||
ns = if defined?(Win32::Registry)
|
ns = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
|
||||||
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces' + "\\#{iface}" ) do |regif|
|
|
||||||
regif.read_s(key)
|
|
||||||
rescue Registry::Error
|
|
||||||
""
|
|
||||||
end
|
|
||||||
else
|
|
||||||
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name '#{key}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{key}"
|
|
||||||
output, _ = Open3.capture2('powershell', '-Command', cmd)
|
|
||||||
output.strip
|
|
||||||
end
|
|
||||||
break ns.split(/[,\s]\s*/) unless ns.empty?
|
break ns.split(/[,\s]\s*/) unless ns.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -164,17 +122,7 @@ module Win32
|
|||||||
|
|
||||||
if add_search
|
if add_search
|
||||||
[ 'Domain', 'DhcpDomain' ].each do |key|
|
[ 'Domain', 'DhcpDomain' ].each do |key|
|
||||||
dom = if defined?(Win32::Registry)
|
dom = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
|
||||||
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces' + "\\#{iface}" ) do |regif|
|
|
||||||
regif.read_s(key)
|
|
||||||
rescue Registry::Error
|
|
||||||
""
|
|
||||||
end
|
|
||||||
else
|
|
||||||
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name '#{key}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{key}"
|
|
||||||
output, _ = Open3.capture2('powershell', '-Command', cmd)
|
|
||||||
output.strip
|
|
||||||
end
|
|
||||||
unless dom.empty?
|
unless dom.empty?
|
||||||
search.concat(dom.split(/,\s*/))
|
search.concat(dom.split(/,\s*/))
|
||||||
break
|
break
|
||||||
@ -185,6 +133,20 @@ module Win32
|
|||||||
search << devo if add_search and devo
|
search << devo if add_search and devo
|
||||||
[ search.uniq, nameserver.uniq ]
|
[ search.uniq, nameserver.uniq ]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_item_property(path, name)
|
||||||
|
if defined?(Win32::Registry)
|
||||||
|
Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
|
||||||
|
reg.read_s(key)
|
||||||
|
rescue Registry::Error
|
||||||
|
""
|
||||||
|
end
|
||||||
|
else
|
||||||
|
cmd = "Get-ItemProperty -Path 'HKLM:\\#{path}' -Name '#{name}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{name}"
|
||||||
|
output, _ = Open3.capture2('powershell', '-Command', cmd)
|
||||||
|
output.strip
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user