MIM 2016: Use Lithnet PS to copy over values from a multivalue reference field to single-value reference field

I assume you know Lithnet PS well else go over to github and get started. For this post here is what I want to do

  1. I am using a multi-value reference attribute “mymulti” which is bound to the Person Object. I have populated some of them with values.
  2. I want to now use a Single value attribute to store the data. I have actually been storing single values in mymulti so really was a waste of a multi. I will create a new single value reference attribute called “mysingle1”. Bind it to Person.
  3. I want to copy the values in mymulti to mysingle1.

I have done 1 and 2. Now for 3, I will use Lithnet PS

$mycurrentvalues = search-resources -Xpath “/Person[(mymulti = /*)]” -AttributesToGet mymulti,mysingle1

ForEach ($mycurrentvalue in $mycurrentvalues){

$mycurrentvalue.mysingle1=$mycurrentvalue.mymulti.value

save-resource $mycurrentvalue

}

Lets look at a Use Case where you have multiple items in mymulti1, here is the logic we want to use.

  1. If there is more than one in mymulti then take the 2nd item
  2. If there is more than one in mymulti, loop through and search for Objectid “1112355”, if found take that else take item 2.

Yes it can get a bit complex when there are multiple entries, finding the logic for the single entry selected is the big task.

$mycurrentvalues = search-resources -Xpath “/Person[(mymulti = /*)]” -AttributesToGet mymulti,mysingle1

$multiIndex = 0

ForEach ($mycurrentvalue in $mycurrentvalues){

If ($mycurrentvalue.mymulti.count -gt 1){

#loop and check for objectid

[Bool]$foundmyvalue=$false

For($multiindex=0;$multiIndex -lt $mycurrentvalue.mymulti.count;$multiIndex++){

If ($mycurrentvalue.mymulti[$multiIndex].value -eq “1112355”){

$mycurrentvalue.mysingle1=$mycurrentvalue.mymulti[$multiIndex].value

save-resource $mycurrentvalue

$Foundmyvalue=$true

}

If($foundmyvalue = false){

$mycurrentvalue.mysingle1=$mycurrentvalue.mymulti[1].value

}

}

}

If ($mycurrentvalue.mymulti.count -eq 1){

$mycurrentvalue.mysingle1=$mycurrentvalue.mymulti.value

save-resource $mycurrentvalue

}

}