Failover forzato

Microsoft.SQLServer.2012.AlwaysOn.ForcedFailoverForAvailabilityReplica (ConsoleTask)

Aprire la console SQLPS ed eseguire il failover nella replica di disponibilità di destinazione in modo che quest'ultima diventi la nuova replica primaria del gruppo di disponibilità. In questa attività viene usato il parametro AllowDataLoss.

Element properties:

TargetMicrosoft.SQLServer.2012.AlwaysOn.AvailabilityReplica
Applicationpowershell.exe
WorkingDirectory
isRequireOutputFalse
AccessibilityInternal
EnabledTrue

Source Code:

<ConsoleTask ID="Microsoft.SQLServer.2012.AlwaysOn.ForcedFailoverForAvailabilityReplica" Accessibility="Internal" Target="AlwaysOnDisc!Microsoft.SQLServer.2012.AlwaysOn.AvailabilityReplica" RequireOutput="false" Category="MonitoringObject">
<Application>powershell.exe</Application>
<Parameters>
<Parameter>-Version 2 -NoExit -NoLogo -NoProfile -Command Invoke-Command {$l = 0;$ag='$Target/Property[Type="AlwaysOnDisc!Microsoft.SQLServer.2012.AlwaysOn.AvailabilityReplica"]/AvailabilityGroupName$';
$l = 0;$serverName = '$Target/Property[Type="System!System.Entity"]/DisplayName$';
$l = 0;$dbReplicaName = ([regex]::Replace('$Target/Property[Type="System!System.Entity"]/DisplayName$', '\\', '%5C'));
#AlwaysOnCommon.ps1

#
# Get FQDN of machine in current domain as we assume that all replicas in the same domain
# If IP address is provided the host name is resolved via DNS
#
function Get-FQDN {
param ([string]$name = $(throw "Name is required parameter."))

# return FQDN
return [System.Net.Dns]::GetHostByName($name).HostName
}

function ImportModuleActionSqlModules($arAssemblies){
$paths = @(
(get-itemproperty -path "hklm:\Software\Microsoft\Microsoft SQL Server\110\Tools\ClientSetup\" | Select SQLPath).SQLPath ,
(get-itemproperty -path "hklm:\Software\Wow6432Node\Microsoft\Microsoft SQL Server\110\Tools\ClientSetup\" | Select SQLPath).SQLPath )

if (!(ImportSQLModules $arAssemblies $paths "110\Tools")) {
Import-Module SQLPS -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null
if(!(IsModuleLoaded "SQLPS"))
{
throw "Unable to load SQLPS modules"
}
}
}

function IsModuleLoaded([string]$include) {
$modules = Get-Module
foreach($module in $modules){
if($module.Name.Contains($include)) {
return $true
}
}
return $false
}

function ImportSQLModules {
param (
$arAssemblies,
$paths,
[string]$include)

$flRet = $false
foreach($path in $paths){
if($path.Contains($include)){
foreach($assemblyName in $arAssemblies){
$filename = $path + $assemblyName
if(Test-Path $filename){
Import-Module $filename -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null
$flRet = $true;
}
}
}
}
return $flRet
}

#AlwaysOnTaskCommon.ps1

#Depends on:
# AlwaysOnCommon.ps1

$arAssemblies = @(,'\PowerShell\Modules\SQLPS\SQLPS.psd1');
ImportModuleActionSqlModules $arAssemblies

function p{
param($v)
if ($v -ilike '*\*') { return $v } else { return '{0}\DEFAULT' -f $v }
}


$rp = ('{0}' -f (p($serverName)));
cd ('sqlserver:\SQL\{0}\AvailabilityGroups\{1}\AvailabilityReplicas\{2}\' -f $rp, $ag, $dbReplicaName);
$avg = (Get-Item ('..\..\..\{0}' -f $ag));
try{ Switch-SqlAvailabilityGroup -InputObject $avg -AllowDataLoss -Confirm; }
catch { Write-Host -ForegroundColor Red $_.Exception.Message; }
Write-Host "Press any key to exit...";
$key=[Console]::ReadKey($true);}</Parameter>
</Parameters>
<WorkingDirectory/>
</ConsoleTask>