2016-12-07 10 views
0

メールボックスに送信され、ファイル共有にコピーされ、名前に基づいて異なる配布グループに電子メールを送信するExcelレポート(15分ごとに実行される)を抽出するスクリプトがあります。レポートのPowershellスクリプトは、変数の値の代わりにTrueを返します

15分以内に複数のレポートがフォルダにダウンロードされた場合、スクリプトは正常に機能し、電子メール内のレポートの名前で正しく電子メールを送信します。

15分の間にダウンロードされたレポートが1つのみで、スクリプトがまだ動作して電子メールを送信しているが、電子メールの本文にレポートの名前の代わりにTrueと表示されるという問題があります。

$downloadTemp = 'C:\Test\' 

# List the files in the Temporary download folder 
$files = (Get-ChildItem $downloadTemp -name) 

# Set the Creation date 
$exists = (Get-ChildItem $downloadTemp | Measure-Object).Count 

If ($exists -gt 0) {Get-Item $downloadTemp | % {$_.CreationTime = (Get-Date)} 
} 

# Look for Specific reports 
$DailyOpen = $files -Like "Daily Open*" 
$DailyProduct = $files -Like "Daily Product*" 
$DailyRaised = $files -Like "Daily Raised*" 
#More variables 

$compareDate = (Get-Date).AddMinutes(-15) 

$diff = ((Get-Item $downloadTemp).CreationTime) 

If ($diff -gt $compareDate) { 

    If ([bool]$DailyOpen -eq $True) { 

     $body = "<HTML><HEAD><META http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"" /><TITLE></TITLE></HEAD>" 
     $body += "<BODY bgcolor=""#FFFFFF"" style=""font-size: Small; font-family: CALIBRI; color: #000000""><P>" 
     $body += "Hi All<br><br>" 
     $body += "This is an email to let you know that a new report " + $DailyOpen + " has arrived in<br><br>" 
     $body += "\\{fileshare}\" 

     Send-MailMessage -To "" -From "" -Subject "New Daily Open Complaints Report Notification" -bodyashtml -Body $body -SmtpServer "" 

    } 

    If ([bool]$DailyProduct -eq $True) { 

     $body = "<HTML><HEAD><META http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"" /><TITLE></TITLE></HEAD>" 
     $body += "<BODY bgcolor=""#FFFFFF"" style=""font-size: Small; font-family: CALIBRI; color: #000000""><P>" 
     $body += "Hi All<br><br>" 
     $body += "This is an email to let you know that a new report " + $DailyProduct + " has arrived in<br><br>" 
     $body += "\\{fileshare}\" 

     Send-MailMessage -To "" -From "" -Subject "New Daily Product Conversions Report Notification" -bodyashtml -Body $body -SmtpServer "" 

    } 
#More If statements here. 1 for every report variable used. 
} 

答えて

1

私はあなたの$が変数ファイルを無視して、その中にコードを再利用する、これを行うためのよりエレガントな方法があると確信しています:

$DailyOpen = Get-ChildItem $downloadTemp -name "Daily Open*" 

$DailyOpen = $files -Like "Daily Open*" 

を交換してください

私のテストでは、1つのファイルと2つのファイルで動作しました。

だからあなたの例では、あなたが交換する必要があります:

If ([bool]$DailyOpen -eq $True) 
    If ([bool]$DailyProduct -eq $True) 

へ:

# Look for Specific reports 
$DailyOpen = $files -Like "Daily Open*" 
$DailyProduct = $files -Like "Daily Product*" 
$DailyRaised = $files -Like "Daily Raised*" 

# Look for Specific reports 
$DailyOpen = Get-ChildItem $downloadTemp -name "Daily Open*" 
$DailyProduct = Get-ChildItem $downloadTemp -name "Daily Product*" 
$DailyRaised = Get-ChildItem $downloadTemp -name "Daily Raised*" 

で、あなたも変更する必要があります
If ($DailyOpen) 
If ($DailyProduct) 

パターンに一致するコンテンツがない場合、これらの変数は初期化されません。

:-)今

ます。また、$ファイルの変数と離れて行うことができますが

$files = @(Get-ChildItem $downloadTemp -name) 

と、すべてのSIへ

1

変更

$files = (Get-ChildItem $downloadTemp -name) 

を楽しみますmilarの場合。

PSはあなたに1つのもの、または配列を与えています。 1つのことがあっても常に配列にする必要があるので、配列コンストラクタ@()を使用する必要があります。

それは単一のもの(ブール演算子と戻るよう-like作用本当かどうかVSこの

$DailyOpen = $files -Like "Daily Open*" 

配列である$filesかに応じて変化する(フィルタとして-like作用し、濾過配列を返す)ためです/ false)。

関連する問題