Invoke-magdevExchangeMW.ps1

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<#PSScriptInfo
.DESCRIPTION
    Allows Interactive Patching of Exchange during maintenance windows (WSUS/SCCM)
.VERSION
    1.0
.GUID
    6e52afd9-3a79-48ed-97a5-8c6a0a804eae
.AUTHOR
    Ken Maglio @kenmaglio
.TAGS
    2013
.RELEASENOTES
    Intial Release.
#>



Add-PSSnapin *exchange* -ErrorAction SilentlyContinue

$WhatIf = $false # you can set to $true and this will not take effect


function Get-Answer {

    param(
        $caption = "Proceed?",
        $message = "Please see prior output to decide!",
        $option1 = "Proceed",
        $option2 = "Stop"
    )
    
    $opt1 = new-Object System.Management.Automation.Host.ChoiceDescription "&$option1","$option1";
    $opt2 = new-Object System.Management.Automation.Host.ChoiceDescription "&$option2","$option2";
    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($opt1,$opt2);
    $answer = $host.ui.PromptForChoice($caption,$message,$choices,0)

    switch ($answer){
        0 { return $true  }
        1 { return $false }
        default { return $false }
    }    
}

function Get-MailStoreHealth {
    param (
        $copyQueueLengthMax = 100
    )
    $allStores = Get-MailboxDatabaseCopyStatus *
    $healthy = 0
    $mounted = 0
    $other = 0
    $copyQueueLengthCurrent = 0

    $allStores | ForEach-Object {
        switch ($_.Status) {
            "Mounted" { $mounted++ }
            "Healthy" { $healthy++ }
            default { $other++ }
        }

        if ($_.CopyQueueLength -gt $copyQueueLengthCurrent) {
            $copyQueueLengthCurrent =  $_.CopyQueueLength
        }
    }

    Write-Host "Current Status: " -ForegroundColor White
    Write-Host "Mounted: $mounted" -ForegroundColor Magenta
    Write-Host "Healthy: $healthy" -ForegroundColor Green
    Write-Host "Other: $other" -ForegroundColor Red
    Write-Host ""
    Write-Host "Max Copy Queue Length: $copyQueueLengthCurrent" -ForegroundColor Cyan


    if ($other -gt 0) {
        Write-Host "*********************************************"  -ForegroundColor Red
        Write-Host "*********************************************"  -ForegroundColor Red
        Write-Host "***** MailStores Not Healthy ******"  -ForegroundColor Red
        Write-Host "*********************************************"  -ForegroundColor Red
        Write-Host "*********************************************"  -ForegroundColor Red
        return $false
    } elseif ($copyQueueLengthCurrent -gt $copyQueueLengthMax) {
        Write-Host "*********************************************"  -ForegroundColor Red
        Write-Host "*********************************************"  -ForegroundColor Red
        Write-Host "***** Copy Queue Length > $copyQueueLengthMax ******"  -ForegroundColor Red
        Write-Host "*********************************************"  -ForegroundColor Red
        Write-Host "*********************************************"  -ForegroundColor Red
        return $false
    } else {
        return $true
    }

}

function Restart-ComputerAndWaitForExchange {
    param(
        $server,
        $WhatIf,
        $waitMinutes = 2
    )

    #reboot server
    if(Get-Answer -message "Do you want to reboot $($server)?" -option1 "Yes" -option2 "No") {
        Write-Host "Issuing Reboot to Server and Waiting for the reboot..." -ForegroundColor Yellow
        Restart-Computer $server -Force -Confirm:$false -Wait -WhatIf:$WhatIf 
        #wait for exchange to be healthy
            
    
        Write-Host "Waiting $waitMinutes minutes for exchange health ..."
        if (-not($WhatIf) ) {
            $Time = [System.Diagnostics.Stopwatch]::StartNew()
            while($Time.Elapsed.Minutes -lt 5) {
                Write-Host "Elapsed: $($Time.Elapsed.Minutes)min."
                $success = Get-MailStoreHealth 
                if ($success) { break; }
                Start-Sleep -Seconds 30                
            }
            $Time.Stop()


            if ($Time.Elapsed.Minutes -ge $waitMinutes) {
                Write-Host "Time Elapsed is > $($waitMinutes)min. and we still have bad Exchange Health"

                Write-Host "*********************************************"  -ForegroundColor Red
                Write-Host "*********************************************"  -ForegroundColor Red
                Write-Host "***** BAD EXCHANGE HEALTH ******"  -ForegroundColor Red
                Write-Host "*********************************************"  -ForegroundColor Red
                Write-Host "*********************************************"  -ForegroundColor Red

                return $false
            }

            Write-Host "Server Reboot and Wait complete!" -ForegroundColor Green

            return $true

        } else {
            Write-Host "---WhatIf -- not waiting"
            return $true
        }        
    }
}


#####################################################################################################################
#####################################################################################################################
#####################################################################################################################
#####################################################################################################################

Get-MailStoreHealth | Out-Null

if (Get-Answer -message "Is Exchange Healthy Enough To Proceed?" -option1 "Yes" -option2 "NO! STOP THE PRESSES!") {
    $allExServers = Get-ExchangeServer
    ForEach($server in $allExServers) {
        Write-Host ("Server: $($server.Name)")        

        $mountedStores = Get-MailboxDatabaseCopyStatus -Server $server.id  | Where {$_.Status -eq 'Mounted'}

        $activationsDone = 0
        if ($mountedStores.Count -gt 0) {  
        
            if(Get-Answer -message "There are active copies on $($server.Name)... Proceed?" -option2 "Skip") {
                                                      
                foreach($mountedStore in $mountedStores) {                
                    $mailStoreName = $mountedStore.Name.Split("\")[0]
                    $mountedServer = $mountedStore.Name.Split("\")[1]
                    Write-Host "Mailstore $mailStoreName is on $($server.Name)" -ForegroundColor Green                
                    $allCopies = Get-MailboxDatabaseCopyStatus $mailStoreName | Where {$_.Name -notlike "*$($mountedServer)"}
                    $moveToServer = $allCopies[0].Name.Split("\")[1]
                    Write-Host "Intent is to Activate on $moveToServer" -ForegroundColor Yellow
                
                    if(Get-Answer -message "Activate $mailStoreName on $($moveToServer)?" -option2 "Skip") {
                        Write-Host "Activating Mailstore..."
                        try {
                            Move-ActiveMailboxDatabase $mailStoreName -ActivateOnServer $moveToServer -WhatIf:$WhatIf -Confirm:$false
                            Write-Host "Activating Mailstore Complete!!!" -ForegroundColor Green
                        } catch {
                            Write-Host "ERROR!!!" -ForegroundColor Red
                            break;
                        }

                        $activationsDone++

                    } else {
                        Write-Host " --- Not Moving cause you said not to..."
                    }
                }       
                Write-Host "Mounted Stores: $($mountedStores.Count) -- 'Move' Activations Completed: $activationsDone"

                $rebootAndHealthy = Restart-ComputerAndWaitForExchange -server $server -WhatIf $WhatIf     
                if (-not($rebootAndHealthy)) {
                    Write-Host "Stopping Script"
                    break;
                }       
            } else {
                Write-Host "Skipping $($server.Name)... " -ForegroundColor Yellow
            }
        } else {
            Write-Host "There are no mailstores active on $($server.Name)..." -ForegroundColor Yellow

            Restart-ComputerAndWaitForExchange -server $server -WhatIf $WhatIf            
        }                
    }

    if(Get-Answer -message "Rebalance?" -option1 "Yup! Do it!" -option2 "No - leave it alone") {
        $scriptPath = "C:\Program Files\Microsoft\Exchange Server\V15\Scripts\RedistributeActiveDatabases.ps1"
        
        $dag = Get-DatabaseAvailabilityGroup

        $argumentList = @()
        $argumentList += ("-DagName", "$($dag.Name)")
        $argumentList += ("-BalanceDbsByActivationPreference")        
        $argumentList += ("-Confirm:`$false")
        $argumentList += ("-WhatIf:`$$($WhatIf.ToString().ToLower())")

        Write-Host "Attempting to invoke-expression with: `"$scriptPath`" $argumentList"
        Invoke-Expression "& `"$scriptPath`" $argumentList"

    }
}