Skip to content

Instantly share code, notes, and snippets.

@KentNordstrom
Last active November 12, 2018 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KentNordstrom/6d73537f6ffc4fdddf1f7356d3603106 to your computer and use it in GitHub Desktop.
Save KentNordstrom/6d73537f6ffc4fdddf1f7356d3603106 to your computer and use it in GitHub Desktop.
Remove Values from MultiValue Attribute
<#
.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