メールの添付ファイルを指定したフォルダにまとめて保存するスクリプトです。
保存先のフォルダは存在してないとだめです。
また、アウトルックでのフォルダ名やメールの題名での検索、日付での検索は可能です。
SaveAttachmentsOnOutlook.ps1
#$allは、全てのフォルダについて検索するかどうかを示すスイッチパラメータ
#$subjectFilterは、題名に対して検索する正規表現を設定する
#$folderは、検索するフォルダを指定する場合に設定する
#$addDaysは、何日前からのメールかを指定する
#数値だけの場合は日数、yが付いている場合は年数、mが付いている場合は月数、hが付いている場合は時間で指定できる
#指定しない場合は全てのメールが対象となる
#$destinationは、保存先フォルダのフルパス
Param([switch] $all, [string] $subjectFilter, [string] $folder, [string] $addDays, [string] $destination)
#フォルダーを取得するための定数定義
$olFolderInbox = 6
#Outlookのインスタンスを取得
$oOutlook = New-Object -ComObject Outlook.Application
#アカウント情報を配列に保存
$address = ($oOutlook.Session.Accounts)
#保存先フォルダのパスのチェック
if($destination.endsWith("\") -ne $true)
{
$destination = $destination + "\"
if((Test-Path -Path $destination) -eq $false)
{
"保存先フォルダが存在しません。"
return
}
}
#主題でフィルタリングするかのフラグ
if($subjectFilter -eq $null)
{
$subject = $false
}
else
{
$subject = $true
$regS = [regex] $subjectFilter
}
#カウンターを初期化
$i = 1
#今日の日付
$today = Get-Date
#開始の日付
$startDay = $null
if($addDays -ne $null)
{
#開始日の計算
$addDays.ToString() -match "(?<d>\d+)" > Out-Null
#数値を抜き出す
$digit = -1 * $matches.d
#年で指定
if($addDays[-1] -eq "y")
{
$startDay = $today.AddYears($digit)
}
#月で指定
elseif($addDays[-1] -eq "m")
{
$startDay = $today.AddMonths($digit)
}
#時間で指定
elseif($addDays[-1] -eq "h")
{
$startDay = $today.AddHours($digit)
}
#日数で指定
else
{
$startDay = $today.AddDays($digit)
}
}
foreach($ac in $address)
{
#リストを表示
"$i" + ":" + $ac.SmtpAddress
$i++
}
$r = Read-Host "メールアドレスを選択してください"
#選択されたアカウント情報を取得
$ac = $address[$r]
#$ac | gm
$ac.DisplayName
#ルートフォルダのメールについて
foreach($f in $ac.DeliveryStore.GetDefaultFolder($olFolderInbox))
{
#全てのフォルダに対して検索する場合
If($all -eq $true)
{
#フォルダー名を表示
"フォルダ: " + $f.Name
#メールを取得
foreach($oItem in $f.Items)
{
if($startDay -eq $null -or $oItem.SentOn -ge $startDay)
{
#テキストメールかHTMLメールかで分岐
If($subject -eq $false)
{
foreach($att in $oItem.Attachments)
{
$att.SaveAsFile($destination + $oItem.Subject + "_" + $att.FileName)
$oItem.SentOn.ToString() + " " + $oItem.Subject
}
}
elseif($regS.IsMatch($oItem.Subject))
{
foreach($att in $oItem.Attachments)
{
$att.SaveAsFile($destination + $oItem.Subject + "_" + $att.FileName)
$oItem.SentOn.ToString() + " " + $oItem.Subject
}
}
}
}
"`n`n"
}
#検索するフォルダ名を指定した場合
elseIf($f.Name -match $folder)
{
#フォルダー名を表示
"フォルダ: " + $f.Name
#メールを取得する
foreach($oItem in $f.Items)
{
if($startDay -eq $null -or $oItem.SentOn -ge $startDay)
{
If($subject -eq $false)
{
foreach($att in $oItem.Attachments)
{
$att.SaveAsFile($destination + $oItem.Subject + "_" + $att.FileName)
$oItem.SentOn.ToString() + " " + $oItem.Subject
}
}
elseif($regS.IsMatch($oItem.Subject))
{
foreach($att in $oItem.Attachments)
{
$att.SaveAsFile($destination + $oItem.Subject + "_" + $att.FileName)
$oItem.SentOn.ToString() + " " + $oItem.Subject
}
}
}
}
"`n`n"
}
}
#選択されたアカウントのフォルダを取得
foreach($f in $ac.DeliveryStore.GetDefaultFolder($olFolderInbox).Folders)
{
#全てのフォルダに対して検索する場青
If($all -eq $true)
{
#フォルダー名を表示
"フォルダ: " + $f.Name
#メールを取得
foreach($oItem in $f.Items)
{
if($startDay -eq $null -or $oItem.SentOn -ge $startDay)
{
If($subject -eq $false)
{
foreach($att in $oItem.Attachments)
{
$att.SaveAsFile($destination + $oItem.Subject + "_" + $att.FileName)
$oItem.SentOn.ToString() + " " + $oItem.Subject
}
}
elseif($regS.IsMatch($oItem.Subject))
{
foreach($att in $oItem.Attachments)
{
$att.SaveAsFile($destination + $oItem.Subject + "_" + $att.FileName)
$oItem.SentOn.ToString() + " " + $oItem.Subject
}
}
}
}
"`n`n"
}
#検索するフォルダ名を指定した場合
elseIf($f.Name -match $folder)
{
#フォルダー名を表示
"フォルダ: " + $f.Name
#メールを取得する
foreach($oItem in $f.Items)
{
if($startDay -eq $null -or $oItem.SentOn -ge $startDay)
{
If($subject -eq $false)
{
foreach($att in $oItem.Attachments)
{
$att.SaveAsFile($destination + $oItem.Subject + "_" + $att.FileName)
$oItem.SentOn.ToString() + " " + $oItem.Subject
}
}
elseif($regS.IsMatch($oItem.Subject))
{
foreach($att in $oItem.Attachments)
{
$att.SaveAsFile($destination + $oItem.Subject + "_" + $att.FileName)
$oItem.SentOn.ToString() + " " + $oItem.Subject
}
}
}
}
"`n`n"
}
}
