Updating specific registry entry using Powershell and Try/Catch?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Updating specific registry entry using Powershell and Try/Catch?

Post by ITEngineer »

Hi People,

I need some assistance to modify the rough script below to:
  • Check the registry value exist or not before changing.
    update specific Registry key.
    Restart DNS service only.
    Test DNS functionality using builtin Powershell and then exit script when all is good no error.

Code: Select all

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
                Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\DNS\Parameters" -Name "TcpReceivePacketSize" -Type DWord -Value 255
        # 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)"
The script above cause the below error:

Exception Type: System.Management.Automation.PSArgumentException
Exception Message: Property TcpReceivePacketSize does not exist at path HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters.

This is as per this article: https://support.microsoft.com/en-us/hel ... nerability

Thank you in advance.
Last edited by ITEngineer on Fri Jul 17, 2020 5:10 am, edited 1 time in total.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Updating specific registry entry using Powershell and Try/Catch?

Post by jvierra »

Add "-ErrorAction SilentlyContinue"
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Updating specific registry entry using Powershell and Try/Catch?

Post by ITEngineer »

jvierra wrote: Fri Jul 17, 2020 4:27 am Add "-ErrorAction SilentlyContinue"
On which line?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Updating specific registry entry using Powershell and Try/Catch?

Post by jvierra »

Which line is causing the error?
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Updating specific registry entry using Powershell and Try/Catch?

Post by ITEngineer »

jvierra wrote: Fri Jul 17, 2020 5:01 am Which line is causing the error?
Somehow it doesn't mention specifically.

Do you mean in this line where the action is?
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\DNS\Parameters" -Name "TcpReceivePacketSize" -Type DWord -Value 255
/* IT Engineer */
weslein
Posts: 7
Last visit: Wed Jul 29, 2020 7:22 pm

Re: Updating specific registry entry using Powershell and Try/Catch?

Post by weslein »

jvierra wrote: Fri Jul 17, 2020 5:01 am Which line is causing the error?
Pretty cool and awesome.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Updating specific registry entry using Powershell and Try/Catch?

Post by ITEngineer »

weslein wrote: Fri Jul 17, 2020 7:04 pm
jvierra wrote: Fri Jul 17, 2020 5:01 am Which line is causing the error?
Pretty cool and awesome.
yes, it is :-)
Powershell is interesting.
/* IT Engineer */
This topic is 3 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked