Use -notlike to filter out multiple strings in PowerShell

后端 未结 9 504
执念已碎
执念已碎 2020-12-14 15:58

I\'m trying to read the event log for a security audit for all users except two, but is it possible to do that with the -notlike operator?

It\'s somethi

9条回答
  •  眼角桃花
    2020-12-14 16:36

    $listOfUsernames = @("user1", "user2", "etc", "and so on")
    Get-EventLog -LogName Security | 
        where { $_.Username -notmatch (
            '(' + [string]::Join(')|(', $listOfUsernames) + ')') }
    

    It's a little crazy I'll grant you, and it fails to escape the usernames (in the unprobable case a username uses a Regex escape character like '\' or '(' ), but it works.

    As "slipsec" mentioned above, use -notcontains if possible.

提交回复
热议问题