-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImport-PhoneNumbers.ps1
182 lines (135 loc) · 4.93 KB
/
Import-PhoneNumbers.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
#requires -version 5
<#
.SYNOPSIS
Import-PhoneNumbers is a Powershell script that leverages remote PS sessions on O365 to allow for importing VZ mobile numbers into Azure Active Directory
.DESCRIPTION
Using A CSV and remote PS sessions on O365 We can import a list of phone numbers to Azure AD
.PARAMETER PhoneNumbers
PhoneNumber: A file path param it is a string.
NameField: Tells the script what COL the names are in
NumberField: Tells the script what COL the phone numbers are in
.INPUTS
PhoneNumbers.csv
.OUTPUTS
Log file stored in C:\Windows\Temp\import-PhoneNumbers.log>
.NOTES
Version: 1.0
Author: Jacob Ernst
Creation Date: 06/12/2019
Purpose/Change: Initial script development
Template: https://gist.github.com/9to5IT/9620683
.EXAMPLE
import-PhoneNumbers -PhoneNumbers "C:\windows\Temp\PhoneNumbers.csv"
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$ErrorActionPreference = "SilentlyContinue"
#Dot Source required Function Libraries
#N/A
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = "1.0"
#Log File Info
$sLogPath = "C:\users\%username%\Documents" #TODO Fix %paths%
$sLogName = "import-PhoneNumbers.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
#-----------------------------------------------------------[Functions]------------------------------------------------------------
#####
#https://stackoverflow.com/questions/7834656/create-log-file-in-powershell
#####
Function Write-Log {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
[String]
$Level = "INFO",
[Parameter(Mandatory=$True)]
[string]
$Message,
[Parameter(Mandatory=$False)]
[string]
$logfile,
[Parameter(Mandatory=$False)]
[string]
$Guid
)
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$Line = "$Stamp $Level $Guid $Message"
If($slogfile) {
Add-Content $logfile -Value $Line
}
Else {
Write-host $Line
}
}
###
#end
###
Function import-RawPhoneNumbers{
Param(
[Parameter(Mandatory = $true)][string]$CSV,
[Parameter(Mandatory = $true)][string]$nameField,
[Parameter(Mandatory = $true)][string]$numberField
)
Begin{
$funcGuid = New-Guid
Write-Log -Level INFO -Guid $funcGuid -Message "Importing CSV To Powershell Object only containing Reletive Data" -logfile $sLogFile
}
Process{
Try{
$rawData = Import-Csv -Path $CSV
$selectedData = $rawData | select -Property "User Name","Wireless Number"
}
Catch{
Write-Log -Level FATAL -Guid $funcGuid -Message $_.Exception -logfile $sLogFile
Break
}
}
End{
If($?){
Write-Log -Level INFO -Guid $funcGuid -Message "Completed Successfully." -logfile $sLogFile
return $selectedData
}
}
}
Function export-PhoneNumbers{
Param(
[Parameter(Mandatory = $true)]$PhoneNumbers,
[Parameter(Mandatory = $false)]$exclutions
)
Begin{
$funcGuid = New-Guid
Write-Log -Level INFO -Guid $funcGuid -Message "Connection To O365..." -logfile $sLogFile
}
Process{
Try{
Write-Log -Level INFO -Guid $funcGuid -Message "Prompting For user Creds" -logfile $sLogFile
$UserCredential = Get-Credential
Write-Log -Level INFO -Guid $funcGuid -Message "Connecting..." -logfile $sLogFile
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Write-Log -Level INFO -Guid $funcGuid -Message "Importing The Session" -logfile $sLogFile
Import-PSSession $Session -DisableNameChecking
Write-Log -Level INFO -Guid $funcGuid -Message "Running Import" -logfile $sLogFile
############################################
#TODO
############################################
$PhoneNumbers = Compare-Object -ReferenceObject $PhoneNumbers -DifferenceObject $exclutions -PassThru -Property "User Name" | select -Property "User Name","Wireless Number"
}
Catch{
Write-Log -Level FATAL -Guid $funcGuid -Message $_.Exception -logfile $sLogFile
Break
}
}
End{
If($?){
Write-Log -Level INFO -Guid $funcGuid -Message "Completed Successfully." -logfile $sLogFile
return $selectedData
}
}
}
#-----------------------------------------------------------[Execution]------------------------------------------------------------
$progGuid = New-Guid
Write-Log -Level INFO -Guid $progGuid -Message "Starting Script" -logfile $sLogFile
$selectedData = import-RawPhoneNumbers
Write-Log -Level INFO -Guid $progGuid -Message "Script Successfully Finished" -logfile $sLogFile