Can't Find Catch or Finally block.

Etan77 1 Reputation point
2021-03-03T19:53:19.427+00:00

Working on a script to change a password in a computer's registry. The problem is I keep getting a Can't Find Catch or Final block error
try. Its a really long script so I just put in the area that's is showing the error. I have the try and catch statements there but I still get the error. Can anyone give it a look and see where I have gone wrong?

try
{
$WinlogonPath = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"
$insecurePass=Get-ItemProperty -Path $WinlogonPath -Name "DefaultPassword" -ErrorAction SilentlyContinue

if(!$insecurePass){

New-ItemProperty -Path $WinlogonPath -Name "DefaultPassword" -PropertyType "string"

}

Per testing on Windows 2016, if the DefaultPassword Key exists, Autologin will not leverage the LSA Encrypted Password for authentication

if($insecurePass.DefaultPassword.Length -ne 0)
{
          throw 'Failed to set auto logon password. The error was: "{0}".' -f $_
   }
    Remove-ItemProperty -Path $WinlogonPath -Name "DefaultPassword" -Force
}
$decryptedPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
# Store the password securely.
$lsaUtil = New-Object ComputerSystem.LSAutil -ArgumentList "DefaultPassword"
$lsaUtil.SetSecret($decryptedPass)

}
Catch
{
throw 'Failed to set auto logon password. The error was: "{0}".' -f $_

}

}

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,449 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,671 Reputation points
    2021-03-03T20:22:48.29+00:00

    When posting code, please use the "Code Sample" icon (or Control+K) so the code is readable and to avoid the system from doing stupid things like removing the "_" after a "$" character. We'd all appreciate it!

    Your problem is that immediately after the line 'Remove-ItemProperty -Path $WinlogonPath -Name "DefaultPassword" -Force' you have a "}" that terminates the "Try" block, but then you have code between that "}" and the beginning of the "Catch" block. I'd guess that the incorrectly placed "}" should be removed and the intervening code should become part of the "Try" block.

    Using indentation in your code would make that pretty obvious, and using an editor (such as VS Code) that flags mismatched braces, parenthesis, quotes, etc. would make your coding a lot easier.

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 33,941 Reputation points Microsoft Vendor
    2021-03-04T03:37:02.323+00:00

    Hi,

    Are the lines between 'Remove-ItemProperty -Path $WinlogonPath -Name "DefaultPassword" -Force' and "Catch" part of the try block? If so, the excess curly bracket immediately following the line 'Remove-ItemProperty' should be removed. If not, they should be moved to somewhere else as no code can be between the end of the try block and the beginning of the catch block.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments