Posted On January 26, 2023

How To Recover SQLSERVER Service from Being Unable to Start

kimconnect 0 comments
blog.KimConnect.com >> Codes , Database , Windows >> How To Recover SQLSERVER Service from Being Unable to Start
# startSqlService.ps1

# special provision to deal with SQL Server not starting up due to various reasons
$sqlServerService='mssqlserver'
$sqlWaitSeconds=120
$desiredState='Running'
$sqlWaitTimespan=[timespan]::fromseconds($sqlWaitSeconds)
if($sqlServerService -in $originalStoppedServices.Name){            	
    try{
    $sqlService=get-service $sqlServerService -EA Ignore
    if($sqlService.Status -ne $desiredState -and $null -ne $sqlService){
        $stopped=try{$sqlService.waitforstatus('Stopped',$sqlWaitTimespan);$true}catch{$false}
        if($stopped){
            $attempt=net start $sqlServerService /T902 2>&1
            $running=(get-service $sqlServerService).Status -eq 'Running'
            $running=if(!$running){try{$sqlService.waitforstatus('Running',$sqlWaitTimespan);$true}catch{$false}}else{$running}
            if($attempt -match 'started' -and $running){
                write-host "$sqlServerService has been started with trace flag /T902" -foregroundcolor Green
            }else{
                write-host "$sqlServerService has NOT been started with trace flag /T902" -foregroundcolor Red
            }
        }else{
            write-warning "$sqlServerService has current state is $((get-service $sqlServerService).Status)"
        }                 
    }                
    }catch{
    write-warning $_                               
    }            
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Invoke-Command to Add A Host Record on DNS Server

# DNS Host Record Information $aRecord="superman" $recordIP='192.168.0.256' $zoneName='kimconnect.com' $dnsServer='dc01.intranet.kimconnect.com' # Admin Credential $adminUsername='DoeManeAdmin' $adminPassword='WhatPassword?' $adminCredential=New-Object…

Windows: Create a Quick Hot Spot From A Wired Computer with Spare Wireless Adapter

# Start the Hot Spot netsh wlan set hostednetwork mode=allow ssid="$env:username hotspot" key=whatpassword netsh wlan…

Bash Shell: Old School Migration of ESXi Guest Virtual Machines

Step 0: Preparations Estimate file-copying duration for cut-over:- Sustained storage transfer speed using a 1GB…