2013-02-20 105 views
0

我在PowerShell中有以下查詢。如果ManageBy字段爲空,我不希望它顯示在列表中。我如何用我的代碼來完成這個任務?感謝從GET-ADUSER刪除空值

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 
-----               ----------             ----------                          ---------- 
[email protected]            FG-SFO-AudioProcessingWorkshop        CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]             FG-NTHA1-D-Work-Release Managers       CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]             Engineering, Digital Work Applications      CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net             Distribution 
[email protected]             FG-NTHA1-D-Work-Release Users        CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]           Jupiter-developers           CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net             Distribution 
[email protected]            IDP Phase 4-5             CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]             Eng, Product             CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]             FG-Projects-ENG_Common          CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]             FG-Projects-companyMediaProducer        CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]             FG-NTHA1-companyMediaProducer         CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]             FG-NTHA1-D-Work-Stage          CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]            FG-Projects-FunctionalActivityReports-READ     CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]          AG-EPSRM_Power_Requestor          CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]            Build Team             CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net             Distribution 
[email protected]            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 
[email protected]          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 
[email protected]            FG-SFO-File-01-Projects-companyMediaProducerArchive-RW   CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]            AG-EC Commander Users           CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]             FG-Projects-CP750            CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net              Security 
[email protected]           Jupiter-buildmasters           CN=Useron\, Michael,OU=Users,OU=New York,DC=company,DC=net             Distribution 

的結果是上述

回答

1

喜歡這個?

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

仍然收到相同的錯誤,添加到我的文章的細節。謝謝 – user1938745 2013-02-20 17:03:27

+0

更新了不同要求的答案。 – mjolinor 2013-02-20 17:31:27

+0

Spits同樣的錯誤:無法找到具有標識的對象: – user1938745 2013-02-21 00:22:46