Author Posts

June 18, 2015 at 6:11 am

I'm trying to check against a list of group names (with associated text in secondary column) against a users group memberships
input file would be 2 columns name,output
ad group name in the first column, and some output text in the second.
i can-not get -compare to work, it doesn't return anything.
if i chang my list to put * before and after the group name in the input file i can use -like but this isn't very nice.
what am i doing wrong with -contains

$sam = Read-Host 'SamAccountName to check app groups?'
$applist  = $path + "\applist.csv"
$user = Get-ADUser $sam -Properties memberof|select -expand memberof
$csv = import-csv $applist

foreach($app in $csv)
{
if ($user -contains $app.name)
{
Write-Host "$($app.output) is the action"
}
}

June 18, 2015 at 6:21 am

-contains doesn't do wildcard matching, and the MemberOf field in AD contains distinguished names. If your $app.name field was also a distinguished name, it would probably work.

Try this instead, and see if it does the trick:

if ($user -like "*$($app.name)*")

In this case, because $user is an array, -like will function more like a filter (returning an array with all of the elements that matched the pattern). However, this is still fine for an If statement, since an empty array means $false, and if one or more strings are found that match the pattern, then it'll evaluate to $true.

June 18, 2015 at 6:45 am

Thanks Dave, it was the "*$($app.name)*"

i'd tried "*$app.name*" and got nowhere.

much appreciated