0

I want to compare if one string (f.e. Edi) contains in another string (f.e. Edil). I wrote a script with out-string, because later I need to compare two obejcts (all users from AD with accounts from NTFSsecurity (permission for folders)).

$AlleBenutzer = ("Adam","Edi", "Georg","Kate")
$Benutzer = ("Adaml", "Edil", "Eval")
foreach ($u in $AlleBenutzer) {$L = $u | Out-String ; write-host $L}
foreach ($v in $Benutzer) {$M = $v | Out-String; write-host $M}

foreach ($user in $L) {if ($M -match $user ) {Write-Host $M}}

I become output from line 3 and 4, but no output from line 6. I don't see my mistake.

1 Answer 1

1

You don't need Out-String here - your array values are already strings. You can run the list of substrings (user names) through a nested set of Where-Object pipelines to test them all:

$AlleBenutzer = ("Adam","Edi", "Georg","Kate")
$Benutzer = ("Adaml", "Edil", "Eval")

$AlleBenutzer |Where-Object {
  $user = $_
  # If any value in $Benutzer matches the user name, 
  # this expression will evaluate to _something_, 
  # iow. $true in the outer Where-Object 
  $Benutzer |Where-Object {$_ -like "*$user*"}
}
Sign up to request clarification or add additional context in comments.

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.