如何使用位于数组中的数组中的数据

时间:2019-06-13 13:10:09

标签: arrays powershell

基本上,我正在整理一个脚本,该脚本将对我们所有的Exchange服务器运行跟踪,该脚本供使用远程Powershell的代理使用,因为该方案不支持管道。

为此,我将结果存储到数组中,然后再导出为CSV,我遇到的问题是收件人本身就是数组。

所以我的问题是我如何引用数组中的数组,以便将收件人放入导出的CSV中。在我当前的脚本中,下面的列写为:

服务器时间戳记EventId源发件人收件人MessageSubject

然后在“收件人”列中填充:

@ {Recipients = System.Collections.ArrayList}

Param (
    [Parameter(Mandatory=$True)]
    [ValidateNotNull()]
    [String]$Start,

    [Parameter(Mandatory=$True)]
    [ValidateNotNull()]
    [String]$End,

    [Parameter(Mandatory=$True)]
    [ValidateNotNull()]
    [String]$Sender,

    [Parameter(Mandatory=$False)]
    [ValidateNotNull()]
    [String]$Recipient,

    [Parameter(Mandatory=$False)]
    [ValidateNotNull()]
    [String]$Subject
)

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri <HostAddress> -Authentication basic -Credential $UserCredential
Import-PSSession $Session -DisableNameChecking

$ifBlock = "0"
$tracingResults = @()

#Get-ExchangeServer | Select Name
#ForEach ($S in (Get-ExchangeServer | select -ExpandProperty name))
if (($Recipient -eq "" -And $Subject -eq "") -And ($ifBlock -eq "0" -or $ifBlock -eq "R+S"))
        {
        $ifBlock = "R+S"
        ForEach ($S in (Get-ExchangeServer | select -ExpandProperty name)){
        $Tracing = Get-MessageTrackingLog -ResultSize Unlimited -server $S -Start $Start -End $End -Sender $Sender | select Timestamp,ServerHostname,EventId,Source,Sender,Recipients,MessageSubject
         if ($tracing -eq ""){
            Write-Output ($S + " : No emails found here")
            }
         if ($Tracing -ne ""){
            $TracingObject = Get-Variable Tracing
                try{
                    $tracingResults += New-Object -TypeName PSObject -Property @{
                    Server = $S
                    Timestamp = $TracingObject.Value[0] | select Timestamp
                    EventId = $TracingObject.Value[0] | select EventId
                    Source = $TracingObject.Value[0] | select Source
                    Sender = $TracingObject.Value[0] | select Sender
                    Recipients = $TracingObject.Value[0] | select Recipients
                    MessageSubject = $TracingObject.Value[0] | select MessageSubject
                    } | Select-Object Server,Timestamp,EventId,Source,Sender,Recipients,MessageSubject
                }
                catch{}
                #Write-Output ($tracing)
            }
        }
        }

Remove-PSSession $Session
$tracingResults | Export-Csv -NoTypeInformation -Path ./trace.csv

2 个答案:

答案 0 :(得分:0)

问题是,您创建了一种复杂的$tracingResults方法...但是,您这样做的方式是,您需要PSCustomObject而不是PSObject。这是minimal-reproducible-example

 $tracingResults = @()
 1..3 | foreach {
     $tracingResults += New-Object -TypeName PSCustomObject -Property @{
                        Server = "serv$_"
                        Timestamp = "time$_"
                        EventId = "EventId$_"
                        Source = "Source$_"
                        Sender = "Sender$_"
                        Recipients = "Recipients$_"
                        MessageSubject = "MessageSubject$_"
                        } | Select-Object Server,Timestamp,EventId,Source,Sender,Recipients,MessageSubject
}
$tracingresults | convertto-csv -NoTypeInformation

答案 1 :(得分:0)

不幸的是,这个解决方案对我的T-Me都不起作用,但是我将这种方法用于其他脚本。

作为参考,我通过遍历返回的每个结果来解决此问题,而不是尝试一次性处理每台服务器的所有返回结果:

 <p-fieldset legend="Global Font Size">
                <input type="text" pInputText placeholder="Enter the global font size">
<button (onClick)="saveGlobalFontSize(fontSize)"> OK </button>
            </p-fieldset>