日历,将Outlook电子邮件导出为CSV

时间:2017-09-03 14:35:01

标签: powershell csv email

我的工作是将Outlook默认收件箱文件夹中的电子邮件导出到CSV文件。

这是脚本:

cls
Function Get-OutlookInBox   #getoutlookcontent
{ 

Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null 
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]  
$outlook = new-object -comobject outlook.application 
$namespace = $outlook.GetNameSpace("MAPI") 
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox) 
$folder.items |  
Select-Object -Property Subject, ReceivedTime, SenderName, to
} 

write-host "select date greater or equal to and hit enter"
Function date1                                                        
#calendar
{
[void] 
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

$objForm = New-Object Windows.Forms.Form 

$objForm.Text = "Select a Date" 
$objForm.Size = New-Object Drawing.Size @(190,190) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Enter") 
    {
        $global:dtmDate=$objCalendar.SelectionStart
        $objForm.Close()
    }
})

$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Escape") 
    {
        $objForm.Close()
    }
})

$objCalendar = New-Object System.Windows.Forms.MonthCalendar 
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})  
[void] $objForm.ShowDialog() 


}

date1

$gt = (get-date).tostring()              #getcurrent date

Get-OutlookInbox |                        #exporting
where { $_.ReceivedTime -gt [datetime] "$globa:dtmdate" -AND $_.ReceivedTime 
-le 
[datetime] "$gt" } | Export-Csv D:\Users\XXX\Desktop\test1.csv -
Encoding ASCII -NoTypeInformation -force

部分代码分别完美无缺。 当我将日期输入到以下代码中时,我可以导出电子邮件:

Get-OutlookInbox |                        #exporting
where { $_.ReceivedTime -gt [datetime] "8/21/2017" -AND $_.ReceivedTime 
-le 
[datetime] "8/24/2017" } | Export-Csv D:\Users\XXX\Desktop\test1.csv -
Encoding ASCII -NoTypeInformation -force 

但是当我试图输入变量而不是精确值时,我收到错误消息msg无法将值转换为“”以输入“System.DateTime”。错误:“字符串未被识别为有效的DateTime。”

我的问题是如何将变量转换为日期时间。

我在代码的这一部分注意到的第二件事

Get-OutlookInbox |                        #exporting
where { $_.ReceivedTime -gt [datetime] "8/21/2017" -AND $_.ReceivedTime 
-le 
[datetime] "8/24/2017" } | Export-Csv D:\Users\XXX\Desktop\test1.csv -
Encoding ASCII -NoTypeInformation -force 

在这种情况下,运算符-gt对我来说非常好,即使它不应该。脚本导出的电子邮件是从我设置的日期开始的。但是,第二部分-le的作用不亚于平等。这是令人困惑的,因为我需要设置日期+ 1。我试过放几个比较运算符但结果总是一样的。

0 个答案:

没有答案