简体   繁体   中英

Create csv file of all disabled AD users with mailboxes Output information from multiple cmdlets in powershell

I am trying to gather some information on disabled user accounts that have mailboxes. I am specifically looking for just user mailboxes not shared mailboxes.

Here is what I have so far.

$Mailboxes = Get-Mailbox | where {$_.RecipientTypeDetails -eq 'UserMailbox'}

$date = get-date -f "MMddyyyy_HHmm" $Disabled = @()

Foreach ($Mailbox in $Mailboxes) { if((Get-ADUser -Identity $Mailbox.SamAccountName).Enabled -eq $False){ $Disabled += Get-MailboxStatistics $Mailbox.SamAccountName | Select -Property DisplayName,TotalItemSize }
} $Disabled | Sort DisplayName | Export-Csv -Path "%path%\DisabledADUsersWithMailbox_$date`.csv" -NoTypeInformation

Additionally what I would like to collect is the users Title, Manager, LastlogonDate all of which can be found using Get-Aduser. I am unsure how I go about collecting the information from both cmdlets and then exporting it all to csv. I have read that I may need to create a custom object. I am struggling with setting that up in this script.

Any help would be much appreciated.

Thanks

the following lines should give you what you want, can't verify it as I have no exchange running here.

$date = get-date -f "MMddyyyy_HHmm" 

$Disabled = @(
    Foreach ($Mailbox in $Mailboxes) { 
        $adUser = get-aduser -Identity $Mailbox.SamAccountName -Properties enabled,manager,title,lastlogontimestamp
        If ($adUser.Enabled -eq $False){
            $mailStats = Get-MailboxStatistics $Mailbox.SamAccountName 
            $attrsht = [ordered]@{
                displayname=$mailstats.displayname
                totalitemsize=$mailStats.totalitemsize
                samaccountname=$aduser.samaccountname
                enabled=$aduser.enabled
                manager=$aduser.manager
                title=$aduser.title
                lastlogontimestamp=[datetime]::FromFileTime($aduser.lastlogontimestamp)
            }
            new-object -TypeName psobject -Property $attrsht
        }
    }
)
 
$Disabled | Sort-Object DisplayName | Export-Csv -Path "%path%\DisabledADUsersWithMailbox_$date`.csv" -NoTypeInformation

Avoid adding elements to an array by using +=. It is slow, alternatively take a look at generic array lists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM