我对脚本非常陌生,但我会解释我想要做什么。 我想动态地将我的DNS服务器添加到我的同事机器中。我需要弄清楚如何做到这一点。每次我们登录时,我们的VPN都会重置DNS服务器列表。一旦进入,我可以更改设置,但这很烦人。我想创建一个
的脚本我也会为搜索顺序执行此操作,但我认为一旦我能够得到这个答案,我就可以继续前进。
我正在查看这篇文章以帮助设置DNS服务器搜索顺序,但是它没有关于如何查看它们的任何信息。 http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/28/use-powershell-to-configure-static-ip-and-dns-settings.aspx
我确信我可以修改并设置DNS服务器IP地址,一旦我可以让它读取当前状态并填充变量。
我试着遵循它,然后用它来读取dns服务器搜索顺序并填充变量,但它似乎不喜欢它。这个库说有DNSServerSearchOrder
,但是我无法获得保存到变量的任何内容。
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394217(v=vs.85).aspx
我尝试在VBS中执行此操作,并且在设置数据后调整数组时出现了一些问题。我从显示DNS服务器的其他人的脚本中获取了这个。我只是试图操纵变量来重新排序,但它无法正常工作。我希望PowerShell能够更容易地修改数组。
On Error Resume Next
strComputer = "."
sithDomain = "sith.empire.com"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objNicConfig In colNicConfigs
DIM strDNSSuffixSO(1)
strDNSServerSO = ""
strDNSHostName = objNicConfig.DNSHostName
strIndex = objNicConfig.Index
strDescription = objNicConfig.Description
strDNSDomain = objNicConfig.DNSDomain
If Not IsNull(objNicConfig.DNSDomainSuffixSearchOrder) Then
For Each strDNSSuffix In objNicConfig.DNSDomainSuffixSearchOrder
ReDim Preserve strDNSSuffixSO(UBound(strDNSSuffixSO) + 1)
strDNSSuffixSO(UBound(strDNSSuffixSO)) = strDNSSuffix
Next
End If
i=0
sithFound = False
'Wscript.echo sithFound
For Each strDNSSuffix In strDNSSuffixSO
If strDNSSuffix = sithDomain Then
strDNSSuffixSO(i) = strDNSSuffixSO(0)
strDNSSuffixSO(0) = sithDomain
sithFound = True
End If
i = i+1
Next
If sithFound = False Then
ReDim Preserve strDNSSuffixSO(UBound(strDNSSuffixSO) + 1)
strDNSSuffixSO(UBound(strDNSSuffixSO)) = strDNSSuffixSO(0)
strDNSSuffixSO(0) = sithDomain
End If
strDNSServerSO = ""
If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
For Each strDNSServer In objNicConfig.DNSServerSearchOrder
strDNSServerSO = strDNSServerSO & VbCrLf & String(37, " ") & _
strDNSServer
Next
End If
strDomainDNSRegistrationEnabled = _
objNicConfig.DomainDNSRegistrationEnabled
strFullDNSRegistrationEnabled = objNicConfig.FullDNSRegistrationEnabled
strDNSSettings = strDNSSettings & VbCrLf & VbCrLf & _
" Network Adapter " & strIndex & VbCrLf & _
" DNS Domain Suffix Search Order:" & strDNSSuffixSO(0) & strDNSSuffixSO(1) '& strDNSSuffixSO(2) & VbCrLf '& _
REM " DNS Server Search Order:" & strDNSServerSO & VbCrLf
REM " Domain DNS Registration Enabled: " & _
REM strDomainDNSRegistrationEnabled & VbCrLf & _
REM " Full DNS Registration Enabled: " & _
REM strFullDNSRegistrationEnabled
Next
WScript.Echo VbCrLf & "DNS Settings" & VbCrLf & VbCrLf & _
"Host Name: " & strDNSHostName & strDNSSettings & VbCrLf & sithFound
答案 0 :(得分:2)
我认为这就是你要找的东西。有关详细信息,请参阅内联文档/注释。
# 1. Get all instances of the Win32_NetworkAdapterConfiguration WMI class
# that are "IPEnabled"
$NetAdapterConf = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IpEnabled = 'true' and Description like '%centrino%'";
# 2. Declare an array of DNS servers to configure
$DnsServerArray = '8.8.8.8', '8.8.4.4';
# 3. For each instance of Win32_NetworkAdapterConfiguration, call the
# SetDNSServerSearchOrder WMI method, passing in the array of DNS
# servers as the one and only parameter. We also append the existing
# DNS servers, so as not to override them completely.
foreach ($NetAdapterConf in $NetAdapterConfList) {
$NetAdapterConf.SetDNSServerSearchOrder(($DnsServerArray + $NetAdapterConf.DNSServerSearchOrder));
}
这是一个非常简单的脚本,只是将DNS服务器搜索顺序配置为$DnsServerArray
变量(Array
String
个{{1}}对象中指定的任何内容。 / p>
编辑:修改脚本以包含用户现有的DNS服务器。