TeamsNumbers2ADAttribute.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 |
<#PSScriptInfo .VERSION 1.1 .GUID a4dc9242-8599-49e9-b368-7e174932f4f6 .AUTHOR Vikas Sukhija .COMPANYNAME TechWizard.cloud .COPYRIGHT Vikas Sukhija .TAGS .LICENSEURI .PROJECTURI https://techwizard.cloud/2020/09/08/microsoft-teams-number/ .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES https://techwizard.cloud/2020/09/08/microsoft-teams-number/ .PRIVATEDATA #> <# .DESCRIPTION This will read teams assigned numbers and add to AD attribute #> param ( [Parameter(Mandatory = $true)] [string]$domain, [Parameter(Mandatory = $true)] [string]$ADattribute, [Parameter(Mandatory = $true)] [ValidateSet('True','False')] [string]$reportonly, [Parameter(Mandatory = $true)] [int]$countofchanges ) function New-FolderCreation { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$foldername ) $logpath = (Get-Location).path + "\" + "$foldername" $testlogpath = Test-Path -Path $logpath if($testlogpath -eq $false) { $null = New-Item -Path (Get-Location).path -Name $foldername -Type directory } }####new folder creation function Write-Log { [CmdletBinding()] param ( [Parameter(Mandatory = $true,ParameterSetName = 'Create')] [array]$Name, [Parameter(Mandatory = $true,ParameterSetName = 'Create')] [string]$Ext, [Parameter(Mandatory = $true,ParameterSetName = 'Create')] [string]$folder, [Parameter(ParameterSetName = 'Create',Position = 0)][switch]$Create, [Parameter(Mandatory = $true,ParameterSetName = 'Message')] [String]$message, [Parameter(Mandatory = $true,ParameterSetName = 'Message')] [String]$path, [Parameter(Mandatory = $false,ParameterSetName = 'Message')] [ValidateSet('Information','Warning','Error')] [string]$Severity = 'Information', [Parameter(ParameterSetName = 'Message',Position = 0)][Switch]$MSG ) switch ($PsCmdlet.ParameterSetName) { "Create" { $log = @() $date1 = Get-Date -Format d $date1 = $date1.ToString().Replace("/", "-") $time = Get-Date -Format t $time = $time.ToString().Replace(":", "-") $time = $time.ToString().Replace(" ", "") New-FolderCreation -foldername $folder foreach ($n in $Name) {$log += (Get-Location).Path + "\" + $folder + "\" + $n + "_" + $date1 + "_" + $time + "_.$Ext"} return $log } "Message" { $date = Get-Date $concatmessage = "|$date" + "| |" + $message +"| |" + "$Severity|" switch($Severity){ "Information"{Write-Host -Object $concatmessage -ForegroundColor Green} "Warning"{Write-Host -Object $concatmessage -ForegroundColor Yellow} "Error"{Write-Host -Object $concatmessage -ForegroundColor Red} } Add-Content -Path $path -Value $concatmessage } } } #Function Write-Log function LaunchSOL { param ( [Parameter(Mandatory = $true)] $domain, [Parameter(Mandatory = $false)] $Credential ) Write-Host -Object "Enter Skype Online Credentials" -ForegroundColor Green $dommicrosoft = $domain + ".onmicrosoft.com" $CSSession = New-CsOnlineSession -Credential $Credential -OverrideAdminDomain $dommicrosoft Import-Module (Import-PSSession -Session $CSSession -AllowClobber) -Prefix SOL -Global } #Function LaunchSOL Function RemoveSOL { $Session = Get-PSSession | Where-Object -FilterScript { $_.ComputerName -like "*.online.lync.com" } Remove-PSSession $Session } #Function RemoveSOL #####################logs and reports################### $log = Write-Log -Name "TeamsNumber2ADAttribute-Log" -folder "logs" -Ext "log" $Report1 = Write-Log -Name "TeamsNumber2ADAttribute-Report" -folder "Report" -Ext "csv" $collection = @() $credential = Get-Credential ######connect to Skob and import modules ################################### Write-Log -message "Start..................Script" -path $log try { LaunchSOL -Domain $domain -Credential $credential Import-Module Activedirectory Connect-AzureAD -Credential $credential Write-Log -Message "Connected to SKOBOnline and loaded AD module" -path $log } catch { $exception = $($_.Exception.Message) Write-Log -Message "$exception" -path $log -Severity Error Write-Log -Message "Exception has occured in connecting to SOL or loading AD module" -path $log -Severity Error Exit; } ##################start processing users################## try { $allvoiceusers = Get-SOLCsOnlineVoiceUser -First 100000 | Select-Object Name, id, Number, LicenseState, PSTNConnectivity, UsageLocation, EnterpriseVoiceEnabled $allvoiceusers = $allvoiceusers | where{$_.number -ne $null} Write-Log -message "Fetched $($allvoiceusers.count) from Teams" -path $log } catch { $exception = $($_.Exception.Message) Write-Log -Message "$exception" -path $log -Severity Error Write-Log -Message "Exception has occured getting details from TEAMS" -path $log -Severity Error Exit; } ##########ADD UPN to the Collected numbers using azureAD and work with onpremAD####### Write-Log -message "Fetching and comparing All voice user numbers against AD" -path $log $tcount=0 ForEach($voiceuser in $allvoiceusers) { $mcoll = "" | Select-Object Name, id, UPN, samaccountname, Number,ExistingNumberinAD, PSTNConnectivity, UsageLocation, EnterpriseVoiceEnabled, Status $error.clear() $azuser = $null $getaduser = $null $mcoll.Name = $voiceuser.Name $mcoll.id = $voiceuser.id $azuser = Get-AzureADUser -ObjectId $voiceuser.id $mcoll.UPN = $azuser.UserPrincipalName Write-Verbose -Message "Processing user..........$($azuser.UserPrincipalName)" $voicenumber = "+" + $voiceuser.Number $mcoll.Number = $voicenumber $getaduser = get-aduser -filter {UserPrincipalName -eq $azuser.UserPrincipalName } -properties $ADattribute if($getaduser){ $samaccountname = $getaduser.samaccountname $mcoll.samaccountname = $samaccountname $status=$null if($getaduser.($ADattribute) -eq $voicenumber) { #Write-Log -message "Both Numbers match for $($azuser.UserPrincipalName)" -path $log $status = "Match" $mcoll.Status = $status $mcoll.ExistingNumberinAD = $getaduser.($ADattribute) } else { #Write-Log -message "Updaing $($azuser.UserPrincipalName) - samaccountname $samaccountname - Number $voicenumber" -path $log $status = "UpdateNumber" $mcoll.Status = $status $mcoll.ExistingNumberinAD = $getaduser.($ADattribute) } } else{ $mcoll.samaccountname = "Not Found" $mcoll.Status = "Not Found" $mcoll.ExistingNumberinAD = "Not Found" } $mcoll.PSTNConnectivity = $voiceuser.PSTNConnectivity $mcoll.UsageLocation = $voiceuser.UsageLocation $mcoll.EnterpriseVoiceEnabled = $voiceuser.EnterpriseVoiceEnabled $collection += $mcoll $tcount = $tcount +1 Write-Progress -Activity "Comparing $samaccountname" -status "$status" -percentComplete ( $tcount/$($allvoiceusers.count)*100) } if($reportonly -eq "False"){ $updatenumbers = $collection | where {$_.Status -eq "UpdateNumber"} if(($updatenumbers.samaccountname.count -gt 0) -and ($updatenumbers.samaccountname.count -lt $countofchanges)){ foreach($n in $updatenumbers){ try{ $samaccountname = $n.samaccountname $vnumber = $n.number Set-aduser -identity $samaccountname -Replace @{$ADattribute = "$vnumber"} Write-Log -message "Updaing amaccountname $samaccountname - Number $vnumber" -path $log } catch{ $exception = $($_.Exception.Message) Write-Log -Message "$exception" -path $log -Severity Error Write-Log -Message "Exception has occured setting number " -path $log -Severity Error } } } elseif($updatenumbers.samaccountname.count -ge $countofchanges){ Write-Log -Message "Error has occured setting number - count of changes $($updatenumbers.count) is more than $countofchanges" -path $log -Severity Error } } $collection | Export-Csv $Report1 -NoTypeInformation #########################close sessions and Recycle logs####################### RemoveSOL Disconnect-AzureAD Write-Log -message "Finish..................Script" -path $log ############################################################################################# |