Domain_Association_Search.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<#PSScriptInfo
.VERSION 1.0
.GUID 9ce7ea87-fdbd-40c0-ba64-69de0bebcaaf
.AUTHOR Nuno Tavares
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>


<#
.DESCRIPTION
 Search for any object in the tenant associated to a specific vanity domain.
#>
 
Param()


#DISCLAIMER:
#THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED “AS IS” WITHOUT
#WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
#LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
#FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
#RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.


#----------------------------------------------
#region Import Assemblies
#----------------------------------------------
[void][Reflection.Assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][Reflection.Assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][Reflection.Assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][Reflection.Assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#endregion Import Assemblies

#Define a Param block to use custom parameters in the project
#Param ($CustomParameter)

function Main {

    Param ([String]$Commandline)

    if((Show-MainForm_psf) -eq 'OK')
    {
        
    }
    
    $script:ExitCode = 0 #Set the exit code for the Packager
}



function Show-MainForm_psf
{

    #----------------------------------------------
    #region Import the Assemblies
    #----------------------------------------------
    [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    #endregion Import Assemblies

    #----------------------------------------------
    #region Generated Form Objects
    #----------------------------------------------
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form1 = New-Object 'System.Windows.Forms.Form'
    $textbox1 = New-Object 'System.Windows.Forms.TextBox'
    $listbox3 = New-Object 'System.Windows.Forms.ListBox'
    $listbox2 = New-Object 'System.Windows.Forms.ListBox'
    $listbox1 = New-Object 'System.Windows.Forms.ListBox'
    $labelAZUREApps = New-Object 'System.Windows.Forms.Label'
    $labelEXOMailboxesMailUser = New-Object 'System.Windows.Forms.Label'
    $labelMSODSUsersGroupsCont = New-Object 'System.Windows.Forms.Label'
    $labelDomainAssociationSea = New-Object 'System.Windows.Forms.Label'
    $labelDomainName = New-Object 'System.Windows.Forms.Label'
    $buttonSearch = New-Object 'System.Windows.Forms.Button'
    $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    #endregion Generated Form Objects


    $form1_Load = {
    #Connections
    $Module = Get-Module

    $ModTMP="| "
    foreach ($Mod in $Module){$ModTMP += $Mod.Name +" | "}

    if($ModTMP.IndexOf("| MSOnline |") -gt -1) {
    Import-Module Msonline
    Write-Host "------------- Import MSOnline -------------" -fore cyan
    }
    else { 
    Install-Module msonline -Force
    Import-Module Msonline
    Write-Host "------------- Install MSOnline -------------" -fore cyan
    }

    if($ModTMP.IndexOf("| AzureAD |") -gt -1)  {
    Import-Module azuread
     Write-Host "------------- Import AzureAD -------------" -fore cyan
     
    }
    else { 
    Install-Module azuread -Force
    Import-Module azuread
    Write-Host "------------- Install AzureAD -------------" -fore cyan
    }

        $livecred = Get-Credential
        Connect-MsolService -Credential $livecred
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powerShell-liveID?serializationLevel=Full -Credential $LiveCred -Authentication Basic –AllowRedirection
        Import-PSSession $Session -AllowClobber
        Connect-AzureAD -Credential $livecred
    }

    
    $labelDomainAssociationSea_Click={
        
    }
    
    $labelDomainName_Click={
        
    }
    
    $textbox1_TextChanged={
        
    }
    
    $buttonSearch_Click = {
        #Clear the Boxes
        $listbox1.Items.Clear()
        $listbox2.Items.Clear()
        $listbox3.Items.Clear()
        
        $form1.Cursor = 'WaitCursor'
        
        #Run MSODS
        $listbox1.Items.Add("Finding active Users...")
        $msods = Get-Msoluser -all | Where-Object { $_.UserPrincipalName -match $textbox1.text }
        $msods2 = Get-MsolUser -all | Where-Object { $_.ProxyAddresses -match $textbox1.text }
        $msods3 = Get-MsolUser -all | Where-Object { $_.WindowsLiveId -match $textbox1.text }
        if ($msods -ne $null) { $listbox1.Items.AddRange($msods.Displayname) }
        if ($msods2 -ne $null) { $listbox1.Items.AddRange($msods2.Displayname) }
        if ($msods3 -ne $null) { $listbox1.Items.AddRange($msods3.Displayname) }
        
        $listbox1.Items.Add("Finding deleted Users...")
        $msods6 = Get-Msoluser -returndeletedusers -all | Where-Object { $_.UserPrincipalName -match $textbox1.text }
        $msods8 = Get-MsolUser -returndeletedusers -all | Where-Object { $_.ProxyAddresses -match $textbox1.text }
        $msods9 = Get-MsolUser -returndeletedusers -all | Where-Object { $_.WindowsLiveId -match $textbox1.text }
        if ($msods6 -ne $null) { $listbox1.Items.AddRange($msods6.Displayname) }
        if ($msods8 -ne $null) { $listbox1.Items.AddRange($msods8.Displayname) }
        if ($msods9 -ne $null) { $listbox1.Items.AddRange($msods9.Displayname) }
        
        $listbox1.Items.Add("Finding Groups...")
        $msods11 = Get-MsolGroup -all | Where-Object { $_.EmailAddresses -match $textbox1.text }
        if ($msods11 -ne $null) { $listbox1.Items.AddRange($msods11.Displayname) }
        
        $listbox1.Items.Add("Finding Contacts...")
        $msods4 = Get-MsolContact -all | Where-Object { $_.UserPrincipalName -match $textbox1.text }
        if ($msods4 -ne $null) { $listbox1.Items.AddRange($msods4.Displayname) }
        
        #Run EXO
        $listbox2.Items.Add("Finding MAilboxes...")
        $exo15 = Get-Mailbox -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        $exo17 = Get-Mailbox -Softdeleted -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        $exo19 = Get-Mailbox -InactiveMailboxOnly -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        $exo25 = Get-Sitemailbox -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        if ($exo15 -ne $null) { $listbox2.Items.AddRange($exo15.Displayname) }
        if ($exo17 -ne $null) { $listbox2.Items.AddRange($exo17.Displayname) }
        if ($exo19 -ne $null) { $listbox2.Items.AddRange($exo19.Displayname) }
        if ($exo25 -ne $null) { $listbox2.Items.AddRange($exo25.Displayname) }
        
        $listbox2.Items.Add("Finding Mail Users...")
        $exo11 = Get-MailUser -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        if ($exo11 -ne $null) { $listbox2.Items.AddRange($exo11.Displayname) }
        
        $listbox2.Items.Add("Finding Groups...")
        $exo1 = Get-DistributionGroup -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        $exo3 = Get-DynamicDistributionGroup -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        $exo5 = Get-UnifiedGroup -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        if ($exo1 -ne $null) { $listbox2.Items.AddRange($exo1.Displayname) }
        if ($exo3 -ne $null) { $listbox2.Items.AddRange($exo3.Displayname) }
        if ($exo5 -ne $null) { $listbox2.Items.AddRange($exo5.Displayname) }
        
        $listbox2.Items.Add("Finding Groups...")
        $exo9 = Get-MailContact -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        if ($exo9 -ne $null) { $listbox2.Items.AddRange($exo9.Displayname) }
        
        $listbox2.Items.Add("Finding Public Folders...")
        $exo21 = Get-Mailbox -PublicFolder -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        $exo23 = Get-MailPublicFolder -ResultSize unlimited | Where-Object { $_.EmailAddresses -match $textbox1.text }
        if ($exo21 -ne $null) { $listbox2.Items.AddRange($exo21.Displayname) }
        if ($exo23 -ne $null) { $listbox2.Items.AddRange($exo23.Displayname) }
        
        #Run Azure
        $listbox3.Items.Add("Finding Applications...")
        $azure = Get-AzureADApplication | Where-Object { $_.IdentifierUris -match $textbox1.text }
        if ($azure -ne $null) { $listbox3.Items.AddRange($azure.Displayname) }
        
        
        $form1.Cursor = 'Default'
        
    }
    

    #region Control Helper Functions
    function Update-ListBox
    {

        param
        (
            [Parameter(Mandatory = $true)]
            [ValidateNotNull()]
            [System.Windows.Forms.ListBox]
            $ListBox,
            [Parameter(Mandatory = $true)]
            [ValidateNotNull()]
            $Items,
            [Parameter(Mandatory = $false)]
            [string]
            $DisplayMember,
            [switch]
            $Append
        )
        
        if (-not $Append)
        {
            $listBox.Items.Clear()
        }
        
        if ($Items -is [System.Windows.Forms.ListBox+ObjectCollection] -or $Items -is [System.Collections.ICollection])
        {
            $listBox.Items.AddRange($Items)
        }
        elseif ($Items -is [System.Collections.IEnumerable])
        {
            $listBox.BeginUpdate()
            foreach ($obj in $Items)
            {
                $listBox.Items.Add($obj)
            }
            $listBox.EndUpdate()
        }
        else
        {
            $listBox.Items.Add($Items)
        }
        
        $listBox.DisplayMember = $DisplayMember
    }
    #endregion
    
    $form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs
        
    }


    #----------------------------------------------
    #region Generated Events
    #----------------------------------------------
    
    $Form_StateCorrection_Load=
    {
        #Correct the initial state of the form to prevent the .Net maximized form issue
        $form1.WindowState = $InitialFormWindowState
    }
    
    $Form_StoreValues_Closing=
    {
        #Store the control values
        $script:MainForm_textbox1 = $textbox1.Text
        $script:MainForm_listbox3 = $listbox3.SelectedItems
        $script:MainForm_listbox2 = $listbox2.SelectedItems
        $script:MainForm_listbox1 = $listbox1.SelectedItems
    }

    
    $Form_Cleanup_FormClosed=
    {
        #Remove all event handlers from the controls
        try
        {
            $textbox1.remove_TextChanged($textbox1_TextChanged)
            $labelDomainAssociationSea.remove_Click($labelDomainAssociationSea_Click)
            $labelDomainName.remove_Click($labelDomainName_Click)
            $buttonSearch.remove_Click($buttonSearch_Click)
            $form1.remove_FormClosing($form1_FormClosing)
            $form1.remove_Load($form1_Load)
            $form1.remove_Load($Form_StateCorrection_Load)
            $form1.remove_Closing($Form_StoreValues_Closing)
            $form1.remove_FormClosed($Form_Cleanup_FormClosed)
        }
        catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
    }
    #endregion Generated Events


    #----------------------------------------------
    #region Generated Form Code
    #----------------------------------------------
    $form1.SuspendLayout()
    #
    # form1
    #
    $form1.Controls.Add($textbox1)
    $form1.Controls.Add($listbox3)
    $form1.Controls.Add($listbox2)
    $form1.Controls.Add($listbox1)
    $form1.Controls.Add($labelAZUREApps)
    $form1.Controls.Add($labelEXOMailboxesMailUser)
    $form1.Controls.Add($labelMSODSUsersGroupsCont)
    $form1.Controls.Add($labelDomainAssociationSea)
    $form1.Controls.Add($labelDomainName)
    $form1.Controls.Add($buttonSearch)
    $form1.AutoScaleDimensions = '11, 20'
    $form1.AutoScaleMode = 'Font'
    $form1.ClientSize = '1075, 549'
    $form1.Font = 'Microsoft Sans Serif, 10.2pt, style=Bold'
    $form1.Margin = '6, 5, 6, 5'
    $form1.Name = 'form1'
    $form1.Text = 'Form'
    $form1.add_FormClosing($form1_FormClosing)
    $form1.add_Load($form1_Load)
    #
    # textbox1
    #
    $textbox1.Font = 'Microsoft Sans Serif, 10.2pt'
    $textbox1.Location = '349, 81'
    $textbox1.Margin = '6, 5, 6, 5'
    $textbox1.Name = 'textbox1'
    $textbox1.Size = '358, 27'
    $textbox1.TabIndex = 13
    $textbox1.add_TextChanged($textbox1_TextChanged)
    #
    # listbox3
    #
    $listbox3.Font = 'Microsoft Sans Serif, 10.2pt'
    $listbox3.FormattingEnabled = $True
    $listbox3.ItemHeight = 20
    $listbox3.Location = '720, 254'
    $listbox3.Margin = '6, 5, 6, 5'
    $listbox3.Name = 'listbox3'
    $listbox3.Size = '312, 264'
    $listbox3.TabIndex = 12
    #
    # listbox2
    #
    $listbox2.Font = 'Microsoft Sans Serif, 10.2pt'
    $listbox2.FormattingEnabled = $True
    $listbox2.ItemHeight = 20
    $listbox2.Location = '381, 254'
    $listbox2.Margin = '6, 5, 6, 5'
    $listbox2.Name = 'listbox2'
    $listbox2.Size = '300, 264'
    $listbox2.TabIndex = 11
    #
    # listbox1
    #
    $listbox1.Font = 'Microsoft Sans Serif, 10.2pt'
    $listbox1.FormattingEnabled = $True
    $listbox1.ItemHeight = 20
    $listbox1.Location = '33, 254'
    $listbox1.Margin = '6, 5, 6, 5'
    $listbox1.Name = 'listbox1'
    $listbox1.Size = '303, 264'
    $listbox1.TabIndex = 10
    #
    # labelAZUREApps
    #
    $labelAZUREApps.AutoSize = $True
    $labelAZUREApps.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold'
    $labelAZUREApps.Location = '730, 206'
    $labelAZUREApps.Margin = '6, 0, 6, 0'
    $labelAZUREApps.Name = 'labelAZUREApps'
    $labelAZUREApps.Size = '58, 32'
    $labelAZUREApps.TabIndex = 9
    $labelAZUREApps.Text = 'AZURE
(Apps)'

    #
    # labelEXOMailboxesMailUser
    #
    $labelEXOMailboxesMailUser.AutoSize = $True
    $labelEXOMailboxesMailUser.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold'
    $labelEXOMailboxesMailUser.Location = '382, 190'
    $labelEXOMailboxesMailUser.Margin = '6, 0, 6, 0'
    $labelEXOMailboxesMailUser.Name = 'labelEXOMailboxesMailUser'
    $labelEXOMailboxesMailUser.Size = '227, 48'
    $labelEXOMailboxesMailUser.TabIndex = 8
    $labelEXOMailboxesMailUser.Text = 'EXO
(Mailboxes, MailUsers, Groups,
Public Folders)'

    #
    # labelMSODSUsersGroupsCont
    #
    $labelMSODSUsersGroupsCont.AutoSize = $True
    $labelMSODSUsersGroupsCont.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold'
    $labelMSODSUsersGroupsCont.Location = '33, 206'
    $labelMSODSUsersGroupsCont.Margin = '6, 0, 6, 0'
    $labelMSODSUsersGroupsCont.Name = 'labelMSODSUsersGroupsCont'
    $labelMSODSUsersGroupsCont.Size = '184, 32'
    $labelMSODSUsersGroupsCont.TabIndex = 7
    $labelMSODSUsersGroupsCont.Text = 'MSODS
(Users, Groups, Contacts)'

    #
    # labelDomainAssociationSea
    #
    $labelDomainAssociationSea.AutoSize = $True
    $labelDomainAssociationSea.Font = 'Microsoft Sans Serif, 12pt, style=Bold'
    $labelDomainAssociationSea.Location = '382, 9'
    $labelDomainAssociationSea.Margin = '6, 0, 6, 0'
    $labelDomainAssociationSea.Name = 'labelDomainAssociationSea'
    $labelDomainAssociationSea.Size = '278, 25'
    $labelDomainAssociationSea.TabIndex = 3
    $labelDomainAssociationSea.Text = 'Domain Association Search'
    $labelDomainAssociationSea.add_Click($labelDomainAssociationSea_Click)
    #
    # labelDomainName
    #
    $labelDomainName.AutoSize = $True
    $labelDomainName.Location = '209, 84'
    $labelDomainName.Margin = '6, 0, 6, 0'
    $labelDomainName.Name = 'labelDomainName'
    $labelDomainName.Size = '127, 20'
    $labelDomainName.TabIndex = 2
    $labelDomainName.Text = 'Domain Name'
    $labelDomainName.add_Click($labelDomainName_Click)
    #
    # buttonSearch
    #
    $buttonSearch.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold'
    $buttonSearch.Location = '730, 78'
    $buttonSearch.Margin = '6, 5, 6, 5'
    $buttonSearch.Name = 'buttonSearch'
    $buttonSearch.Size = '126, 35'
    $buttonSearch.TabIndex = 1
    $buttonSearch.Text = 'Search'
    $buttonSearch.UseVisualStyleBackColor = $True
    $buttonSearch.add_Click($buttonSearch_Click)
    $form1.ResumeLayout()
    #endregion Generated Form Code

    #----------------------------------------------

    #Save the initial state of the form
    $InitialFormWindowState = $form1.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $form1.add_Load($Form_StateCorrection_Load)
    #Clean up the control events
    $form1.add_FormClosed($Form_Cleanup_FormClosed)
    #Store the control values when form is closing
    $form1.add_Closing($Form_StoreValues_Closing)
    #Show the Form
    return $form1.ShowDialog()

}

#Start the application
Main ($CommandLine)