-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerShellM365Toolbox.ps1
More file actions
414 lines (274 loc) · 17.9 KB
/
Copy pathPowerShellM365Toolbox.ps1
File metadata and controls
414 lines (274 loc) · 17.9 KB
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
<#
.SYNOPSIS
Handy tool for stuff that usually requires PS, but I'm to lazy to use
.DESCRIPTION
Handy tool for stuff that usually requires PS, but I'm to lazy to use
.INPUTS
GUI
.OUTPUTS
None
.NOTES
Version: 0.1.2
Author: TenOfNine
Creation Date: 03.11.2021
.EXAMPLE
None provided
#>
using module Microsoft.PowerShell.Management
using module Microsoft.PowerShell.Utility
using namespace System.Management.Automation
using namespace System.PresentationFramework
using namespace System.Windows.Markup
$ErrorActionPreferenceBackup = $ErrorActionPreference
Set-StrictMode -Version 'Latest'
Add-Type -AssemblyName 'PresentationFramework'
$Script:My = [HashTable]::Synchronized(@{})
$Script:My.WindowXaml = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PowerShellM365Toolbox"
mc:Ignorable="d"
Title="PowerShell M365 Toolbox" Height="480" Width="820
" ResizeMode="NoResize" SizeToContent="Manual">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="17*"/>
<ColumnDefinition Width="63*"/>
</Grid.ColumnDefinitions>
<TabControl Grid.ColumnSpan="2">
<TabItem Header="Overview" Height="20">
<Grid Background="#FFE5E5E5">
<GroupBox HorizontalAlignment="Left" Height="200" Header="To what am I connected?" Margin="10,10,0,0" VerticalAlignment="Top" Width="280">
<StackPanel>
<CheckBox x:Name="OverviewCBAzure" Content="Azure AD" IsEnabled="False"/>
<CheckBox x:Name="OverviewCBEXO" Content="Exchange Online" IsEnabled="False"/>
<CheckBox x:Name="OverviewCBTeams" Content="Teams" IsEnabled="False"/>
</StackPanel>
</GroupBox>
<GroupBox HorizontalAlignment="Left" Height="200" Header="About" Margin="587,10,0,0" VerticalAlignment="Top" Width="197">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="PowerShell M365 Toolbox v0.1.2 03.11.2021 https://github.com/TenOfNine/PowerShellM365Toolbox" Width="187"/>
</GroupBox>
<GroupBox HorizontalAlignment="Left" Height="194" Header="Required Modules" Margin="307,19,0,0" VerticalAlignment="Top" Width="260">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Install-Module Azure-AD Install-Module ExchangeOnlineManagement" Width="250"/>
</GroupBox>
</Grid>
</TabItem>
<TabItem Header="Azure AD" Height="20">
<Grid Background="#FFE5E5E5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="27*"/>
<ColumnDefinition Width="115*"/>
<ColumnDefinition Width="652*"/>
</Grid.ColumnDefinitions>
<TabControl Grid.ColumnSpan="3">
<TabItem Header="Start">
<Grid Background="#FFE5E5E5">
<Button x:Name="azstartbtnconnectaz" Content="Connect to Azure" HorizontalAlignment="Left" Height="30" Margin="14,19,0,0" VerticalAlignment="Top" Width="175"/>
</Grid>
</TabItem>
<TabItem Header="Groups">
<Grid Background="#FFE5E5E5">
<Label Content="Cloud-Groups a user is member/owner of" HorizontalAlignment="Left" Height="24" Margin="8,10,0,0" VerticalAlignment="Top" Width="240"/>
<TextBox x:Name="azgroupsofUserSearchtb1" HorizontalAlignment="Left" Height="20" Margin="10,39,0,0" TextWrapping="Wrap" Text="Enter UPN" VerticalAlignment="Top" Width="181" IsEnabled="False"/>
<Button x:Name="azgroupsofUserSearchbtn1" Content="Search" HorizontalAlignment="Left" Height="20" Margin="200,39,0,0" VerticalAlignment="Top" Width="180" IsEnabled="False"/>
<Button x:Name="azgroupsofUserCopyMemberbtn1" Content="Copy Member-Groups to Clip" HorizontalAlignment="Left" Height="20" Margin="11,69,0,0" VerticalAlignment="Top" Width="180" IsEnabled="False"/>
<Button x:Name="azgroupsofUserCopyOwnerbtn1" Content="Copy Owner-Groups to Clip" HorizontalAlignment="Left" Height="20" Margin="200,69,0,0" VerticalAlignment="Top" Width="180" IsEnabled="False"/>
<ListBox x:Name="azgroupsofUserlb1" HorizontalAlignment="Left" Height="285" Margin="11,94,0,0" VerticalAlignment="Top" Width="180" d:ItemsSource="{d:SampleData ItemCount=5}"/>
<ListBox x:Name="azgroupsofUserlb2" Grid.Column="1" HorizontalAlignment="Left" Height="285" Margin="200,94,0,0" VerticalAlignment="Top" Width="180" d:ItemsSource="{d:SampleData ItemCount=5}"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
</TabItem>
<TabItem Header="EXO" Height="20">
<Grid Background="#FFE5E5E5">
<TabControl>
<TabItem Header="Start">
<Grid Background="#FFE5E5E5">
<Button x:Name="exostartbtnconnectexo" Content="Connect to Exchange Online" HorizontalAlignment="Left" Height="30" Margin="14,19,0,0" VerticalAlignment="Top" Width="175"/>
</Grid>
</TabItem>
<TabItem Header="Roomlists">
<Grid Background="#FFE5E5E5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9*"/>
<ColumnDefinition Width="799*"/>
</Grid.ColumnDefinitions>
<Button x:Name="exoroomlistsaddbtn" Content="Add" HorizontalAlignment="Left" Height="25" Margin="485,174,0,0" VerticalAlignment="Top" Width="40" Grid.Column="1" IsEnabled="False"/>
<Button x:Name="exoroomlistsdelbtn" Content="Del" HorizontalAlignment="Left" Height="25" Margin="485,208,0,0" VerticalAlignment="Top" Width="40" Grid.Column="1" IsEnabled="False"/>
<TextBox x:Name="exoroomlistsSearchtb1" HorizontalAlignment="Left" Height="20" Margin="0,39,0,0" TextWrapping="Wrap" Text="Search" VerticalAlignment="Top" Width="135" Grid.Column="1" IsEnabled="False"/>
<TextBox x:Name="exoroomlistsSearchtb2" HorizontalAlignment="Left" Height="20" Margin="539,39,0,0" TextWrapping="Wrap" Text="Search" VerticalAlignment="Top" Width="135" Grid.Column="1" IsEnabled="False"/>
<Button x:Name="exoroomlistsSearchbtn1" Content="Search" HorizontalAlignment="Left" Height="20" Margin="145,39,0,0" VerticalAlignment="Top" Width="86" Grid.Column="1" IsEnabled="False"/>
<Button x:Name="exoroomlistsSearchbtn2" Content="Search" HorizontalAlignment="Left" Height="20" Margin="683,39,0,0" VerticalAlignment="Top" Width="86" Grid.Column="1" IsEnabled="False"/>
<Label Content="Roomlists" HorizontalAlignment="Left" Height="24" Margin="61,10,0,0" VerticalAlignment="Top" Width="110" Grid.Column="1"/>
<Label Content="Rooms in selected roomlist" HorizontalAlignment="Left" Height="24" Margin="280,10,0,0" VerticalAlignment="Top" Width="160" Grid.Column="1"/>
<Label Content="All rooms" HorizontalAlignment="Left" Height="24" Margin="599,10,0,0" VerticalAlignment="Top" Width="110" Grid.Column="1"/>
<ListBox x:Name="exoroomlistslb1" Grid.Column="1" HorizontalAlignment="Left" Height="295" Margin="1,84,0,0" VerticalAlignment="Top" Width="230" d:ItemsSource="{d:SampleData ItemCount=5}"/>
<ListBox x:Name="exoroomlistslb2" Grid.Column="1" HorizontalAlignment="Left" Height="295" Margin="245,84,0,0" VerticalAlignment="Top" Width="230" d:ItemsSource="{d:SampleData ItemCount=5}"/>
<ListBox x:Name="exoroomlistslb3" Grid.Column="1" HorizontalAlignment="Left" Height="295" Margin="539,84,0,0" VerticalAlignment="Top" Width="230" d:ItemsSource="{d:SampleData ItemCount=5}"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
'@
$Script:My.Window = [XamlReader]::Parse($Script:My.WindowXaml)
$Script:My.CheckBox1 = $Script:My.Window.FindName('OverviewCBAzure')
$Script:My.CheckBox2 = $Script:My.Window.FindName('OverviewCBEXO')
$Script:My.CheckBox3 = $Script:My.Window.FindName('OverviewCBTeams')
#---------------------Azure-Declarations-------------------
$Script:My.ConnectAzurebtn = $Script:My.Window.FindName('azstartbtnconnectaz')
$Script:My.AzGroupsOfMemberSearchTb1 = $Script:My.Window.FindName('azgroupsofUserSearchtb1')
$Script:My.AzGroupsOfMemberSearchBtn1 = $Script:My.Window.FindName('azgroupsofUserSearchbtn1')
$Script:My.AzGroupsOfMemberCopyMemberBtn1 = $Script:My.Window.FindName('azgroupsofUserCopyMemberbtn1')
$Script:My.AzGroupsOfMemberCopyOwnerBtn1 = $Script:My.Window.FindName('azgroupsofUserCopyOwnerbtn1')
$Script:My.AzGroupsOfMemberlistView1 = $Script:My.Window.FindName('azgroupsofUserlb1')
$Script:My.AzGroupsOfMemberlistView2 = $Script:My.Window.FindName('azgroupsofUserlb2')
#---------------------Azure-Declarations-------------------
#----------------------EXO-Declarations--------------------
$Script:My.ConnectExchangebtn = $Script:My.Window.FindName('exostartbtnconnectexo')
$Script:My.EXORoomlistView1 = $Script:My.Window.FindName('exoroomlistslb1')
$Script:My.EXORoomlistView2 = $Script:My.Window.FindName('exoroomlistslb2')
$Script:My.EXORoomlistView3 = $Script:My.Window.FindName('exoroomlistslb3')
$Script:My.EXORoomlistsAddBtn = $Script:My.Window.FindName('exoroomlistsaddbtn')
$Script:My.EXORoomlistsDelBtn = $Script:My.Window.FindName('exoroomlistsdelbtn')
$Script:My.EXORoomlistsSearchBtn1 = $Script:My.Window.FindName('exoroomlistsSearchbtn1')
$Script:My.EXORoomlistsSearchBtn2 = $Script:My.Window.FindName('exoroomlistsSearchbtn2')
$Script:My.EXORoomlistsSearchTb1 = $Script:My.Window.FindName('exoroomlistsSearchtb1')
$Script:My.EXORoomlistsSearchTb2 = $Script:My.Window.FindName('exoroomlistsSearchtb2')
#----------------------EXO-Declarations--------------------
$arraylistgroups = New-Object System.Collections.ArrayList
$arraylistrooms = New-Object System.Collections.ArrayList
$arraylistmembers = New-Object System.Collections.ArrayList
$AzGroupsOfMemberSearch = New-Object System.Collections.ArrayList
#----------------------Connect-Buttons--------------------
$Script:My.ConnectAzurebtn.Add_Click({
try {$var = Get-AzureADTenantDetail}catch [Microsoft.Open.Azure.AD.CommonLibrary.AadNeedAuthenticationException]{Connect-AzureAD}
$Script:My.AzGroupsOfMemberSearchTb1.IsEnabled = $true
$Script:My.AzGroupsOfMemberSearchBtn1.IsEnabled = $true
$Script:My.AzGroupsOfMemberlistView1.Items.Clear()
$Script:My.AzGroupsOfMemberlistView2.Items.Clear()
$Script:My.ConnectAzurebtn.IsEnabled = $false
$Script:My.CheckBox1.IsChecked = $true
})
$Script:My.ConnectExchangebtn.Add_Click({
$getsessions = Get-PSSession | Select-Object -Property State, Name
$isconnected = (@($getsessions) -like '@{State=Opened; Name=ExchangeOnlineInternalSession*').Count -gt 0
If ($isconnected -ne "True") {
Connect-ExchangeOnline
}
$Script:My.EXORoomlistsAddBtn.IsEnabled = $true
$Script:My.EXORoomlistsDelBtn.IsEnabled = $true
$Script:My.EXORoomlistsSearchTb1.IsEnabled = $true
$Script:My.EXORoomlistsSearchTb2.IsEnabled = $true
$Script:My.EXORoomlistsSearchBtn1.IsEnabled = $true
$Script:My.EXORoomlistsSearchBtn2.IsEnabled = $true
$Script:My.EXORoomlistView1.Items.Clear()
$Script:My.EXORoomlistView2.Items.Clear()
$Script:My.EXORoomlistView3.Items.Clear()
$arraylistgroups.Clear();
$arraylistrooms.Clear();
$arraylistmembers.Clear();
loadexoroomlistview
$Script:My.ConnectExchangebtn.IsEnabled = $false
$Script:My.CheckBox2.IsChecked = $true
})
#----------------------Connect-Buttons--------------------
#--------------Azure-GroupsOfMember-or-Owner--------------
$Script:My.AzGroupsOfMemberSearchBtn1.Add_Click({
$varAzGroupsOfMemberUPN = $Script:My.AzGroupsOfMemberSearchTb1.Text.ToString()
$Script:My.AzGroupsOfMemberlistView1.Items.Clear()
$Script:My.AzGroupsOfMemberlistView2.Items.Clear()
Get-AzureADUser -SearchString $varAzGroupsOfMemberUPN | Get-AzureADUserMembership | ? {$_.ObjectType -ne "Role"} | ? {$_.DirSyncEnabled -like ""} | % {Get-AzureADGroup -ObjectId $_.ObjectId | select DisplayName} | Sort-Object -Property DisplayName | ForEach-Object {$Script:My.AzGroupsOfMemberlistView1.Items.Add($_.DisplayName) |Out-Null}
Get-AzureADUser -SearchString $varAzGroupsOfMemberUPN | Get-AzureADUserOwnedObject | ? {$_.ObjectType -ne "Role"} | ? {$_.DirSyncEnabled -like ""} | % {Get-AzureADGroup -ObjectId $_.ObjectId | select DisplayName} | Sort-Object -Property DisplayName | ForEach-Object {$Script:My.AzGroupsOfMemberlistView2.Items.Add($_.DisplayName) |Out-Null}
$Script:My.AzGroupsOfMemberCopyMemberBtn1.IsEnabled = $true
$Script:My.AzGroupsOfMemberCopyOwnerBtn1.IsEnabled = $true
})
$Script:My.AzGroupsOfMemberCopyMemberBtn1.Add_Click({
$varAzGroupsOfMemberUPN = $Script:My.AzGroupsOfMemberSearchTb1.Text.ToString()
Get-AzureADUser -SearchString $varAzGroupsOfMemberUPN | Get-AzureADUserMembership | ? {$_.ObjectType -ne "Role"} | ? {$_.DirSyncEnabled -like ""} | % {Get-AzureADGroup -ObjectId $_.ObjectId | select DisplayName} | Sort-Object -Property DisplayName | Select -ExpandProperty DisplayName | Set-Clipboard
})
$Script:My.AzGroupsOfMemberCopyOwnerBtn1.Add_Click({
$varAzGroupsOfMemberUPN = $Script:My.AzGroupsOfMemberSearchTb1.Text.ToString()
Get-AzureADUser -SearchString $varAzGroupsOfMemberUPN | Get-AzureADUserOwnedObject | ? {$_.ObjectType -ne "Role"} | ? {$_.DirSyncEnabled -like ""} | % {Get-AzureADGroup -ObjectId $_.ObjectId | select DisplayName} | Sort-Object -Property DisplayName | Select -ExpandProperty DisplayName | Set-Clipboard
})
#--------------Azure-GroupsOfMember-or-Owner--------------
#----------------------EXO-Roomlists--------------------
$Script:My.EXORoomlistsSearchBtn1.Add_Click({
$varEXORoomlistsSearchBtn1 = $Script:My.EXORoomlistsSearchTb1.Text.ToString()
if($varEXORoomlistsSearchBtn1 -eq "Search"){$varEXORoomlistsSearchBtn1=""}
$Script:My.EXORoomlistView1.Items.Clear()
$arraylistgroups.Clear();
$global:groups -match $varEXORoomlistsSearchBtn1 | Select-Object DisplayName, Id | ForEach-Object {$Script:My.EXORoomlistView1.Items.Add($_.DisplayName) |Out-Null
$arraylistgroups.Add($_.Id)
}
})
$Script:My.EXORoomlistsSearchBtn2.Add_Click({
$varEXORoomlistsSearchBtn2 = $Script:My.EXORoomlistsSearchTb2.Text.ToString()
if($varEXORoomlistsSearchBtn2 -eq "Search"){$varEXORoomlistsSearchBtn2=""}
$Script:My.EXORoomlistView3.Items.Clear()
$arraylistrooms.Clear();
$global:rooms -match $varEXORoomlistsSearchBtn2 | ForEach-Object {$Script:My.EXORoomlistView3.Items.Add($_) |Out-Null
$arraylistrooms.Add($_)
}
})
$Script:My.EXORoomlistsAddBtn.Add_Click({
$selected = $Script:My.EXORoomlistView1.SelectedIndex
$index1 = $arraylistgroups[$selected]
$selected = $Script:My.EXORoomlistView3.SelectedIndex
$index2 = $arraylistrooms[$selected]
Add-DistributionGroupMember -Id $index1 -Member "$index2"
})
$Script:My.EXORoomlistsDelBtn.Add_Click({
$selected = $Script:My.EXORoomlistView1.SelectedIndex
$index1 = $arraylistgroups[$selected]
$selected = $Script:My.EXORoomlistView2.SelectedIndex
$index2 = $arraylistmembers[$selected]
Remove-DistributionGroupMember -Id $index1 -Member "$index2"
loadexoroomlistview
})
#----------------------EXO-Roomlists--------------------
#--------------------EXO-Room-ListView------------------
$Script:My.EXORoomlistView1.Add_SelectionChanged({
$Script:My.EXORoomlistView2.Items.Clear()
$selected = $Script:My.EXORoomlistView1.SelectedIndex
$index = $arraylistgroups[$selected]
$members = Get-DistributionGroupMember -Id $index | select -ExpandProperty DisplayName | Sort-Object
$arraylistmembers.Clear();
$members | ForEach-Object {$Script:My.EXORoomlistView2.Items.Add($_) |Out-Null
$arraylistmembers.Add($_)
}
})
#--------------------EXO-Room-ListView------------------
#---------------------Functionblock---------------------
function loadexoroomlistview(){
$global:groups = Get-DistributionGroup -RecipientTypeDetails RoomList
$global:rooms = Get-Mailbox -RecipientTypeDetails RoomMailbox | select -ExpandProperty DisplayName
$global:groups = $global:groups | Sort-Object -Property "DisplayName"
$global:rooms = $global:rooms | Sort-Object -Property "DisplayName"
$Script:My.EXORoomlistView1.Items.Clear()
$arraylistgroups.Clear();
$global:groups | Select-Object DisplayName, Id | ForEach-Object {$Script:My.EXORoomlistView1.Items.Add($_.DisplayName) |Out-Null
$arraylistgroups.Add($_.Id)
}
$Script:My.EXORoomlistView3.Items.Clear()
$arraylistrooms.Clear();
$global:rooms | ForEach-Object {$Script:My.EXORoomlistView3.Items.Add($_) |Out-Null
$arraylistrooms.Add($_)
}
}
#---------------------Functionblock---------------------
#-------------------Clean-up-garbage--------------------
$Script:My.Window.Topmost = $true
$Script:My.Window.ShowDialog() | Out-Null
Remove-Variable -Name 'My' -Force
Set-StrictMode -Off
$ErrorActionPreference = $ErrorActionPreferenceBackup
Remove-Module -Name 'Microsoft.PowerShell.Management', 'Microsoft.PowerShell.Utility' -Force
#-------------------Clean-up-garbage--------------------