-
Notifications
You must be signed in to change notification settings - Fork 0
/
$profile.txt
384 lines (251 loc) · 11.6 KB
/
$profile.txt
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
Function New-O365Session{
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ParameterSetName="BasicSession")]
[Switch]$Basic,
[parameter(Mandatory=$true, ParameterSetName="BasicSession")]
[System.Management.Automation.PSCredential]$Credential,
[parameter(Mandatory=$true, ParameterSetName="ModernSession")]
[Switch]$Modern
)
If($Basic){
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Credential -Authentication Basic –AllowRedirection
Import-PSSession $Session
Return $Session
}
If($Modern){
$CreateEXOPSSession = (Get-ChildItem -Path $env:userprofile -Filter CreateExoPSSession.ps1 -Recurse -ErrorAction SilentlyContinue -Force | Select -Last 1).DirectoryName
. "$CreateEXOPSSession\CreateExoPSSession.ps1"
Connect-EXOPSSession
}
}
Function Copy-O365GroupMembership{
[CmdletBinding()]
Param(
[parameter(Mandatory=$true)][String]$SourceGroup,
[parameter(Mandatory=$true)][String]$DestinationGroup,
[ValidateSet("Basic", "Modern")]
[String]$Session
)
If($Session -eq "Basic"){New-O365Session -Basic -Credential (Get-Credential "f12admin@")}
If($NewSession = "Modern"){ New-O365Session -Modern }
Get-DistributionGroupMember -Identity $SourceGroup | % {Write-Host -ForegroundColor Yellow "Attempting to add" $_.Name "..." ;Add-DistributionGroupMember -Identity $DestinationGroup -Member $_.PrimarySmtpAddress}
}
Function Disable-O365MobileDevices{
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]$Identity,
[ValidateSet("Basic", "Modern")]
[String]$Session
)
If($Session -eq "Basic"){New-O365Session -Basic -Credential (Get-Credential "f12admin@")}
ElseIf($Session -eq "Modern"){New-O365Session -Modern}
Get-MobileDevice -Mailbox $Identity | Remove-MobileDevice -confirm:$false -Verbose
Set-CASMailbox -Identity $Identity -ActiveSyncEnabled $False -Verbose
}
Function Convert-SharedMailbox{
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]$Identity,
[ValidateSet("Basic", "Modern")]
[String]$Session
)
If($Session -eq "Basic"){New-O365Session -Basic -Credential (Get-Credential "f12admin@")}
ElseIf($Session -eq "Modern"){New-O365Session -Modern}
Set-Mailbox -Identity $Identity -Type shared -verbose
sleep 5
Get-Mailbox -Identity $Identity | Select name,IsShared
}
Function Remove-O365DistributionGroupMembership{
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]$Identity,
[ValidateSet("Basic", "Modern")]
[String]$Session
)
If($Session -eq "Basic"){New-O365Session -Basic -Credential (Get-Credential "f12admin@")}
ElseIf($Session -eq "Modern"){New-O365Session -Modern}
$DistributionGroups = Get-Distributiongroup -resultsize unlimited
$UserDName = (Get-Mailbox $Identity).name
ForEach ($Group in $DistributionGroups){
if ((Get-Distributiongroupmember $Group.Name | select -expand name) -contains $UserDName){
write-host -foregroundcolor yellow "Removing $UserDName from group '$Group'"
Remove-DistributionGroupMember -Identity "$Group" -Member "$UserDName" -verbose -Confirm:$False
}
}
}
Function Block-O365Mailbox {
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]$Identity,
[parameter(Mandatory=$true)]
[System.Management.Automation.PSCredential]$Credential
)
Connect-MsolService -Credential $Credential
$UserPrincipleName = (Get-Mailbox -Identity $Identity).PrimarySmtpAddress
Set-MsolUser -UserPrincipalName $UserPrincipleName -BlockCredential $true
Get-MsolUser -UserPrincipalName $UserPrincipleName | select Name,BlockCredential
}
Function Get-O365MailboxLicenses {
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]$Identity,
[parameter(Mandatory=$true)]
[System.Management.Automation.PSCredential]$Credential
)
Connect-MsolService -Credential $Credential
$UserPrincipleName = (Get-Mailbox -Identity $Identity).PrimarySmtpAddress
(Get-MsolUser -UserPrincipalName $UserPrincipleName).Licenses | select AccountskuId | ft @{L='Account Licenses';E={$_.AccountSkuID}}
}
Function Remove-O365MailboxLicenses {
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]$Identity,
[parameter(Mandatory=$true)]
[System.Management.Automation.PSCredential]$Credential
)
New-O365Session -Basic $Credential
Connect-MsolService -Credential $Credential
$UserPrincipleName = (Get-Mailbox -Identity $Identity).PrimarySmtpAddress
(get-MsolUser -UserPrincipalName $UserPrincipleName).licenses.AccountSkuId | foreach{
Write-host -ForegroundColor Cyan "Removing License: $_"
Set-MsolUserLicense -UserPrincipalName $UserPrincipleName -RemoveLicenses $_
}
}
Function Set-O365MailboxForwarding {
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]$Identity,
[parameter(Mandatory=$true)]
[String]$ForwardingAddress,
[ValidateSet("Basic", "Modern")]
[String]$Session,
[Switch]$DeliverToMailboxAndForward
)
If($Session -eq "Basic"){New-O365Session -Basic}
ElseIf($Session -eq "Modern"){New-O365Session -Modern}
Set-Mailbox -Identity $Identity -ForwardingAddress ((Get-Mailbox -Identity $ForwardingAddress).PrimarySMTPAddress) -DeliverToMailboxAndForward $DeliverToMailboxAndForward -Confirm:$false
Get-Mailbox -Identity $Identity | Format-List Name,ForwardingAddress,DeliverToMailboxandForward
}
Function Disable-O365Mailbox {
<#
.Synopsis
Completely closes down an account when an employee leaves the company.
.Description
Converts mailbox to a shared mailbox, Removes Distribution Group Membership, Removes mobile devices and disables activesync, Forwards email and, displays mailbox licenses.
.Parameter Identity
Email address or name on mailbox to be disabled
.Parameter ForwardingAddress
Email address to forward to.
.Parameter Session
Runs the New-O365Session command in either 'Basic' or 'Modern'
.Example
Disable-O365Mailbox -Identity "Jim John"
.Example
Disable-O365Mailbox -Identity "[email protected]" -ForwardingAddress "Tim Thompson" -Session Basic
#>
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String]$Identity,
[ValidateSet("Basic", "Modern")]
[String]$Session,
[String]$ForwardingAddress,
[System.Management.Automation.PSCredential]$Credential
)
If($Session -eq "Basic"){New-O365Session -Basic}
ElseIf($Session -eq "Modern"){New-O365Session -Modern}
Disable-O365MobileDevices -Identity $Identity
Convert-SharedMailbox -Identity $Identity
Remove-O365DistributionGroupMembership -Identity $Identity
If($ForwardingAddress){Set-O365MailboxForwarding -Identity $Identity -ForwardingAddress $ForwardingAddress}
Write-Host -ForegroundColor Yellow "Enter admin credentials to gather mailbox licenses or cancel to finish command."
Get-O365MailboxLicenses -Identity $Identity -Credential $Credential
}
Function Add-O365BlockedIP{
<#
.Synopsis
Adds an IP Address to the IPBlockList
.Description
Adds an IP Address to the IPBlockList in that can be found under Exchange Admin Center > Protection > Connection Filter.
This is a useful command if you need to block an IP address of a domain that consistently sends spam mail.
.Parameter IpAddress
Format: '192.168.1.1' No Quotation marks.
This is the IP Address of the sender domain you want to block.
.Parameter PolicyName
Name of the policy that will be appended. Default is "Default"
.Parameter Session
Runs the New-O365Session command in either 'Basic' or 'Modern'
.Example
Add-O365BlockedIP -IpAddress 192.168.244.242
.Example
Add-O365BlockedIP -IpAddress 192.168.244.242 -PolicyName "ConnectionPolicy" -Session Basic
#>
[CmdletBinding()]
Param(
[parameter(Mandatory=$true)]
$IpAddress,
[ValidateSet("Basic", "Modern")]
[String]$Session,
[String]$PolicyName = "Default"
)
If($Session -eq "Basic"){New-O365Session -Basic -Credential (Get-Credential "f12admin@")}
ElseIf($Session -eq "Modern"){New-O365Session -Modern}
If($IpAddress.FromIP){ $IpAddress = $IpAddress.FromIP }
$IPblocklist = (Get-HostedConnectionFilterPolicy -Identity $PolicyName).IPBlockList
$IPblocklist += $IPAddress
Set-HostedConnectionFilterPolicy -Identity $PolicyName -IPBlockList $IPblocklist
Get-HostedConnectionFilterPolicy -Identity $PolicyName | select name,Ipblocklist | ft
}
Function Trace-O365Email {
<#
.Synopsis
Searches a mailbox for an email and provides the sender's IP Address.
.Description
Searches a mailbox for an email and provides the sender's IP Address.
Can be used in conjunction with Add-O365BlockedIP to block a sender quickly.
To return only the IP Address, wrap the command with (<command>).FromIP
.Parameter RecipientAddress
Email Address of the Recipient.
.Parameter SenderAddress
Email Address of the Sender.
.Parameter Subject
Subject of the email, searched with -like operator.
.Parameter StartDate
Starting Day to begin searching emails from. Default 2 weeks from current date (Get-Date.AddDays(-14))
.Parameter EndDate
End day to begin searching emails from. Default is current date.
.Parameter Session
Runs the New-O365Session command in either 'Basic' or 'Modern'
.Example
Trace-O365Email -RecipientAddress "[email protected]" -SenderAddress "*@Aol.com"
.Example
Trace-O365Email -RecipientAddress "[email protected]" -SenderAddress "[email protected]" -Startdate 02/14/2019 -EndDate 02/17/2019
.Example
Add-O365BlockedIP -IpAddress (Trace-O365Email -RecipientAddress "[email protected]" -SenderAddress "[email protected]")
#>
[CmdletBinding()]
Param(
[parameter(Mandatory=$true)]
[String]$RecipientAddress,
[String]$SenderAddress = "",
[ValidateSet("Basic", "Modern")]
[String]$Session,
[String]$StartDate = (Get-Date).AddDays(-14),
[String]$EndDate = (Get-Date),
[String]$Subject
)
If($Session -eq "Basic"){New-O365Session -Basic -Credential (Get-Credential "f12admin@")}
ElseIf($Session -eq "Modern"){New-O365Session -Modern}
$Trace = Get-MessageTrace -RecipientAddress $RecipientAddress -SenderAddress $SenderAddress -StartDate $StartDate -EndDate $EndDate
If($Subject){ $Trace = $Trace | Where-Object -Property Subject -Like "*$Subject*" }
$SenderIPAddress = $Trace | select FromIP,SenderAddress,Subject
Return $SenderIPAddress
}