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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
| # 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 } |