Creating a dynamic distribution list using an imported CSV file

Welcome Forums General PowerShell Q&A Creating a dynamic distribution list using an imported CSV file

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
2 weeks ago.

  • Author
    Posts
  • #113336

    Participant
    Points: 0
    Rank: Member

    I have a CSV file that has a repeated job code number and various office location numbers. I am wanting to create a dynamic distribution list with this information. I have included a screenshot below. 

     

    I would like there to be parameters that would ask questions such as ListName, Alias and then have it use the imported CSV file as the RecipientFilter criteria. Is this able to be done?

  • #113377

    Participant
    Points: 20
    Rank: Member

    Yes, all that is doable. Did you try anything or are you expecting any ready made script ?
    If you have given it a try, post the script with the error/difficulty you are facing, else please give it a try.

  • #113389

    Participant
    Points: 0
    Rank: Member

    Thanks for the reply kvprasoon.

    Yes, I tried the following code:

     

     

    Param (
    [Parameter(Mandatory=$True,Position=0)][string]$ConfigFile = $(throw '- Need parameter input file (e.g. "c:\users.csv")'),
    [Parameter(Mandatory=$True,HelpMessage='Enter the Name of DG')][string]$DGName,
    [Parameter(Mandatory=$True,HelpMessage='Enter the Alias')][string]$aliasDG,
    [Parameter(Mandatory=$True,HelpMessage='Enter the JobCode')][string]$jobcode
    )
    $Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size (500, 300)
    $ScriptPath = $MyInvocation.MyCommand.Path
    $ScriptDir  = Split-Path -Parent $ScriptPath
    $Computer = $env:computername
    $TimeStamp = (Get-Date).ToString('_yyyyMMdd-HHmmss')
    start-transcript -path $ScriptDir\GoodSam.CreateWebContactGroups_$TimeStamp.txt
    $csvinput = import-csv $ConfigFile 
    ####End Common Setup ####
    
    foreach ($line in $csvinput)
    {
    $center=$line.CenterNumber
    $group=New-DynamicDistributionGroup -Name $DGName -Alias $aliasDG -RecipientFilter {((RecipientType -eq 'CenterNumber') -and (CustomAttribute1 -eq '$jobcode')) }
    set-dynamicdistributiongroup -identity $group -HiddenFromAddressListsEnabled $true -RequireSenderAuthenticationEnabled $false
    
    }
    

    The code fails and does not generate a Dynamic Group.

  • #113413

    Participant
    Points: 20
    Rank: Member

    Well, here are some comments.

    • Parameter $ConfgFile is declared as mandatory, but you have a default value of a throw statement(hope you kept it for custom error message). Since the Parameter is mandatory, PowerShell is not gonna consider the default value to throw the custom error if no value is provided, so you can make mandatory to $false
    • You have CenterNumber picked from CSV and assigned to $center variable but, 'CenterNumber' is what you have used for filtering.
    • $JobCode variable is inside single quotes, hence it will get treates as pure string than a variable, quotes are not required, see more at about_quoting_rules

    You must be logged in to reply to this topic.