-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ps1
78 lines (60 loc) · 1.6 KB
/
script.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
param(
[string]$InputString,
[switch]$FirstSearch,
[switch]$EXO_On,
[switch]$EXO_Modified,
[string]$Username
)
function IsEmail($inputString) {
return $inputString -match "^[\w\.-]+@[\w\.-]+\.\w+$"
}
Write-Host("Input string: $InputString")
Write-Host("First search: $FirstSearch")
if ($EXO_On) {
Write-Host("Username: $Username")
}
if ($FirstSearch) {
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
if($EXO_On) {
Connect-ExchangeOnline -UserPrincipalName $Username -ShowProgress $false -ShowBanner $false
}
}
else {
if($EXO_Modified && !$EXO_On) {
Disconnect-ExchangeOnline
}
}
$entries = $InputString -split ";"
$mnems = @()
foreach ($entry in $entries) {
Write-Host("Processing entry: $entry")
$entry = $entry.Trim()
if (IsEmail $entry) {
Write-Host "Email: $entry"
$mnems += Get-Mailbox $entry | Select-Object SamAccountName
}
else {
$nameParts = $entry -split ","
if ($nameParts.Length -eq 2) {
$firstName = $nameParts[0].Trim()
$lastName = $nameParts[1].Trim()
Write-Host "Name: $firstName $lastName"
$mnems += Get-Mailbox $lastName, $firstName | Select-Object SamAccountName
}
else {
$noResults = @{
Message = "No results found for $entry"
}
$obj = New-Object -TypeName PsObject -Property $noResults
$mnems += $obj
}
}
}
$mnems | ForEach-Object {
if ($_.Message) {
$_.Message
}
else {
$_.SamAccountName
}
}