Powershell:根据属性过滤属性 [英] Powershell: Filtering Properties Against properties
问题描述
我在PowerShell方面的经验有限,自学成才,所以这可能是基本知识,但我似乎做得不好。
I have limited, self-taught experience with PowerShell so this is probably something basic but I can't seem to get it right.
我处于活动状态目录,我需要提取电子邮件地址不是以其SamAccountName开头的用户的列表。
(因此,如果您的登录名是jdoe,但是您的电子邮件地址是johndoe@mycompany.com,那么您的个人资料将被退回)
I'm in Active Directory and I need to pull a list of users who's email address doesn't start with their SamAccountName. (So if your login is jdoe but your email is johndoe@mycompany.com then your profile would be returned)
需要...但我不知道如何将这两个属性相互比较。
I've got most of what I need...but I can't figure out how to compare the two properties against eachother.
现在我有
Get-ADUser -Filter 'enabled -eq $true' -Properties *|
Where {$_.PasswordNeverExpires -eq $false} |
Select Name, SamAccountName, EmailAddress, PasswordNeverExpires
我尝试了一些不同的操作过滤我需要的内容,以下命令将确切显示我想要的内容(但是此语法当然不起作用)
I've tried a few different things to filter what I need, the following command shows exactly what I want (but of course this syntax doesn't work)
Get-ADUser -Filter 'enabled -eq $true' -Properties *|
Where {$_.PasswordNeverExpires -eq $false} |
Where-Object EmailAddress -Contains SamAccountName |
Select Name, SamAccountName, EmailAddress, PasswordNeverExpires
谢谢!
推荐答案
对第二个管道元素中的 Where-Object
过滤器使用脚本块:
Use a scriptblock for the Where-Object
filter like in your second pipeline element:
Where-Object { $_.EmailAddress -notlike "$($_.SamAccountName)*" }
您甚至可以使用-和
运算符将其与第一个过滤器组合:
You can even combine it with the first filter, using the -and
operator:
Where-Object { $_.PasswordNeverExpires -eq $false -and $_.EmailAddress -notlike "$($_.SamAccountName)*" }
最后,仅指定所需的属性,而不是 -属性*
(无需等待域控制器返回不需要的数据):
Finally, specify only the properties you need rather that -Properties *
(no need to wait for the Domain Controller to return data you won't need):
$Properties = 'Name','SamAccountName','EmailAddress','PasswordNeverExpires'
Get-ADUser -Filter 'enabled -eq $true' -Properties $Properties |Where-Object {
$_.PasswordNeverExpires -eq $false -and
$_.EmailAddress -notlike "$($_.SamAccountName)*"
} |Select-Object $Properties
这篇关于Powershell:根据属性过滤属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!