ExchangeAdminToolkit.psm1

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
Function Get-DeletedMailboxList{  
<#
.Synopsis
   Gets a list of Deleted mailboxes from exchange and outputs into to a CSV file as well as console
.DESCRIPTION
   Gets a list of Hard deleted mailboxes (Remove-Mailbox, Disable-Mailbox) from all databases in the exchange organisation
   It then outputs them both to the console and to a CSV file which defaults to c:\temp\DeletedMailboxes.csv
.EXAMPLE
   Get-DeletedMailboxList
.EXAMPLE
   Get-DeletecMailboxList -CSVPath C:\MyDir\File.csv -SupressConsole
.LINK
    http://www.f-e-a-r.co.uk
.NOTES
 Author: Daniel Caulfield - 2017
#>

    [CmdletBinding(SupportsShouldProcess=$True)]    
    [OutputType([int])]
    Param
    (
        # Path to CSV File
        [Parameter(Mandatory=$false,
                   Position=0)]
        $CSVPath = "$env:TEMP\DeletedMailboxes.csv",

        # Supress output to the console and just create CSV
        [switch]$SupressConsole
    )
Begin{
    Write-Verbose "SupressConsole = $SupressConsole, CSVPath = $CSVPath"
    }
Process{
    Write-Verbose "Querying all databases for all mailboxes and filtering for deleted"
    $DeletedMailboxes = Get-mailboxdatabase |Get-MailboxStatistics | Where-Object{$_.DisconnectReason -eq 'Disabled'} | Select-Object DisplayName,TotalItemSize,Database,MailboxGuid,DisconnectDate,DisconnectReason,LastLogonTime,LegacyDN
    Write-Verbose "Data collected, writing outputs"
    if ($SupressConsole -eq $False) {Write-Output $DeletedMailboxes}
    $DeletedMailboxes | ConvertTo-Csv | Out-File $CSVPath
}
End{
    Write-Verbose "Command Complete"
    }
}

Function Invoke-LoopCheckMoveRequest {
<#
.Synopsis
   Starts a loop checking mailbox move request statistics
.DESCRIPTION
   Starts a loop checking mailbox move request statistics for all moves or optionally only ones not completed
   Use CTRL+C to end the loop
.EXAMPLE
   Start-LoopCheckMoveRequest
.EXAMPLE
   Start-LoopCheckMoveRequest -ExcludeComplete -Interval 120
.LINK
    http://www.f-e-a-r.co.uk
.NOTES
 Author: Daniel Caulfield - 2017
#>

    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Excludes moves with a ststus of Completed
        [switch]$ExcludeComplete,
        #Interval in seconds to run the check
        [int]$Interval = '60'
    )
Begin{
    Write-Verbose "Starting Loop with an interval of $interval and ExcludeComplete $ExcludeComplete"
    Write-Output "Starting loop use CTRL+C to stop"
    $a = '1'
    }
Process{
    While ($a = 1){
        Write-Verbose 'Running Loop'
        If ($ExcludeComplete -eq $False) {Get-MoveRequest | Get-MoveRequestStatistics -OutVariable Results}
        If ($ExcludeComplete -eq $True) {Get-MoveRequest | Where-Object{$_.status -ne 'Completed'} | Get-MoveRequestStatistics -OutVariable Results}
        $Completed = ($Results | Where-Object {$_.status -eq 'Completed'}).count
        $Queued  = ($Results | Where-Object {$_.status -eq 'queued'}).count
        $AS = ($Results | Where-Object {$_.status -eq 'AutoSuspended'}).count
        $Rel = ($Results | Where-Object {($_.status -like 'relinquished*') -or ($_.status -like 'stalled*')}).count
        $failed = ($Results | Where-Object {$_.status -like 'Failed*'}).count
        $prog = ($Results | Where-Object {($_.status -eq 'InProgress') -or ($_.status -like 'copying*') -or ($_.status -like 'loading*') -or ($_.status -like 'creating*') -or ($_.status -like 'initial*')}).count
        Write-Output ""
        Write-Output "Completed : $Completed"
        Write-Output "In Progress : $Prog"
        Write-Output "Queued : $Queued"
        Write-Output "Suspended : $AS"
        Write-Output "Stalled : $Rel"
        Write-Output "Failed : $Failed"
        
        Write-Output "Next Check $Interval seconds..."
        Write-Verbose "Going to sleep for $Interval Seconds"
        Start-Sleep $interval
        }
    }
}

Function Invoke-LoopCheckRestoreRequest {
<#
.Synopsis
   Starts a loop checking mailbox Restore request statistics
.DESCRIPTION
   Starts a loop checking mailbox Restore request statistics for all moves or optionally only ones not completed (default 60 second interval)
   Use CTRL+C to end the loop
.EXAMPLE
   Start-LoopCheckRestoreRequest
.EXAMPLE
   Start-LoopCheckRestoreRequest -ExcludeComplete -Interval 120
.LINK
    http://www.f-e-a-r.co.uk
.NOTES
 Author: Daniel Caulfield - 2017
#>

    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Excludes moves with a ststus of Completed
        [switch]$ExcludeComplete,
        #Interval in seconds to run the check
        [int]$Interval = '60'
    )
Begin{
    Write-Verbose "Starting Loop with an interval of $interval and ExcludeComplete $ExcludeComplete"
    Write-Output "Starting loop use CTRL+C to stop"
    $a = '1'
    }
process{
    While ($a = 1){
        Write-Verbose 'Running Loop'
        If ($ExcludeComplete -eq $False) {Get-MailboxRestoreRequest | Get-MailboxRestoreRequestStatistics}
        If ($ExcludeComplete -eq $True) {Get-MailboxRestoreRequest | Where-Object{$_.status -ne 'Completed'} | Get-MailboxRestoreRequestStatistics}
        Write-Output "Next Check $Interval seconds..."
        Write-Verbose "Going to sleep for $Interval Seconds"
        Start-Sleep $interval
        }
    }
}

Function Add-EXPSSnapin {
<#
.Synopsis
   Adds the Exchange Management PowerShell Snapin
.DESCRIPTION
   Adds the Exchange Management PowerShell Snapin, This requires the management tools for Exchange 2010 (or later) to be installed.
.EXAMPLE
   Add-EXPSSnapin
#>

    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Skips checking that the PSSnapin loaded
        [Switch]$SkipConfirmation
    )
    Add-PSSnapin 'Microsoft.Exchange.Management.PowerShell.E2010'
    If ($SkipConfirmation -eq $False) {If ((Get-PSSnapin 'Microsoft.Exchange.Management.PowerShell.E2010').Name -eq 'Microsoft.Exchange.Management.PowerShell.E2010') {Write-Output 'PSSnapin loaded'} }
}

Function Invoke-LoopCheckMailboxRepairRequest {
<#
.Synopsis
   Starts a loop checking mailbox Repair request statistics
.DESCRIPTION
   Starts a loop checking mailbox Repair request statistics for all repairs or optionally only ones not completed (default 60 second interval)
   Use CTRL+C to end the loop
.EXAMPLE
   Start-LoopCheckMailboxRepairRequest
.EXAMPLE
   Start-LoopCheckMailboxRepairRequest -ExcludeComplete -Interval 120
.LINK
    http://www.f-e-a-r.co.uk
.NOTES
 Author: Daniel Caulfield - 2017
#>

    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Excludes moves with a ststus of Completed
        [switch]$ExcludeComplete,
        #Interval in seconds to run the check
        [int]$Interval = '60'
    )
Begin{
    Write-Verbose "Starting Loop with an interval of $interval and ExcludeComplete $ExcludeComplete"
    Write-Output "Starting loop use CTRL+C to stop"
    $a = '1'
    }
process{
    While ($a = 1){
        Write-Verbose 'Running Loop'
        If ($ExcludeComplete -eq $False) {Get-MailboxRpairRequest}
        If ($ExcludeComplete -eq $True) {Get-MailboxRepairRequest | Where-Object{$_.status -ne 'Completed'}}
        Write-Output "Next Check $Interval seconds..."
        Write-Verbose "Going to sleep for $Interval Seconds"
        Start-Sleep $interval
        }
    }
}