使用一个脚本在 Windows 7 和 Windows 10 中设置新 DNS 设置的 Powershell 脚本
Powershell script to set new DNS settings in Windows 7 and Windows 10 with one script
需要帮助创建一个可以与 Windows 7 (powershell 2.0) 和 Windows 10 (powershell 5.0) 一起使用的 powershell 脚本,以便可以通过 GPO 推送我。 *我们没有域 DHCP 是我们从 MPLS 路由器获得 IP 租约...
到目前为止,我已经使用 windows 7(非 powershell)
的批处理文件完成了以下操作
@ECHO OFF
set vardns1=1.1.1.1
set vardns2=2.2.2.2
ECHO Setting Primary DNS
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%
ECHO Setting Secondary DNS
netsh int ip add dns name = "Local Area Connection" addr = %vardns2%
ECHO Flushing DNS
ipconfig /flushdns
ECHO Registering New DNS settings
ipconfig /registerdns
此代码用于 Windows 10 (powershell 5.0)
If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent
()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
Exit
}
#The servers that we want to use
$newDNSServers = "1.1.1.1","2.2.2.2"
# Get all network adapters that already have DNS servers set
$adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DNSServerSearchOrder -ne $null}
# Set the DNS server search order for all of the previously-found adapters
$adapters | ForEach-Object {$_.SetDNSServerSearchOrder($newDNSServers)}
#Flushing DNS
ipconfig /flushdns
#Registering DNS
ipconfig /registerdns
Set-DnsClientGlobalSetting -SuffixSearchList @("mydomain.local")
构建两个脚本并在 GPO 上使用 WMI 过滤 运行 一个在 W10 上,另一个在 W7 系统上。比尝试制作多态脚本更容易。
为什么不能在 Windows 7 台机器上 运行 Windows 10 powershell 脚本?
您正在使用 WMI 设置 DNS 地址,应该没有问题。
您可以尝试 运行在 windows 7 机器上本地安装它并分享错误吗?
如果您通过 GPO 使用 powershell,最好将错误写入日志文件。
即使是像这样基本的脚本末尾的东西
if($Error)
{
Export-Clixml -Path C:\GPOError.xml
}
这将保存整个错误对象
您可以将其导入回 powershell 并使用
查看
导入 Clixml
需要帮助创建一个可以与 Windows 7 (powershell 2.0) 和 Windows 10 (powershell 5.0) 一起使用的 powershell 脚本,以便可以通过 GPO 推送我。 *我们没有域 DHCP 是我们从 MPLS 路由器获得 IP 租约...
到目前为止,我已经使用 windows 7(非 powershell)
的批处理文件完成了以下操作@ECHO OFF
set vardns1=1.1.1.1
set vardns2=2.2.2.2
ECHO Setting Primary DNS
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%
ECHO Setting Secondary DNS
netsh int ip add dns name = "Local Area Connection" addr = %vardns2%
ECHO Flushing DNS
ipconfig /flushdns
ECHO Registering New DNS settings
ipconfig /registerdns
此代码用于 Windows 10 (powershell 5.0)
If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent
()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
Exit
}
#The servers that we want to use
$newDNSServers = "1.1.1.1","2.2.2.2"
# Get all network adapters that already have DNS servers set
$adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DNSServerSearchOrder -ne $null}
# Set the DNS server search order for all of the previously-found adapters
$adapters | ForEach-Object {$_.SetDNSServerSearchOrder($newDNSServers)}
#Flushing DNS
ipconfig /flushdns
#Registering DNS
ipconfig /registerdns
Set-DnsClientGlobalSetting -SuffixSearchList @("mydomain.local")
构建两个脚本并在 GPO 上使用 WMI 过滤 运行 一个在 W10 上,另一个在 W7 系统上。比尝试制作多态脚本更容易。
为什么不能在 Windows 7 台机器上 运行 Windows 10 powershell 脚本?
您正在使用 WMI 设置 DNS 地址,应该没有问题。
您可以尝试 运行在 windows 7 机器上本地安装它并分享错误吗?
如果您通过 GPO 使用 powershell,最好将错误写入日志文件。
即使是像这样基本的脚本末尾的东西
if($Error)
{
Export-Clixml -Path C:\GPOError.xml
}
这将保存整个错误对象 您可以将其导入回 powershell 并使用
查看导入 Clixml