Search code examples
sql-serverpowershellpowershell-4.0

Error catching mechanism in powershell


Just a broad question here - is there a generic error catching mechanism in powershell? I'm having issue where by connection to MSSSQL server times out randomly via a powershell script and then re-running it would be fine.

Just want to know if there is any try-catch or similar error capturing in powershell available. Or if any one has better solution to catch connection timeouts let me know.

Thank you. Zulfiqar


Solution

  • Yes, you could use Try-Catch block. Here is an example:

    Write-Host "Disabling IP v6 - Reboot required after installation/update!" 
    $breboot = $True
    try
    {
        New-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters\" -Name "DisabledComponents" -Value 0xffffffff -PropertyType "DWord" -ErrorAction Stop
    }
    catch
    {
        Write-Host ("IPv6 already disabled - no reboot required!")
        $breboot = $False
    }