PowerShell:Where-Object通配符

3

我希望能够搜索特定电话号码/分机号的Active Directory用户的所有属性。

可以通过以下方式获取所有属性:

get-aduser joesmith -Properties *

但我希望过滤出特定的结果,比如扩展号码为1234的(可能在多个地方,例如extensionAttribute1、OfficePhone、HomePhone、Mobile等)。

我尝试了以下方法:

get-aduser joesmith -Properties * | where-object {$_ -like "*1234*" }

但是where-object想要$_.value,而我不知道确切的值。

我应该如何搜索多个属性的值?我希望看到以下结果:

mobile        1234
officephone   12345
othermobile   61234
1个回答

4

要遍历那些你不知道名称的属性值(例如OfficePhone,CustomAttribute2,mobile),可以使用以下方法:

get-aduser joesmith -Properties * | foreach-object { 
  foreach ($property in $_.PSObject.Properties) {
    if ($property.value -like "*1234*") {
      "$($property.name) $($property.value)"
    }
  }
}

另外,您还可以使用“Get-Member”,例如:get-aduser joesmith -prop *|gm -MemberType Property|select -ExpandProperty name|%{if($test.$_ -match "@philips"){[pscustomobject]@{Name=$_;Value=$test.$_}}} 这也许会给您带来更容易处理的结果。 - TheMadTechnician

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接