Thursday, 28 March 2019

Enable AlwaysOn Feature using powershell



I have noticed many might be enabling or disabling SQLServer Alwayson using GUI which is time-consuming and also tedious. Once you have failover cluster manager setup on a collection of the node. You can use the below commands to enable Alwayson feature and restart SQL server on all the instance which are part of the failover clustering. 

Default Instance 

$Nodes  =('ULDB01')
$InstanceName='MSSQLSERVER'
foreach($node in $Nodes ){
Enable-SqlAlwaysOn -Path "SQLSERVER:\SQL\$node\DEFAULT" -Force
Get-service "$InstanceName" -Computername $node | stop-service -passthru -force
Get-service "$InstanceName" -Computername $node | start-service -passthru
Get-service "SQLSERVERAGENT" -Computername $node | start-service -passthru
}

Named Instance

$Nodes  =('ULDB01')
$InstanceName='MSSQL'
$NamedInstance='$MON'
foreach($node in $Nodes ){
Enable-SqlAlwaysOn -Path "SQLSERVER:\SQL\$node\DEFAULT" -Force
Get-service "$InstanceName$NamedInstance" -Computername $node | stop-service -passthru -force 
Get-service "$InstanceName$NamedInstance" -Computername $node | start-service -passthru
Get-service "SQLAGENT$NamedInstance" -Computername $node | start-service -passthru
}