使用 Powershell 和 Try/Catch 更新特定注册表项?
Updating specific registry entry using Powershell and Try/Catch?
我需要一些帮助来将下面的粗略脚本修改为:
- 更改前检查注册表值是否存在。
- 更新特定的注册表项。
- 只重启 DNS 服务。
- 使用内置 Powershell 测试 DNS 功能,然后在一切正常且没有错误时退出脚本。
脚本:https://pastebin.com/jm34bssi
Import-Module DnsServer
try
{
$RegistryPath = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters'
$ipV4 = Test-Connection -ComputerName $env:COMPUTERNAME -Count 1 | Select IPV4Address
#Test & check the DNS value if it is changed already or not?
If ( (Get-ItemProperty -Path $RegistryPath -Name 'TcpReceivePacketSize').ToString() -ne 0xFF00 )
{
# Update the below Registry key value:
Subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters
Value: TcpReceivePacketSize
Type: DWORD
Value data: 0xFF00
# Restart the DNS service after the succesful change
net stop dns
net start dns
Get-ItemProperty -Path $RegistryPath | Format-List
#Test the DNS server functionality, if no errors, generated from the below test, then all is good, exit script.
try
{
$testConnection = Test-Connection $domaincontroller -Count 1
If (($testConnection -ne "") -or ($testconnection -ne $null))
{
Test-DnsServer -IPAddress $ipV4
Test-DnsServer -IPAddress $ipV4 -Context Forwarder
Test-DnsServer -IPAddress $ipV4 -Context RootHints
Test-DnsServer -IPAddress $ipV4 -ZoneName $env:USERDOMAIN
}
else
{
Write-Host "$computername DNS test failed".
Exit
}
}
catch
{
Write-Output "Exception Type: $($_.Exception.GetType().FullName)"
Write-Output "Exception Message: $($_.Exception.Message)"
}
}
else
{
Write-Host "$computername DNS has been updated"
}
}
catch
{
Write-Output "Exception Type: $($_.Exception.GetType().FullName)"
Write-Output "Exception Message: $($_.Exception.Message)"
}
上面的脚本导致以下错误:
Exception Type: System.Management.Automation.PSArgumentException
Exception Message: Property TcpReceivePacketSize does not exist at path HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters.
要使用 PowerShell cmdlet 获取或设置注册表项,您需要使用 PowerShell 驱动器名称 HKLM:
HKLM:\SYSTEM\CurrentControlSet\Services\DNS\Parameters
或在长配置单元名称前添加提供者 Registry::
(双冒号):
Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters
见Note
$RegistryPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\DNS\Parameters'
# try and get the property 'TcpReceivePacketSize'
$property = Get-ItemProperty -Path $RegistryPath -Name 'TcpReceivePacketSize' -ErrorAction SilentlyContinue
if (!$property) {
Write-Host "Creating new property 'TcpReceivePacketSize'"
# create the registry path if not already exists
if (!(Test-Path -Path $RegistryPath)) {
$null = New-Item -Path $RegistryPath -Force
}
# set the property TcpReceivePacketSize to the wanted value
$null = New-ItemProperty -Path $RegistryPath -Name 'TcpReceivePacketSize' -Value 0xFF00 -Type DWORD
}
elseif ([int]$property.TcpReceivePacketSize -ne 0xFF00) {
# the property was found, but the value needs to be set to 0xFF00
Write-Host "Updating property 'TcpReceivePacketSize'"
Set-ItemProperty -Path $RegistryPath -Name 'TcpReceivePacketSize' -Value 0xFF00 -Type DWORD
}
# do the rest of your Dns testing here
我需要一些帮助来将下面的粗略脚本修改为:
- 更改前检查注册表值是否存在。
- 更新特定的注册表项。
- 只重启 DNS 服务。
- 使用内置 Powershell 测试 DNS 功能,然后在一切正常且没有错误时退出脚本。
脚本:https://pastebin.com/jm34bssi
Import-Module DnsServer
try
{
$RegistryPath = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters'
$ipV4 = Test-Connection -ComputerName $env:COMPUTERNAME -Count 1 | Select IPV4Address
#Test & check the DNS value if it is changed already or not?
If ( (Get-ItemProperty -Path $RegistryPath -Name 'TcpReceivePacketSize').ToString() -ne 0xFF00 )
{
# Update the below Registry key value:
Subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters
Value: TcpReceivePacketSize
Type: DWORD
Value data: 0xFF00
# Restart the DNS service after the succesful change
net stop dns
net start dns
Get-ItemProperty -Path $RegistryPath | Format-List
#Test the DNS server functionality, if no errors, generated from the below test, then all is good, exit script.
try
{
$testConnection = Test-Connection $domaincontroller -Count 1
If (($testConnection -ne "") -or ($testconnection -ne $null))
{
Test-DnsServer -IPAddress $ipV4
Test-DnsServer -IPAddress $ipV4 -Context Forwarder
Test-DnsServer -IPAddress $ipV4 -Context RootHints
Test-DnsServer -IPAddress $ipV4 -ZoneName $env:USERDOMAIN
}
else
{
Write-Host "$computername DNS test failed".
Exit
}
}
catch
{
Write-Output "Exception Type: $($_.Exception.GetType().FullName)"
Write-Output "Exception Message: $($_.Exception.Message)"
}
}
else
{
Write-Host "$computername DNS has been updated"
}
}
catch
{
Write-Output "Exception Type: $($_.Exception.GetType().FullName)"
Write-Output "Exception Message: $($_.Exception.Message)"
}
上面的脚本导致以下错误:
Exception Type: System.Management.Automation.PSArgumentException
Exception Message: Property TcpReceivePacketSize does not exist at path HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters.
要使用 PowerShell cmdlet 获取或设置注册表项,您需要使用 PowerShell 驱动器名称 HKLM:
HKLM:\SYSTEM\CurrentControlSet\Services\DNS\Parameters
或在长配置单元名称前添加提供者 Registry::
(双冒号):
Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters
见Note
$RegistryPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\DNS\Parameters'
# try and get the property 'TcpReceivePacketSize'
$property = Get-ItemProperty -Path $RegistryPath -Name 'TcpReceivePacketSize' -ErrorAction SilentlyContinue
if (!$property) {
Write-Host "Creating new property 'TcpReceivePacketSize'"
# create the registry path if not already exists
if (!(Test-Path -Path $RegistryPath)) {
$null = New-Item -Path $RegistryPath -Force
}
# set the property TcpReceivePacketSize to the wanted value
$null = New-ItemProperty -Path $RegistryPath -Name 'TcpReceivePacketSize' -Value 0xFF00 -Type DWORD
}
elseif ([int]$property.TcpReceivePacketSize -ne 0xFF00) {
# the property was found, but the value needs to be set to 0xFF00
Write-Host "Updating property 'TcpReceivePacketSize'"
Set-ItemProperty -Path $RegistryPath -Name 'TcpReceivePacketSize' -Value 0xFF00 -Type DWORD
}
# do the rest of your Dns testing here