2017-09-10 11 views
-1

私は、Sender、Recipient、Subjectの3つの配列を持っています。私は以下のように表形式でそれらを整理する必要があります。ここで異なる配列の値を表形式で整列するにはどうすればよいですか?

Sender    Recipient    Subject 
[email protected]  [email protected]   xxx 

は私のコード

dir|ForEach-Object { 
    $Name=$_.Name 
    foreach ($N in $Name) { Import-Csv $N|` 
    Foreach { 
     $Sender+=$_.SenderAddress 
     $Recipient+=$_.RecipientAddress 
     $Subject+=$_.Subject 
    } 
    } 
} 

誰もこれに見ることができるのですか?

よろしく、 キラン

+0

プログラミング言語タグを追加し、あなたが –

+0

を試してみました何のサンプルは、あなたが私たちにこれらの配列のコード例を与えることができますしてください? どのようにこれらの3つの配列を一致させることができますか?$ Sender [0]、$ Recipient [0]、$ Subject [0]? – SteloNLD

+0

PS C:公共\ドキュメント\ \ Users \ユーザーフィッシング> DIR |をForEach-Objectに{ $名= $ _ foreachの($名で$ N){ インポート、CSV $ Nを名前| ' のForeach { $。送信者+ = $ _。SenderAddress $受信者+ = $ _。RecipientAddress $件名+ = $ _。件名 } } } – LittleBuddha

答えて

2

あなたが複数の完全に整列配列を持っている場合、あなたはforループと一緒にそれらを簡単にzip圧縮することができます

$Sender = @('[email protected]','[email protected]') 
$Recipient = @('[email protected]','[email protected]') 
$Subject = @('Overdue invoices','Receipts') 

$mailObjects = for($i = 0; $i -lt $Sender.Count; $i++){ 
    New-Object psobject -Property @{ 
     Sender = $Sender[$i] 
     Recipient = $Recipient[$i] 
     Subject = $Subject[$i] 
    } 
} 

今、あなたはFormat-Tableを使用することができます。

PS C:\> $mailObjects |Format-Table 
Sender    Recipient    Subject 
------    ---------    ------- 
[email protected] [email protected] Overdue invoices 
[email protected] [email protected] Receipts 
+0

インポート| ForEachオブジェクト{ 検索 - メールボックス - 識別 "$ _。RecipientAddress" -SearchQuery "From:$ _。SenderAddress"、 "Subject: $ _。件名 "-targetmailbox" XXXXX "-targetfolder"テスト "-deletecontent -Force -loglevel full -WhatIf } – LittleBuddha

+0

今、別の問題があります。上記のスクリプトは動作していません。インポートはCSVファイルをインポートするために作成した関数です。それは実行されますが、エラーの下で私に与えます。 – LittleBuddha

+0

@LittleBuddhaあなたが解決しようとしている実際の問題を説明してください。出力フォーマットのように聞こえない場合は、実際の問題です –

0

あなたのコードを正しく解釈していれば、配列ではなくcsvファイルの列を参照しています。インポート後に希望のプロパティを選択するだけで十分ですが、新しい短いプロパティ名は[pscustomobject]で実現できます。

があるとします

> Get-Content first.csv 
"othercol","SenderAddress","RecipientAddress","Subject" 
"","[email protected]","[email protected]","Overdue invoices" 
"","[email protected]","[email protected]","Receipts" 

と:

>Get-Content second.csv: 
"SenderAddress","RecipientAddress","Subject","othercol" 
"[email protected]","[email protected]","Overdue deliveries","xyz" 
"[email protected]","[email protected]","Orders","abc" 

この1つのライナー:

foreach ($File in (gci *.csv)){Import-Csv $File|Select SenderAddress,Recipientaddress,Subject} 

意志出力:

SenderAddress  RecipientAddress  Subject 
-------------  ----------------  ------- 
[email protected] [email protected] Overdue invoices 
[email protected] [email protected] Receipts 
[email protected] [email protected] Overdue deliveries 
[email protected] [email protected] Orders 

これより冗長バージョン:

ForEach ($File in (Get-ChildItem *.csv)){ 
    Import-Csv $File | Select-Object SenderAddress,Recipientaddress,Subject | 
     ForEach-Object{[pscustomobject] @{Sender = $_.SenderAddress 
              Recipient = $_.Recipientaddress 
              Subject = $_.Subject} 
     } 
} 

は、この出力が得られます。

Sender    Recipient    Subject 
------    ---------    ------- 
[email protected] [email protected] Overdue invoices 
[email protected] [email protected] Receipts 
[email protected] [email protected] Overdue deliveries 
[email protected] [email protected] Orders 
関連する問題