Friday, 10 May 2013

PowerShell Script to Install SQL Server Service Pack(SQL2012/SQL2008)

The below Script helps to Install SQL Server Service Pack (SQL 2012/SQL2008) unattended using powershell, The same script can be modifed easily for earlier versions of SQL Servers,The only version specific information is that the script reads the install log in the bootstrap folder and prints it to the host after service pack is applied/failed and the bootstrap log path changes with each version and I have included only SQL 2012 and SQl 2008 path to the script.

 # Script to Install SQL Server Service Pack  
 # Created by - Vinoth N Manoharan  
 # Version 1.0  
 # Date - 24/04/2013  
 # Script Help :-  
 #---------------  
 # Parameter 1 :- "-e" to specify the path of Setup file for the SP Install  
 # Parameter 2 :- "-i" to Specify the Instance name to be updated or "ALL" to update all the instances on a Server  
 # Parameter 3 :- "-v" to Specify the version of SQL Server; This can take only values SQL2008 or SQL2012   
 # Example1:- SQLSPInstall.ps1 -e <Setup File Full UNC Path> -i <Intance_Name> -v <SQL2008/SQL2012>  
 # Example2:- SQLSPInstall.ps1 -e U:\SQLServer2008R2SP2-KB2630458-x64-ENU.exe -i All -v SQL2008  
 # Example3:- SQLSPInstall.ps1 -e U:\SQLServer2008R2SP2-KB2630458-x64-ENU.exe -i MSSQLSERVER -v SQL2012  
 # Example4:- SQLSPInstall.ps1 -e U:\SQLServer2008R2SP2-KB2630458-x64-ENU.exe -i INST1 -v SQL2012  


 Clear-Host   
 if($args.Length -ne 6)  
 {  
 Write-Host "Incorrect Paramenter Count use -e to the path of Setup file for the SP Install, use -a to specify the Instance Name or All Instances and use -v to specify SQL Version" -ForegroundColor Red  
 $uParameterHelp = "  
 Help:-  
 ******  
      # Parameter 1 :- -e to specify the path of Setup file for the SP Install  
      # Parameter 2 :- -i to Specify the Instance name to be updated or ALL to update all the instances on a Server   
      # Parameter 3 :- -v to Specify the version of SQL Server; This can take only values SQL2008 or SQL2012   
      # Example1:- SQLSPInstall.ps1 -e <Setup File Full UNC Path> -i <Intance_Name> -v <SQL2008/SQL2012>  
      # Example2:- SQLSPInstall.ps1 -e U:\SQLServer2008R2SP2-KB2630458-x64-ENU.exe -i All -v SQL2008  
      # Example3:- SQLSPInstall.ps1 -e U:\SQLServer2008R2SP2-KB2630458-x64-ENU.exe -i MSSQLSERVER -v SQL2012  
      # Example4:- SQLSPInstall.ps1 -e U:\SQLServer2008R2SP2-KB2630458-x64-ENU.exe -i INST1 -v SQL2012"  
 Write-Host $uParameterHelp -ForegroundColor Blue  
 }  
 elseif((($args[0] -eq "-e") -or ($args[0] -eq "-E")) -and (($args[2] -eq "-i") -or ($args[2] -eq "-I")) -and (($args[4] -eq "-v") -or ($args[4] -eq "-V")) -and (($args[5].ToUpper() -eq "SQL2008") -or ($args[5].ToUpper() -eq "SQL2012")) )  
 {  
 Write-Host "START: SQL SERVICE PACK INSTALL PROGRAM MAIN"  
 $uSQLSetupEXE = $args[1]  
 $instname = $args[3]  
 $inststr = $null  
 $uSQLVERSION = $args[5]  
 if(($instname -eq "ALL") -or ($instname -eq "all"))  
 {$inststr = "/allinstances"}else{$inststr = "/instancename="+$instname}  
 $uStartProcessArg = "/q /IAcceptSQLServerLicenseTerms /Action=Patch "+$inststr  
 [datetime]$unow = Get-Date  
 #SQL BootStrap Folder SQL Install Log File  
           if($uSQLVERSION -eq "SQL2012"){$uSQLBootStrapLog = "C:\Program Files\Microsoft SQL Server\110\Setup Bootstrap\Log\Summary.txt"}  
           elseif($uSQLVERSION -eq "SQL2008"){$uSQLBootStrapLog = "C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log\Summary.txt"}  
                Try  
                {  
                <#***************Execute SQL Server Install**********************#>  
                Write-Host "START[Exec SP]:$uSQLSetupEXE"  
                Start-Process $uSQLSetupEXE -ArgumentList $uStartProcessArg -Wait -NoNewWindow -ErrorAction Stop  
                #Print the console output  
                }  
                Catch  
                {  
                 Write-host -ForegroundColor Red "ERROR[Exec SP]:"$_.Exception.Message  
                 Write-Host "END: SQL SERVICE PACK INSTALL PROGRAM MAIN"  
                 EXIT;  
                }  
                Try  
                {  
                     #Print the SQL BootStrap Folder Log if there is new Log  
                     foreach ($uSQLBootStrapLog_readline in get-content $uSQLBootStrapLog -ErrorAction Stop)  
                     {  
                                                   if($uSQLBootStrapLog_readline -match "End time:*")  
                                                   {  
                                                         $uenddateVal = $uSQLBootStrapLog_readline.Split(":")  
                                                          [datetime]$ubootstrapdate = $uenddateVal[1].Trim()+":"+$uenddateVal[2].Trim()+":"+$uenddateVal[3].Trim()  
                                                   }  
                                                   if($uSQLBootStrapLog_readline -match "Requested action:*")  
                                                   {  
                                                        $ubootstrapstring = $ubootstrapstring + $uSQLBootStrapLog_readline+"`n`r"  
                                                        $ubootstrapstring_errorlog = $ubootstrapstring_errorlog + $uSQLBootStrapLog_readline+"`n`r`n`r"  
                                                        break  
                                                   }  
                                                   else  
                                                   {  
                                                        $ubootstrapstring = $ubootstrapstring + $uSQLBootStrapLog_readline+"`n`r"  
                                                        $ubootstrapstring_errorlog = $ubootstrapstring_errorlog + $uSQLBootStrapLog_readline+"`n`r`n`r"  
                                                   }  
                     }  
                     if($unow -lt $ubootstrapdate){Write-Host $ubootstrapstring}  
                }  
                catch  
                {  
                     Write-host -ForegroundColor Red "ERROR [Read Bootstrap Log]:"$_.Exception.Message  
                }  
                write-host -ForegroundColor DarkGreen "STATUS [Exec SP]:SQL Setup started Successfully, Check Module 4 Comments for Install Status!"  
                write-host -ForegroundColor DarkGreen "STATUS [Exec SP]:SQL Server Install Summary Log located @$uSQLBootStrapLog"  
                Write-Host "END: SQL SERVICE PACK INSTALL PROGRAM MAIN"  
 }else  
 {  
 Write-Host "Incorrect Paramenter use -e to the path of Setup file for the SP Install, use -a to specify the Instance Name or All Instances and use -v to specify SQL Version" -ForegroundColor Red  
 $uParameterHelp = "  
 Help:-  
 ******  
      # Parameter 1 :- -e to specify the path of Setup file for the SP Install  
      # Parameter 2 :- -i to Specify the Instance name to be updated or ALL to update all the instances on a Server   
      # Parameter 3 :- -v to Specify the version of SQL Server; This can take only values SQL2008 or SQL2012   
      # Example1:- SQLSPInstall.ps1 -e <Setup File Full UNC Path> -i <Intance_Name> -v <SQL2008/SQL2012>  
      # Example2:- SQLSPInstall.ps1 -e U:\SQLServer2008R2SP2-KB2630458-x64-ENU.exe -i All -v SQL2008  
      # Example3:- SQLSPInstall.ps1 -e U:\SQLServer2008R2SP2-KB2630458-x64-ENU.exe -i MSSQLSERVER -v SQL2012  
      # Example4:- SQLSPInstall.ps1 -e U:\SQLServer2008R2SP2-KB2630458-x64-ENU.exe -i INST1 -v SQL2012"  
 Write-Host $uParameterHelp -ForegroundColor Blue  
 }  
Copyright © 2013 Vinoth N Manoharan.The information provided in this post is provided "as is" with no implied warranties or guarantees.

3 comments:

  1. Hi Vinoth.
    Buenos días,

    SQL 2014 Service pack 2 fails with error:

    (01) 2016-11-05 12:51:37 Slp: Message:
    (01) 2016-11-05 12:51:37 Slp: El directorio de datos de usuario del Registro no es válido. Compruebe que la clave DefaultData bajo el subárbol de la instancia apunta a un directorio válido.
    (01) 2016-11-05 12:51:37 Slp: HResult : 0x851a0043
    (01) 2016-11-05 12:51:37 Slp: FacilityCode : 1306 (51a)
    (01) 2016-11-05 12:51:37 Slp: ErrorCode : 67 (0043)
    (01) 2016-11-05 12:51:37 Slp: Data:
    (01) 2016-11-05 12:51:37 Slp: SQL.Setup.FailureCategory = ConfigurationValidationFailure
    (01) 2016-11-05 12:51:37 Slp: WatsonConfigActionData = PATCH@VALIDATION@SQL_ENGINE_CORE_INST

    However, 'DefaulData' key does not exists and i've review all registry related to sql with no wrong path found.

    Se trata de un Windows Server 2012 R2 standard.
    ¿Any ideas?

    Tahnks in advance.

    ReplyDelete
  2. Sorry. My email: carlos@agpsoftware.com

    ReplyDelete