Skip to content

Instantly share code, notes, and snippets.

@JohanSelmosson
Created January 12, 2022 11:12
Show Gist options
  • Save JohanSelmosson/461c484a92eab24805b1e44dcb63e9b3 to your computer and use it in GitHub Desktop.
Save JohanSelmosson/461c484a92eab24805b1e44dcb63e9b3 to your computer and use it in GitHub Desktop.
#WIP in progress as of 2022-01-12
function Start-ExchangeMaintenance {
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[validateNotNullOrEmpty()]
[String]$MaintenanceServer,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$failoverServerFQDN
)
begin {
function Test-ElevationStatus {
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
{ Write-Output $true }
else
{ Write-Output $false }
}
if ((Test-ElevationStatus) -ne $true) {
Write-Warning "Not admin, exiting.."
break
}
$failoverServer = ($failoverServerFQDN).split('.')[0]
$clusterstatus = get-clusternode
if (($clusterstatus | where-object {$_.Name -eq $failoverserver}).State -ne "Up" ) {
Write-Warning "Abort, $failoverserver is paused as a cluster node. Are you sure it´s not in maintenance mode?"
$clusterstatus
Return
}
}
process {
Set-ServerComponentState $MaintenanceServer -Component HubTransport -State Draining -Requester Maintenance
Restart-Service MSExchangeTransport
Push-Location $exscripts
.\StartDagServerMaintenance.ps1 -ServerName $MaintenanceServer -MoveComment Maintenance -PauseClusterNode
Pop-Location
Redirect-Message -Server $MaintenanceServer -Target $FailoverServerFQDN
Set-ServerComponentState $MaintenanceServer -Component ServerWideOffline -State Inactive -Requester Maintenance
#To verify the server has been placed into maintenance mode, confirm that only Monitoring and RecoveryActionsEnabled are in an Active state
Write-Host "Only Monitoring and ReRecoveryActionsEnabled should be active"
Get-ServerComponentState $MaintenanceServer | Format-Table Component,State -Autosize
#To verify the server is not hosting any active database copies, status skall vara blocked
Write-Host "Below status should be Blocked"
Get-MailboxServer $MaintenanceServer | Format-List DatabaseCopyAutoActivationPolicy
#To verify that the failover server can activate the mailboxdatabases, status skall vara Unrestricted
Write-Host "The Failover servers status should be Unrestricted"
Get-MailboxServer $FailoverServer | Format-List DatabaseCopyAutoActivationPolicy
}
end {
}
}
function Stop-ExchangeMaintenanceMode {
[CmdletBinding()]
param (
[parameter(Mandatory = $true)]
[validateNotNullOrEmpty()]
[String]$ServerName
)
begin {
function Test-ElevationStatus {
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
{ Write-Output $true }
else
{ Write-Output $false }
}
if ((Test-ElevationStatus) -ne $true) {
Write-Warning "Not admin, exiting.."
break
}
}
process {
#configure the server as out of maintenance mode and ready to accept client connections
Set-ServerComponentState $ServerName -Component ServerWideOffline -State Active -Requester Maintenance
#To execute the StopDagServerMaintenance.ps1 script
Push-Location $ExScripts
.\StopDagServerMaintenance.ps1 -serverName $ServerName
Pop-Location
#enable the transport queues to resume accepting and processing messages
Set-ServerComponentState $ServerName -Component HubTransport -State Active -Requester Maintenance
#resume transport activity
#Invoke-command används om du inte jobbar lokalt på $servername
#Invoke-Command -ComputerName $servername -ScriptBlock {Restart-Service MSExchangeTransport}
#Kör kommandot direkt om du är inloggad på $servername lokalt
Restart-Service MSExchangeTransport
#verify the server is not in maintenance mode, ForwardSyncDaemon och ProvisioningRps kan vara Inactive
Write-Host "Only ForwardSyncDaemon and ProvisioningRps should be Inactive"
Get-ServerComponentState $ServerName | Format-Table Component,State -Autosize
#Balansera om aktiveringspreferens (endast om nya databaser eller servrar lagts till)
#.\RedistributeActiveDatabases.ps1 -DagName EX2019DAG01 -BalanceActivationPreferences -Confirm:$false
#Aktivera databaser på respektive server enligt Aktiveringspreferens
Push-Location $ExScripts
.\RedistributeActiveDatabases.ps1 -DagName EX2019DAG01 -BalanceDbsByActivationPreference -Confirm:$False
Pop-Location
}
end {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment