脚本不向新列添加新数据

Script not adding new data to new column

我已经编写了一个脚本 return 来自 Active Directory 和 Exchange 的一些详细信息,然后将这些详细信息放入一个数组中。

问题是 Exchange 部分正在覆盖数组中的现有值,而我希望它基本上为 Exchange 数据创建一个新列并将其放入其中。

这是脚本:

$adusers = Get-ADUser -Filter {Enabled -eq $true} -Properties LastLogonDate, createTimeStamp, mail |
           Where-Object { $_.Name -like $_."Name" } |
           Select Name, SamAccountName, LastLogonDate, createTimeStamp, mail

$adusers | ForEach-Object {
    $mailboxstats = Get-Mailbox $_.mail |
                    Get-MailboxStatistics |
                    Select-Object TotalItemSize
    $adusers += $mailboxstats
}

return $adusers

使用Select-Object修改集合中的项目:

return $adusers | Select *,@{Name='TotalItemSize';Expression={
    $mailboxstats = Get-Mailbox $_.mail |
                    Get-MailboxStatistics |
                    Select-Object -Expand TotalItemSize}}

无论如何都要使用 calculated property for the mailbox size. Also, the condition $_.Name -like $_."Name" is always true, so you can simply remove your Where-Object filter, and the return keyword isn't required, because PowerShell functions return all non-captured output on the Success output stream

将您的代码更改为如下内容:

Get-ADUser -Filter {Enabled -eq $true} -Properties LastLogonDate, createTimeStamp, mail |
    Select Name, SamAccountName, LastLogonDate, createTimeStamp, mail,
        @{n='MailboxSize';e={
            Get-Mailbox $_.mail |
                Get-MailboxStatistics |
                Select-Object -Expand TotalItemSize
        }}

非常感谢。完美地解决了这个问题。

谢谢!!!

Get-ADUser -Filter {Enabled -eq $true} -Properties LastLogonDate, createTimeStamp, mail |
    Select Name, SamAccountName, LastLogonDate, createTimeStamp, mail,
        @{n='MailboxSize';e={
            Get-Mailbox $_.mail |
                Get-MailboxStatistics |
                Select-Object -Expand TotalItemSize
        }}