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

Microsoft SQL: Shrink vs Truncate

Shrink The shrink command is to reduce the physical log file size of a database.…

PowerShell: Download and Expand Zip File – Legacy Compatible

function downloadFile{ param( $url, $tempFolder="C:\Temp" ) try{ $fileName=split-path $url -leaf $tempFile = "$tempFolder\$fileName" try{[Net.ServicePointManager]::SecurityProtocol =…

PowerShell: Delete Hidden System Volume Information Directory

# Incident:The System Volume Information has been copied over to an SMB share, where such…