簡體   English   中英

Powershell 如果找到多個條目,則在 output CSV 文件中添加新行

[英]Powershell add a new line in output CSV file if multiple entries found

我正在從 Office 365 導出郵箱。一些具有相同 primarysmtpaddress 的郵箱處於活動狀態,也處於非活動狀態。 我在這里輸入代碼有以下代碼:

$MBX = import-csv "c:\retention\LegalHold\test.csv"

$MBX | Add-Member -MemberType NoteProperty -Name Retention -Value NoPolicy
$MBX | Add-Member -MemberType NoteProperty -Name NoMailbox -Value $False
$MBX | Add-Member -MemberType NoteProperty -Name IsInactiveMailbox -Value $null
$MBX | Add-Member -MemberType NoteProperty -Name PrimarySmtpAddress -Value $null

$count=0
$fullcount = ($mbx | measure-object).count

foreach ($M in $MBX){ 
$policy = $null

$count++
$search = $m.custodian.tostring()

Write-progress -activity "Checking Mailbox $count out of $fullcount --- $search " -percentcomplete (($count / $fullcount)*100) -status "Processing"

    Try{
        $ErrorActionPreference = 'Stop'
        $Policy = Invoke-Command -scriptblock {Get-EXOMailbox $m.custodian -IncludeInactiveMailbox -properties primarysmtpaddress,inplaceholds,isinactivemailbox,LitigationHoldEnabled,LitigationHoldDuration,Office}
        #$Policy = Invoke-Command -scriptblock {Get-Mailbox $m.name -IncludeInactiveMailbox}
        #$m.MBXFound = $True
        $m.IsInactiveMailbox = $Policy.IsInactiveMailbox -join ','
        $m.PrimarySmtpAddress = $Policy.PrimarySmtpAddress -join ','

    }Catch{$m.NoMailbox = "$True"}
            
}

$MBX | export-csv C:\retention\legalhold\test_check.csv -NoTypeInformation

而不是像這樣輸出到 CSV :

在此處輸入圖像描述

我希望將帶有活動和非活動郵箱的郵箱放在單獨的行上,如下所示:

在此處輸入圖像描述

您需要遍歷$Policy的結果,而不僅僅是使用-join

$MBX = import-csv "c:\retention\LegalHold\test.csv"

$MBX | Add-Member -MemberType NoteProperty -Name Retention -Value NoPolicy
$MBX | Add-Member -MemberType NoteProperty -Name NoMailbox -Value $False
$MBX | Add-Member -MemberType NoteProperty -Name IsInactiveMailbox -Value $null
$MBX | Add-Member -MemberType NoteProperty -Name PrimarySmtpAddress -Value $null

$count=0
$fullcount = ($mbx | measure-object).count

$Output = foreach ($M in $MBX){ 
$policy = $null

$count++
$search = $m.custodian.tostring()

Write-progress -activity "Checking Mailbox $count out of $fullcount --- $search " -percentcomplete (($count / $fullcount)*100) -status "Processing"

    Try{
        $ErrorActionPreference = 'Stop'
        $Policy = Invoke-Command -scriptblock {Get-EXOMailbox $m.custodian -IncludeInactiveMailbox -properties primarysmtpaddress,inplaceholds,isinactivemailbox,LitigationHoldEnabled,LitigationHoldDuration,Office}
        #$Policy = Invoke-Command -scriptblock {Get-Mailbox $m.name -IncludeInactiveMailbox}
        #$m.MBXFound = $True
        $Policy | ForEach-Object {
            $m.IsInactiveMailbox = $_.IsInactiveMailbox
            $m.PrimarySmtpAddress = $_.PrimarySmtpAddress
            $m
        }
    }Catch{$m.NoMailbox = "$True";$m}            
}

$Output | export-csv C:\retention\legalhold\test_check.csv -NoTypeInformation

暫無
暫無

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

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