Remove Values from MultiValue Attribute
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
<# | |
.SYNOPSIS | |
Script to remove values from MultiValue attribute based on search criteria. | |
#> | |
PARAM( | |
[string]$MultiValueAttribute = 'emailAlias', | |
[ValidateSet("StartsWith","EndsWith","Equals")] | |
[string]$SearchType, | |
[string]$SearchValue = '@domain.onmicrosoft.com', | |
[string]$ResourceType = 'Person', | |
[string]$FIMServiceURI = 'http://localhost:5725' | |
) | |
#region Lithnet | |
if(!(Get-Module -Name LithnetRMA)) | |
{ | |
Import-Module LithnetRMA; | |
} | |
Set-ResourceManagementClient -BaseAddress $FIMServiceURI; | |
#endregion Lithnet | |
#Build the query | |
$XPathQuery = New-XPathQuery -AttributeName $MultiValueAttribute -Operator $SearchType -Value $SearchValue | |
$XPathExpression = New-XPathExpression -ObjectType $ResourceType -QueryObject $XPathQuery | |
#Get the Objects | |
$Objects = Search-Resources $XPathExpression -AttributesToGet $MultiValueAttribute | |
#Remove the values from each Object | |
foreach ($Object in $Objects) | |
{ | |
$ValuesToRemove = @() | |
$Values = ($Object.psobject.Properties | ?{$_.Name -eq $MultiValueAttribute}).Value | |
switch($SearchType) | |
{ | |
"StartsWith"{$ValuesToRemove = $Values | ?{$_ -like ($SearchValue + '*')}} | |
"EndsWith"{$ValuesToRemove = $Values | ?{$_ -like ('*' + $SearchValue)}} | |
"Equals"{$ValuesToRemove = $Values | ?{$_ -eq $SearchValue}} | |
} | |
foreach($Value in $ValuesToRemove){($Object.psobject.Properties | ?{$_.Name -eq $MultiValueAttribute}).Value.Remove($Value)} | |
$Object | Save-Resource | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment