Friday 10 May 2013

Managing Cluster from Command Line move to Powershell cmdlets in Windows 2012


Recently I was having issues with installation of one of our SQL server 2012 on a Windows 2012 cluster, in order to trouble shoot the issue I tried to query the cluster from

Command line using “cluster res”  which error out as below,

C:\Users\ManoharV>cluster res
'cluster' is not recognized as an internal or external command,
operable program or batch file.

On further analysis in MSDN I found out that Managing Cluster from Command Line has been moved to PowerShell from Windows 2012 and can be done only through Powershell cmdlets,

So I used “get-clusterresouce” cmdlet to get the result, similarly all the actions done using cluster.exe can be done using PS cmdlet refer http://technet.microsoft.com/en-us/library/ee461009.aspx for all cmdlets related to cluster.


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.