#CDV2EXO # # Simple Skript to import CSV-Based contacts into exchange Online with Exchange Online PowerShell V2 # requires an application https://www.msxfaq.de/cloud/exchangeonline/betrieb/exo_powershell_automation.htm # Pending # - Update existing contact if required # - Ignore existing Guest account # - Performance Counter # - Logging in Logfile # - Displayname übernehmen # - Export-Funktion # WorkworceIR in CustomAtribute13 als matching??? param ( $csvfile = ".\csv2exo.csv", $delimiter = ",", $encoding = "utf8", $CertificateThumbPrint ="815E2AD820B297DF4ED8D278CCE2D06EDAE35267", $AppID ="9cb958d8-3f03-4f5e-b71a-8ae25a289b78", $Organization = "msxfaqdev.onmicrosoft.com", $keyproperty = "ExtensionCustomAttribute5", $keyvalue = "A5" ) Write-Host "CDV2EXO: Start" $error.clear() ######################################################### write-host "############# Importing and validating CSV-File ############" ######################################################## #region CSVImport Write-Host "CDV2EXO: CSV-File Try to import $($csvfile)" if (!(Test-Path -Path $csvfile -PathType leaf)) { Write-error "Unable to find CSV-File at $($csvfile)" exit 1 } [hashtable]$csvtable = @{} import-csv $csvfile -Delimiter $delimiter -Encoding $encoding | ForEach-Object { $mail = $_.mail.tolower().replace("smtp:","") Write-host "Processing Entry $($mail)" $csvtable.Add($mail,$_) } Write-Host " CSV Import done" Write-Host "Total lines: $($csvtable.count)" #endregion ################################################## write-host "############# Connecting Exchange Online #############" ################################################ Write-Host "Connecting new Exchange Online Session" Connect-ExchangeOnline ` -CertificateThumbPrint $CertificateThumbPrint ` -AppID $AppID ` -Organization $Organization ` -ShowBanner:$false Write-Host "Exchange Online Connection OK" ################################################### write-host "############# Collect contacts ###########" ################################################## $command = "get-mailcontact -filter {$($keyproperty) -eq ""$($keyvalue)""} -resultsize unlimited" Invoke-Expression -Command $command | ForEach-Object { Write-host "Processing EXO: $($_.ExternalEmailAddress)" $mail = $_.ExternalEmailAddress.tolower().replace("smtp:","") if ($csvtable.ContainsKey($mail)) { Write-host "existing contact found - Skip. check for Updated properties not yet implemented" -ForegroundColor Green $csvtable.remove($mail) } else { Write-host "Orphaned Contact -Remove in EXO" -ForegroundColor Magenta remove-mailcontact -identity $mail -confirm:$false } } ########################################### write-host "############# Adding new contacts ##########" ########################################### foreach ($csvkey in $csvtable.keys) { Write-Host "Adding new contact $($csvkey)" -ForegroundColor Cyan New-Mailcontact ` -Name "$($csvtable[$csvkey].name)" ` -ExternalEmailAddress "$($csvtable.item($csvkey).mail)" Write-Host "Updating new contact $($csvkey)" Write-Host " Update Keyproperty $($keyproperty) = $($keyvalue)" -backgroundColor blue $command = "Set-MailContact -identity $($csvtable.item($csvkey).mail) -$($keyproperty) $($keyvalue)" Invoke-Expression -Command $command } Write-Host "Remove existing Exchange Online Session" Disconnect-ExchangeOnline -Confirm:$false Write-host "CDV2EXO: End"