哈希表中的Powershell表达式[已关闭]

kyxcudwk  于 2023-01-05  发布在  Shell
关注(0)|答案(1)|浏览(124)

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
16天前关闭。
Improve this question
我最近对powershell脚本有了更多的了解,我在摸索着,但是我不能解决这个问题。
基本上我知道这个代码是有效的:

Get-Mailbox -ResultSize Unlimited |
Select-Object ExternalDirectoryObjectId, DisplayName,PrimarySmtpAddress, Alias, UserPrincipalName, ArchiveStatus, ArchiveDatabase,FirstName, LastName, RecipientTypeDetails, `
@{Name="EmailAddresses";Expression={
    ($_.EmailAddresses | 
    Where-Object {$_ -clike "smtp:*"} | 
    ForEach-Object {$_ -replace "smtp:",""}) -join "," 
}} |
Export-Csv "C:\temp\export.csv" -NoTypeInformation -Encoding UTF8

问题是,我想运行命令从几个模块上的所有邮箱,以产生一个单一的CSV,所以我这样做:

$mailboxes = get-mailbox -resultsize unlimited
$export = @()

foreach($mailbox in $mailboxes)
{
    $UPN = $mailbox.MicrosoftOnlineServicesID
    $mailboxstat = Get-MailboxStatistics $UPN
    $MSOL = get-msoluser -UserPrincipalName $UPN        

    $Properties = @{
        "Object ID" = $mailbox.ExchangeGUID
        "Display Name" = $mailbox.DisplayName
        "Alias" = $mailbox.Alias
        "Primary SMTP Address" = $mailbox.PrimarySMTPAddress
        "Mailbox Size" = $mailboxstat.TotalItemSize
        "First" = $MSOL.FirstName
        "Last" = $MSOL.LastName
        "RecipientTypeDetails" = $mailbox.RecipientTypeDetails
        "ArchiveStatus" = $mailbox.ArchiveStatus
        "ArchiveGUID" = $mailbox.ArchiveGUID
        "LitigationHold" = $mailbox.LitigationHold
        "Licenses"=$MSOL.Licenses
        "ProxyAddresses"= @{Name="EmailAddresses";Expression={($_.EmailAddresses | Where-Object {$_ -clike "smtp:*"}
    }
    write-host $UPN

    #$properties += $x500

    $export += New-Object psobject -property $properties 
    
}
write-host $export
$exportpath = saveAs
$export | 
    select-object "Object ID", "Display Name","Primary SMTP Address","ProxyAddresses","ArchiveStatus","ArchiveGUID",First,Last,LitigationHold, "Mailbox Size" | 
    Export-Csv -notypeinformation -path $exportpath

这与代理地址无关。如果我把哈希/表达式代码放在导出代码中,它不会为每个邮箱生成结果。但无论我如何编码,我都无法让它生成我想要的结果。
我试过在$properties散列表中创建一个新对象,convert-string,几乎所有我能想到或在google上找到的东西。
有人能帮助我如何需要格式化这段代码,它的工作方式,我需要什么?
先谢了

5f0d552i

5f0d552i1#

您的问题是,您正在定义一个哈希表,您将其视为Select-ObjectProperties参数,但随后您将其发送到New-Object,后者的工作方式非常不同/将哈希表转换为PSObject,而不是选择现有属性。

$export += ([PSCustomObject]@{
    "Object ID" = $mailbox.ExchangeGUID
    "Display Name" = $mailbox.DisplayName
    "Alias" = $mailbox.Alias
    "Primary SMTP Address" = $mailbox.PrimarySMTPAddress
    "Mailbox Size" = $mailboxstat.TotalItemSize
    "First" = $MSOL.FirstName
    "Last" = $MSOL.LastName
    "RecipientTypeDetails" = $mailbox.RecipientTypeDetails
    "ArchiveStatus" = $mailbox.ArchiveStatus
    "ArchiveGUID" = $mailbox.ArchiveGUID
    "LitigationHold" = $mailbox.LitigationHold
    "Licenses"=$MSOL.Licenses
    "ProxyAddresses"= ($mailbox.EmailAddresses | Where-Object {$_ -clike "smtp:*"}) -join ','
})

也就是说,无需提供代码将管道中的当前对象转换为名为EmailAddresses的属性,然后将结果分配给名为ProxyAddresses的属性,只需计算并分配您希望ProxyAddresses直接具有的值。
我还在hashtable之前使用了[PSCustomObject],将hashtable转换为PSCustomObject,并将其直接赋给数组。如果您愿意,可以继续将hashtable保存在变量中(例如,之前调用的$properties),然后将其作为一个参数传递给New-Object,以便在赋给数组之前将其转换为PSObject

相关问题