簡體   English   中英

無法向 Powershell 數組添加項目

[英]Cannot add items to Powershell array

我對 Powershell 比較陌生,但無法在網上找到答案。

我試圖在 Exchange 2010 中獲取每個禁用用戶的電子郵件數量,但還需要獲取用戶的標題表單 AD,因為組織使用 AD 中的標題屬性按類型對用戶進行分組

我寫了以下內容,但我無法獲得我需要的數據,它只是將長度和數字返回到 CSV 文件,例如“長度”“10”“3”“34”

如果我將 $title 排除在 $Disabled+= 的分配之外,則用戶名和項目計數將添加到 csv 文件中,但我也確實需要標題。 誰能指出我哪里出錯了。

Import-Module ActiveDirectory

$i=0

$disUsers = Get-ADUser -Filter * -SearchBase "ou=User Disabled Accounts,dc=test,dc=com" -Properties SamAccountName,Title

$Disabled = @()

$disUsers | Foreach-Object{      
    $sam = $_.SamAccountName
    $title = $_.Title
    $mailDetail=Get-MailboxStatistics $sam | Select -Property DisplayName,ItemCount
    $Disabled += $title, $mailDetail
    $i++;   
 }


$Disabled | Export-Csv -Path $env:userprofile\desktop\DisabledADUserTitlewithMailbox.csv -NoTypeInformation

不幸的是,使用史蒂夫提供的代碼會出現以下錯誤

Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: 'ADCDisabledMail'  Key being added: 'ADCDisabledMail'" ...     

Exception calling "Add" with "2" argument(s): "Key cannot be null. Parameter name: key"...

編輯在史蒂文的幫助下,我能夠使用以下內容

'Import-Module ActiveDirectory' 

$i=0

$disUsers=Get-ADUser -Filter {mailNickName -like '*'} -SearchBase "ou=User Disabled Accounts,dc=test,dc=com" -Properties SamAccountName,Title 

$dis2 = $disUsers.count

$DisabledUser = @()

$disUsers | Foreach-Object{

Write-Host "Processing record $i of $dis2"

                $sam = $_.SamAccountName

                $title = $_.Title

$mailDetail=Get-MailboxStatistics $sam | Select-Object DisplayName, @{ Name = 'Title'; Expression = {$title}}, ItemCount  

                                $DisabledUser+= $mailDetail
  $i++;   

}

$DisabledUser | Export-Csv -Path $env:userprofile\desktop\DisabledADUserTitlewithMailbox.csv -NoTypeInformation

聽起來您真正想做的是關聯數據以創建一個小報告。 您正在處理來自不同命令的數據,因此您需要一個屬性來加入。 在這種情況下,我會查看 LegacyExchangeDN AD 屬性和Get-MailboxStatistics返回的 LegacyDN 屬性。 代碼可能類似於:

$DisabledUsers = @{}

Get-ADUser -SearchBase 'ou=User Disabled Accounts,dc=test,dc=com' -Filter * -Properties 'Title','legacyExchangeDN' |
ForEach-Object{ $DisabledUsers.Add( $_.legacyExchangeDN, $_ ) }

$DisabledUsers.Values.SamAccountName | 
Get-MailboxStatistics |
Select-Object DisplayName, ItemCount, @{ Name = 'Title'; Expression = { $DisabledUsers[$_.LegacyDN].Title } }

這將輸出如下內容:

DisplayName   ItemCount Title
-----------   --------- -----
Mr. Smith        113576 Executives

如果您希望它直接轉到 CSV 文件,只需在Select-Object命令之后添加Export-CSV命令,如下所示:

$DisabledUsers = @{}

Get-ADUser -SearchBase 'ou=User Disabled Accounts,dc=test,dc=com' -Filter * -Properties 'Title','legacyExchangeDN' |
ForEach-Object{ $DisabledUsers.Add( $_.legacyExchangeDN, $_ ) }

$DisabledUsers.Values.SamAccountName | 
Get-MailboxStatistics |
Select-Object DisplayName, ItemCount, @{ Name = 'Title'; Expression = { $DisabledUsers[$_.LegacyDN].Title } } |
Export-CSV -Path $env:userprofile\desktop\DisabledADUserTitlewithMailbox.csv -NoTypeInformation

我會使用 Exchange 命令行管理程序中的Get-User ,但是它沒有 LegacyExchangeDN 作為返回的屬性。 它確實有 SamAccountName,但使用它會迫使我通過Get-Mailbox橋接所有內容。 無論如何,這是使用哈希表來引用不同集合中的相關值的一種非常常見的技術。

我確信需要做一些額外的工作才能使報告恰到好處。

順便說一句,盡量避免使用+=運算符來追加數組。 獲取數組的最佳方法是讓 PowerShell 像我上面那樣提供它。 但是,如果您無法解決它,最常見的替代方法是 ArrayList。 像大多數事情一樣,有幾種方法可以解決,下面只是 1 個示例。

# To create:
$ArrList = [Collections.ArrayList]@()

#To Add a value:
[Void]$ArrList.Add( 'ValueOrObjectHere' )

注意:使用 Google 機器很容易找到+=和 ArrayList 的文檔/討論...

更新:

解決最近編輯中指出的錯誤:

第一個錯誤基本上是不可能的。 原諒我,但我必須假設你犯了一些錯誤來產生這個錯誤。 LegacyExchangeDN 應始終以'/o=...'開頭,錯誤引用的關鍵是'ADCDisabledMail' 此外,LegacyExchangeDN 在 Active Directory 中自然是獨一無二的,因此您幾乎不可能有重復項。 因此,我沒有做出任何努力,也沒有任何保證,以防止這種不太可能發生的錯誤。

注意:如果您重復測試代碼,您必須重新創建哈希, $DisabledUsers = @{}否則哈希將存在於上次運行中,並且重復鍵錯誤是肯定的...

第二個錯誤,“密鑰不能為空”可能是由於引用的 OU 中未啟用郵箱的 AD 帳戶有效地導致這些用戶的 LegacyExchangeDN 屬性為空。 因此,空鍵......您可以通過修改過濾器以僅返回啟用郵件的用戶來避免這種情況:

$disUsers = Get-ADUser -Filter  { mailNickName -like '*' } -SearchBase "ou=User Disabled Accounts,dc=test,dc=com" -Properties SamAccountName,Title

注意:作為參考,mailNickName 是通常與Get-Mailbox返回的別名屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM