2016-04-08 8 views
0

私は短い日付と短い時間の両方としてSentOnを取得したいと思います。どうやってやるの?PowershellのOutlook MailItemとは別に日付と時刻を取得するには?

$mailbox = "[email protected]" 

$outlook = New-Object -com Outlook.Application 
$ns = $outlook.GetNamespace("MAPI") 
$inbox = $ns.Folders.Item($mailbox).Folders.Item("Inbox") 
$searchItems = $inbox.Folders.Item("MySubFolder").Folders.Item("MyNestedSubFolder").Items 
$searchItems | Select Subject, SentOn.ToShortTimeString() 

これは私も$searchItems | Select Subject, [datetime](SentOn).ToShortTimeString()を試してみましたが、同じエラーを得たSentOn : The term 'SentOn' is not recognized as the name of a cmdlet, function, script file, or operable program.

エラーになります。

答えて

1

。ここで、そのタイプのメソッドを使用してプロパティの出力を変更したいとします。すでに見てきたように、プロパティを動的に編集することはできません。

Calculated propertiesは、その目的のために設計されているので、ここにうまく収まります。

select Subject, @{Name="SentOn";Expression={($_.SentOn).ToShortDateString()}} 

SentOn以外のプロパティを変更する必要がある場合は、計算されたプロパティをカンマで区切って追加するだけです。通常の既存プロパティと同様です。

1

試してみてください。改鋳する理由はありませんので、プロパティSentOnSystem.DateTimeタイプのものであり、ループ内の検査の際

$searchItems | %{[pscustomobject]@{ 
        Subject=$_.Subject; 
        ShortTime=(get-date $_.SentOn.DateTime).ToShortTimeString(); 
        ShortDate=(get-date $_.SentOn.DateTime).ToShortDateString()} 
       } 
+0

現在のオブジェクトに必要なものが既に存在する場合、不要な呼び出しで新しいオブジェクトを構築する必要はありません。 – Matt

関連する問題