7

I have two arrays

  • $adg - (A list of AD groups)

  • $dbs - (A list of database names)

Lets say I use this command $adg -match $dbs.i

The output will be all the AD groups which have the string $dbs in its name.

However, I am aiming to find the DBs in which are not part of the AD groups array.

Eg:

$adg = @("Dev22_Owner","Test49_Owner","Rocket_Owner")
$dbs = @("Dev22", "Confidential", "InternDB", "Rocket", "Test49")

What approach should I take to get the output:

Confidential

InternDB

I tried $dbs | Where $adg -notmatch $dbs.i but there is no output.

2 Answers 2

8

I would first remove the unecessary user part from the ad group:

$groups = $adg | ForEach-Object {
    $_ -replace '_.*?$'
}

Then you can use the Where-Object cmdlet with the -notin operator to filter them:

$dbs | Where-Object { $_ -notin $groups }

Output:

Confidential
InternDB
Sign up to request clarification or add additional context in comments.

Comments

1

To offer a more concise PSv3+ alternative to Martin Brandl's helpful answer:

PS> (Compare-Object ($adg -replace '_Owner$') $dbs).InputObject
Confidential
InternDB
  • ($adg -replace '_Owner$') returns a new array with copies of the input string that have _Owner suffix stripped from them.

  • Compare-Object compares the stripped array with the other array and returns objects that represent the differences between the two arrays.

  • Accessing the .InputObject on the difference objects returns the values that differ.

    • Note: If $dbs contained items that aren't in the stripped $adg array, the above would return a union of all differing items, irrespective of which side they're unique to; to distinguish the sides / limit to a side, you'd have to use the .SideIndicator property (value => indicates values exclusive to the right side, <= those exclusive to the left side).

As for what you tried:

$adg -match $dbs.i doesn't work as intended and is effectively the same as $adg -match '', which invariably returns all $adg items.

The reason is that array $dbs has no .i property, so the expression evaluates to $null, which on the RHS of match is coerced to a string and therefore is converted to the empty string.

Generally, the RHS of -match does not support arrays - only a single regular expression.

If you do supply an array, it is implicitly converted to a string by joining the elements to form a space-separated list; e.g. array 1, 2 is coerced to '1 2' and '1 2' -match (1, 2) therefore evaluates to $True.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.