2016-12-07 1 views
0

15 분마다 실행되는 Excel 보고서를 추출하여 파일 공유로 복사하고 이름을 기반으로 다른 메일 그룹에 전자 메일을 보내는 스크립트가 있습니다 보고서의Powershell 스크립트는 변수 값 대신 True를 반환합니다.

15 분 안에 두 개 이상의 보고서가 폴더로 다운로드되면 스크립트가 정상적으로 작동하고 전자 메일의 보고서 이름과 함께 올바르게 전자 메일을 보냅니다.

문제는 15 분 동안 하나의 보고서 만 다운로드되고 스크립트가 여전히 작동하고 전자 메일을 보내는 경우 전자 메일 본문의 보고서 이름 대신 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 = $files -Like "Daily Open*" 

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

내 테스트에서는 단일 파일과 두 개의 파일로 작업했습니다.

그래서 예를 들어 당신이 대체해야합니다 :

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) 

패턴에 맞는 콘텐츠가 없으면이 변수는 초기화되지 않습니다.

또한 지금

이 :-)

즐길 거리 $ 파일 변수를 할 수

1

변경

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

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

모든시에 밀러 사건들.

PS는 하나의 물건 또는 배열을 제공합니다. 배열이 하나라도 있어도 배열이 필요하므로 배열 생성자 @()을 사용해야합니다.야

때문에

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

변경 $files 배열이며 (필터로서 -like 작용 및 필터링 된 어레이를 리턴한다)는 한 가지 (부울 연산자로 -like 행위인지 대 true를 돌려 여부에 따라 /그릇된).

관련 문제