Powershell& Get-ADUser - 第2部分 - 放置A& B在一起

时间:2015-10-14 16:22:28

标签: powershell split active-directory

前一段时间我发布了一个问题,我已经回答了我需要做的事情 - Powershell & Get-ADUser - Split OU getting the 3rd, 4th and 5th elements from the end

ANSWER GIVEN

Get-ADUser -Filter * -Properties CanonicalName |
 select -ExpandProperty  CanonicalName |
 ForEach-Object {
  $Parts = $_.split('/')

   $Object = 
    [PSCustomObject]@{
     OU = $Parts[1]
     Where = $Parts[2]
     Type = 'N/A'
     }

  if ($Parts.count -ge 5)
    { $Object.Type = $Parts[3] }

  $Object
}

现在我需要添加一些其他字段(姓名,SAM帐户,电子邮件)并检查电子邮件*。这是我的第二个问题。

Powershell查询

$Headers= @{Label="OU Path";Expression={$_.CanonicalName}},
@{Label="Distinguished Name";Expression={$_.DistinguishedName}},
@{Label="Name";Expression={$_.DisplayName}},
@{Label="SAM Account";Expression={$_.SAMAccountName}},
@{Label="E-mail";Expression={$_.EmailAddress}},

Get-ADUser -Filter * -Properties
CanonicalName,DistinguishedName,DisplayName,
SamAccountName,EmailAddress | Select $Headers

如何将我在上一个问题的答案中提供的内容与此查询结合起来,以获得以下输出?

此外,我需要检查电子邮件,如果@ company.com然后“EType”= YES。

预期结果

OU          Where     Type      Name     SAM      Email               *EType
-----------------------------------------------------------------------------
DS          AMS       N/A       Name1    brname1  name1@company.com   YES
DS          TECHM     N/A       Name2    xsname2  name2@company.com   YES
Developers  CH        Agencies  Name3    agname3  name3@gmail.com     NO
Developers  CH        Market    Name4    chname4  name4@company.com   YES
Developers  HK        Agencies  Name5    agname5  name5@other.com     NO
Developers  HK        Market    Name6    hkname6  name6@company.com   YES

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我相信你正在寻找像你的@Headers那样正确的事情:

Get-ADUser -Filter * -Properties CanonicalName,DistinguishedName,DisplayName,SamAccountName,EmailAddress | Select-Object @{Label="OU Path";Expression={$_.CanonicalName.Split("/")[1]}},
@{Label="Where";Expression={$_.CanonicalName.Split("/")[2]}},
@{Label="Type";Expression={if ($_.CanonicalName.Split("/").Count -ge 5) { $_.CanonicalName.Split("/")[3] } else { 'N/A' }}},
@{Label="Distinguished Name";Expression={$_.DistinguishedName}},
@{Label="Name";Expression={$_.DisplayName}},
@{Label="SAM Account";Expression={$_.SAMAccountName}},
@{Label="E-mail";Expression={$_.EmailAddress}},
@{Label="EType";Expression={ if($_.EmailAddress.IndexOf("@company.com") -gt -1) { 'YES' } else { 'NO' } }} | Format-Table