从GET-ADUSER中删除空值

时间:2013-02-20 01:51:35

标签: powershell

我在powershell中有以下查询。如果ManageBy字段为null,我不希望它显示在列表中。我如何用我的代码实现这一目标?感谢

Import-Module activedirectory

(Get-ADUser -Identity user –Properties MemberOf | Select MemberOf).MemberOf | Get-ADGroup -Properties ManagedBy | Select Name, ManagedBy, Distinguishedname, GroupCategory |
Where-Object {
$_.Distinguishedname -notlike "*Test*" -and $_.Distinguishedname -notlike "*Test1*" -and $_.name -notlike "*Test3*"}|
ForEach-Object {
If ($_.ManagedBy) {
$result = New-Object PSObject
Add-Member -input $result NoteProperty 'Managed By' ((Get-ADUser -Identity $_.ManagedBy).givenName + ' ' + ((Get-ADUser -Identity $_.ManagedBy).surName))
Add-Member -input $result NoteProperty 'Group Name' $_.Name
Add-Member -input $result NoteProperty 'Email' (Get-ADUser -Identity $_.ManagedBy -Properties mail).Mail
Add-Member -input $result NoteProperty 'Group Type' $_.GroupCategory
Write-Output $result
}

} | select 'Email','Group Name','Managed By','Group Type' | sort 'Managed By'

尝试 select 'Email','Group Name','Managed By','Group Type' | where {$_.'Managed By'} | Sort 'Managed By' 并仍然收到以下错误。

Get-ADUser : Cannot find an object with identity: 'CN=Managers,OU=Groups,OU=Users,OU=New York,DC=company,DC=net' under: 'DC=company,DC=net'.
At line:9 char:55
+ Add-Member -input $result NoteProperty 'Managed By' ((Get-ADUser -Identity $_.Ma ...
+                                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (CN=Managers...DC=company,DC=net:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Get-ADUser : Cannot find an object with identity: 'CN=Managers,OU=Groups,OU=Users,OU=New York,DC=company,DC=net' under: 'DC=company,DC=net'.
At line:11 char:49
+ Add-Member -input $result NoteProperty 'Email' (Get-ADUser -Identity $_.ManagedB ...
+                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (CN=Managers...DC=company,DC=net:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser



   Email                                                         Group Name                                                    Managed By                                                                                                      Group Type
-----                                                         ----------                                                    ----------                                                                                                      ----------
regu.r@company.com                                              FG-SFO-AudioProcessingWorkshop                                CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
DCB@company.com                                                 FG-NTHA1-D-Work-Release Managers                            CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
DCB@company.com                                                 Engineering, Digital Work Applications                      CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                 Distribution
DCB@company.com                                                 FG-NTHA1-D-Work-Release Users                               CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
kent@company.com                                          Jupiter-developers                                            CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                 Distribution
LMAU@company.com                                                IDP Phase 4-5                                                 CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
PXM@company.com                                                 Eng, Product                                                  CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
user1@company.com                                                 FG-Projects-ENG_Common                                        CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
user1@company.com                                                 FG-Projects-companyMediaProducer                                CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
SXM@company.com                                                 FG-NTHA1-companyMediaProducer                                   CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
DCB@company.com                                                 FG-NTHA1-D-Work-Stage                                       CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
AAQUI@company.com                                               FG-Projects-FunctionalActivityReports-READ                    CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
nelle.chapman@company.com                                       AG-EPSRM_Power_Requestor                                      CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
user2@company.com                                              Build Team                                                    CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                 Distribution
user2@company.com                                              Build Notifications                                           CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                 Distribution
                                                               FG-nyo-file-01-projects-buildsarea_ReadWrite                  CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
                                                               MMS-Users                                                     CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                 Distribution
holly.krer@company.com                                       Work BG All                                                 CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                 Distribution
                                                                test                                                                CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                 Distribution
user2@company.com                                              FG-SFO-File-01-Projects-companyMediaProducerArchive-RW          CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
user2@company.com                                               AG-EC Commander Users                                         CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
EJN@company.com                                                 FG-Projects-CP750                                             CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                     Security
kent@company.com                                          Jupiter-buildmasters                                          CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net                                                 Distribution

结果高于

1 个答案:

答案 0 :(得分:1)

喜欢这个吗?

   $filter = [scriptblock]::create("distinguishedname -eq '$($_.ManagedBy)'")
   $Manager = Get-ADUser -Filter $filter -Properties Mail 
   Add-Member -input $result NoteProperty 'Email' $Manager.Mail