How to Use Powershell to Create Manager-Based Distribution Lists from a CSV File (FIM 2010)

How to Use Powershell to Create Manager-Based Distribution Lists from a CSV File (FIM 2010)

FIM ScriptBox Item

Summary

Create Manager-based Distribution Lists from a CSV file.

The CSV file must include a header row, such as in the following example:

DisplayName,MailNickname,Description,Manager
DL-Hongs Team,HHanTeam,Hong Han's team,hhan

Script Code

rg

 

001
002
003
004
005 006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
PARAM($CSVFile, $Domain, $Scope = "Universal", $Type = "Distribution", $Owner = "Administrator")
#----------------------------------------------------------------------------------------------------------
 set-variable -name URI -value "http://fim:5725/resourcemanagementservice"
 set-variable -name MEMBERSHIPLOCKED -value $true
 set-variable -name PREFILTER -value "<Filter xmlns:xsi=`"http://www.w3.org/2001/XMLSchema-instance`" xmlns:xsd=`"http://www.w3.org/2001/XMLSchema`" Dialect=`"http://schemas.microsoft.com/2006/11/XPathFilterDialect`" xmlns=`"http://schemas.xmlsoap.org/ws/2004/09/enumeration`">"
 set-variable -name POSTFILTER -value "</Filter>"
#----------------------------------------------------------------------------------------------------------
 function SetAttribute
 {
    PARAM($object, $attributeName, $attributeValue)
    END
    {
        write-host $attributeName $attributeValue
        $importChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
        $importChange.Operation = 1
        $importChange.AttributeName = $attributeName
        $importChange.AttributeValue = $attributeValue
        $importChange.FullyResolved = 1
        $importChange.Locale = "Invariant"
        if ($object.Changes -eq $null) {$object.Changes = (,$importChange)}
        else {$object.Changes += $importChange}
    }
} 
#----------------------------------------------------------------------------------------------------------
 function CreateObject
 {
    PARAM($objectType)
    END
    {
       $newObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
       $newObject.ObjectType = $objectType
       $newObject.SourceObjectIdentifier = [System.Guid]::NewGuid().ToString()
       $newObject
     } 
 }
#----------------------------------------------------------------------------------------------------------

if(@(get-pssnapin | where-object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {add-pssnapin FIMAutomation}

# Get Owner
$ownerObject = export-fimconfig -uri $URI `
                                –onlyBaseResources `
                                -customconfig "/Person[AccountName='$OWNER']"
if($ownerObject -eq $null) {throw "Owner not found!"} 
$ownerID = $ownerObject.ResourceManagementObject.ObjectIdentifier -replace "urn:uuid:",""


# Read CSV file and process each line
import-csv($CSV) | foreach {

 # Check if a group with the same name already exists
 $objectName = $_.DisplayName
 $exportObject = export-fimconfig -uri $URI `
                                  –onlyBaseResources `
                                  -customconfig "/Group[DisplayName='$objectName']"
 if($exportObject) {write-host "`nGroup $objectName already exists"}
 else
  {
  # Get Manager
  $manager = $_.Manager
  $managerObject = export-fimconfig -uri $URI `
                                –onlyBaseResources `
                                -customconfig "/Person[AccountName='$manager']"
  if($managerObject -eq $null) {write-host "`nManager $manager not found"} 
  $managerID = $managerObject.ResourceManagementObject.ObjectIdentifier -replace "urn:uuid:",""

                                                              
  # Construct group Criteria Filter
  $filter = $PREFILTER + "/Person[(Manager = '" + $managerID + "') or " `
            + "(ObjectID ='" + $managerID + "')]" + $POSTFILTER                                

  # Create group object and set attributes
  $newGroup = CreateObject -objectType "Group"
  SetAttribute -object $newGroup -attributeName "DisplayName" -attributeValue $objectName
  SetAttribute -object $newGroup -attributeName "MailNickname" -attributeValue $_.MailNickname
  SetAttribute -object $newGroup -attributeName "Domain" -attributeValue $DOMAIN
  SetAttribute -object $newGroup -attributeName "Scope" -attributeValue $SCOPE
  SetAttribute -object $newGroup -attributeName "Type" -attributeValue $TYPE
  SetAttribute -object $newGroup -attributeName "Filter" -attributeValue $filter
  SetAttribute -object $newGroup -attributeName "Description" -attributeValue $_.Description
  SetAttribute -object $newGroup -attributeName "Owner" -attributeValue $ownerID
  SetAttribute -object $newGroup -attributeName "DisplayedOwner" -attributeValue $ownerID
  SetAttribute -object $newGroup -attributeName "MembershipLocked" -attributeValue $MEMBERSHIPLOCKED
  SetAttribute -object $newGroup -attributeName "MembershipAddWorkflow" -attributeValue "None"
  
  # Import group object into FIM
  $newGroup | Import-FIMConfig -uri $URI
  write-host "`nGroup creation request complete`n"
  }
 }

 

note Note
To provide feedback about this script, create a post on the FIM TechNet Forum.
For more FIM related Windows PowerShell scripts, see the FIM ScriptBox.

 

Leave a Comment
  • Please add 3 and 7 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Peter Geelen - MSFT edited Revision 6. Comment: Fixed color scheme (rgb to color name)

  • Richard Mueller edited Revision 5. Comment: Replaced RGB values with color names in HTML to restore colors

  • Richard Mueller edited Revision 4. Comment: Removed (en-US) from title

  • Craig Lussier edited Revision 3. Comment: added en-US to tags and title

  • Ed Price MSFT edited Revision 1. Comment: Updated title to standards (start with How to and shouldn't use gerunds).

Page 1 of 1 (5 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Ed Price MSFT edited Revision 1. Comment: Updated title to standards (start with How to and shouldn't use gerunds).

  • Craig Lussier edited Revision 3. Comment: added en-US to tags and title

  • Richard Mueller edited Revision 4. Comment: Removed (en-US) from title

  • Richard Mueller edited Revision 5. Comment: Replaced RGB values with color names in HTML to restore colors

  • Peter Geelen - MSFT edited Revision 6. Comment: Fixed color scheme (rgb to color name)

  • Code is cropped out.

  • Code is cropped out.

Page 1 of 1 (7 items)