Last active
March 19, 2019 13:43
-
-
Save lucashalbert/fced05645923a0a4bc358b0140449e7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Author: Lucas Halbert <contactme@lhalbert.xyz> | |
Date Written: 08/18/2017 | |
Date Modified: 08/31/2017 | |
Version: 0.3 | |
Description: Imports a CSV file with names and emails into a specified distribution group | |
Revision History: | |
08/31/2017 c0.3 - Add Try/Catchs and logging to script. | |
08/30/2017 v0.2 - Add Header option Import-Csv. | |
Fix quotation problems with Write-Output | |
08/18/2017 v0.1 - Inital Draft | |
#> | |
# Gather paramters | |
param( | |
[Parameter(Mandatory=$true)][string]$CSVFile, | |
[Parameter(Mandatory=$true)][string]$DistributionGroup | |
) | |
# Variables | |
$DistributionGroupOU="OU=DG,OU=Contacts,DC=domain,DC=tld" | |
$LogFile="C:\temp\PopulateDistributionGroup-$(Get-Date -Format "yyyyMMdd-HHmm").log" | |
# Check if CSVFile exists | |
if (!(Test-path $CSVFile)) { | |
throw [System.IO.FileNotFoundException] "$(Get-Date): ERROR: File not found. ($CSVFile)" | Out-File $LogFile -Append | |
} | |
# Check if DistributionGroup exists | |
if (!(Get-DistributionGroup -Identity $DistributionGroup)) { | |
throw "$(Get-Date): ERROR: DistributionGroup not found. ($DistributionGroup)" | Out-File $LogFile -Append | |
} | |
# Get all members of DistributionGroup | |
$members=Get-DistributionGroupMember -Identity $DistributionGroup | select -expand EmailAddresses | |
# Perform import of CSVFile to DistributionGroup | |
Import-Csv $CSVFile -Header NAME,EMAILADDRESS | ForEach-Object { | |
# Check if contact already exists. If not, create new contact | |
Try | |
{ | |
# Check if contact already exists | |
Get-Contact -Identity $_.EMAILADDRESS -ErrorAction Stop | Out-File $LogFile -Append | |
"$(Get-Date): INFO: $_.EMAILADDRESS is already a contact" | Out-File $LogFile -Append | |
} | |
Catch | |
{ | |
Try | |
{ | |
# Create new mail contact in OU | |
New-MailContact -OrganizationalUnit $DistributionGroupOU -Name $_.NAME -ExternalEmailAddress $_.EMAILADDRESS -ErrorAction Stop | |
"$(Get-Date): SUCCESS: Contact has been created for$_.EMAILADDRESS" | Out-File $LogFile -Append | |
} | |
Catch | |
{ | |
"$(Get-Date): ERROR: Failed to create contact for $_.EMAILADDRESS" | Out-File $LogFile -Append | |
} | |
} | |
# Check if contact is a member of distribution group. if not, add member to distribution group | |
if ($members -contains $_.EMAILADDRESS) { | |
"$(Get-Date): INFO: $_.EMAILADDRESS is already a member of $DistributionGroup" | Out-File $LogFile -Append | |
} else { | |
Try | |
{ | |
Add-DistributionGroupMember -Identity $DistributionGroup -Member $_.EMAILADDRESS -ErrorAction Stop | |
"$(Get-Date): SUCCESS: $_.EMAILADDRESS has been add to $DistributionGroup" | Out-File $LogFile -Append | |
} | |
Catch | |
{ | |
"$(Get-Date): ERROR: Failed to add $_.EMAILADDRESS to $DistributionGroup" | Out-File $LogFile -Append | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment