在 PowerShell 中使用 -notlike 过滤掉多个字符串 [英] Use -notlike to filter out multiple strings in PowerShell

查看:97
本文介绍了在 PowerShell 中使用 -notlike 过滤掉多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为除两个用户之外的所有用户读取事件日志以进行安全审核,但是否可以使用 -notlike 运算符来执行此操作?

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?

就是这样:

Get-EventLog -LogName Security | where {$_.UserName -notlike @("*user1","*user2")}

我让它为单个用户工作,例如:

I have it working for a single user, like:

Get-EventLog -LogName Security | where {$_.UserName -notlike "*user1"}

推荐答案

V2 至少包含 -username 参数,该参数采用 string[],并支持 globbing.

V2 at least contains the -username parameter that takes a string[], and supports globbing.

V1 你想像这样扩展你的测试:

V1 you want to expand your test like so:

Get-EventLog Security | ?{$_.UserName -notlike "user1" -and $_.UserName -notlike "*user2"}

或者您可以在内联数组上使用-notcontains",但这只有在您可以对用户名进行精确匹配时才有效.

Or you could use "-notcontains" on the inline array but this would only work if you can do exact matching on the usernames.

<代码>... |?{@("user1","user2") -notcontains $_.username}

这篇关于在 PowerShell 中使用 -notlike 过滤掉多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆