{"id":2448,"date":"2012-12-16T04:07:50","date_gmt":"2012-12-16T04:07:50","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?p=2448"},"modified":"2012-12-16T04:07:50","modified_gmt":"2012-12-16T04:07:50","slug":"adding-and-removing-the-same-value-from-a-multivalued-property","status":"publish","type":"post","link":"https:\/\/www.wapshere.com\/missmiis\/adding-and-removing-the-same-value-from-a-multivalued-property","title":{"rendered":"Adding and removing the same value from a multivalued property"},"content":{"rendered":"<p>For some reason I had assumed that I wouldn&#8217;t be able to remove and re-add the same value to a multivalued property in the same request &#8211; but it turns out you can &#8211; and it helped me greatly simplify a script, so hooray for that!<\/p>\n<p><!--more-->So why did I want to do this anyway?<\/p>\n<p>I&#8217;m doing a service request design where people need entitlement objects to allow the appropriate service role to be linked to their profile. In case of any workflow failures or other weirdness I want a script to run periodically and just check everything&#8217;s lined up.<\/p>\n<p>I was mucking around with trying to compare entitlements to linked roles &#8211; but when I realised I can just remove all the current roles and re-add the ones I need in the same request it made the script a lot simpler.<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/12\/mvaddremove.jpg\" \/><\/p>\n<p>I then just need to add a count to check there actually are changes to upload &#8211; no point flooding the FIM service with requests that remove and re-add exactly the same values &#8211; and my script is done.<\/p>\n<p>Here&#8217;s the script. It uses the FIMPowerShell Function library from <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ff720152(v=ws.10).aspx\">http:\/\/technet.microsoft.com\/en-us\/library\/ff720152(v=ws.10).aspx<\/a><\/p>\n<pre>PARAM($Filter)\r\n### \r\n### Review-Entitlements.ps1\r\n###\r\n### Run overnight to confirm that people's service-roles match their entitlements.\r\n### Can be broken down by XPath filter to reduce the size of the query.\r\n###\r\n\r\n. E:\\scripts\\functions.ps1\r\n\r\nif (-not $Filter) {$Filter = \"\/Person\"}\r\n\r\n## Get all people according to the filter\r\n$People = Export-FIMConfig -CustomConfig $Filter -OnlyBaseResources\r\n\r\nforeach ($person in $People)\r\n{\r\n    $PersonID = ($person.ResourceManagementObject.ObjectIdentifier).Replace(\"urn:uuid:\",\"\")\r\n\r\n    $Changes = New-Object System.Collections.ArrayList  # Keeps track of the number of changes so we don't run unnecessary requests\r\n\r\n    $ImportObject = ModifyImportObject -TargetIdentifier $PersonID -ObjectType \"Person\"\r\n\r\n    $CurrentRoles = ($person.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq 'Applications'}).Values\r\n    if ($CurrentRoles) {foreach ($role in $CurrentRoles)\r\n    {\r\n        RemoveMultiValue -ImportObject $ImportObject -AttributeName \"Applications\" -NewAttributeValue $role\r\n        $Changes.Add($role)\r\n    }}\r\n\r\n    $Filter = \"\/Entitlement[PersonLink='\" + $PersonID + \"' and \" +\r\n                \"ExpirationTime &gt; op:subtract-dayTimeDuration-from-dateTime(fn:current-dateTime(), xs:dayTimeDuration('P1D')) and \" +\r\n                \"StartDate &lt; fn:current-dateTime() and \" +\r\n                \"RoleAttribute = 'Applications'\" +\r\n                \"]\"\r\n    $Entitlements = Export-FIMConfig -CustomConfig $Filter -OnlyBaseResources\r\n    if ($Entitlements) {foreach ($ent in $Entitlements)\r\n    {\r\n        $role = ($ent.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq 'RoleLink'}).Value\r\n        AddMultiValue -ImportObject $ImportObject -AttributeName \"Applications\" -NewAttributeValue $role\r\n        if ($Changes.Contains($role)) {$Changes.Remove($role)}\r\n        else {$Changes.Add($role)}\r\n    }}\r\n\r\n    if ($Changes.Count -gt 0) {Import-FIMConfig $ImportObject}\r\n}\r\n\r\n\u00c2\u00a0<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For some reason I had assumed that I wouldn&#8217;t be able to remove and re-add the same value to a multivalued property in the same request &#8211; but it turns out you can &#8211; and it helped me greatly simplify a script, so hooray for that!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[42,60,23],"tags":[],"class_list":["post-2448","post","type-post","status-publish","format-standard","hentry","category-fim-2010","category-fim-2010-r2","category-powershell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pkp1o-Du","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/comments?post=2448"}],"version-history":[{"count":9,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2448\/revisions"}],"predecessor-version":[{"id":2459,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2448\/revisions\/2459"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=2448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/categories?post=2448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/tags?post=2448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}