2016-12-07 88 views
0

我有一个脚本,用于提取发送到邮箱的Excel报告(每15分钟运行一次),并将它们复制到文件共享并根据名称通过电子邮件发送不同的通讯组的报告。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*" 

在我的测试中,它可以处理单个文件和两个文件。

所以在你的例子中,你将需要更换:

# 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 ([bool]$DailyOpen -eq $True) 
    If ([bool]$DailyProduct -eq $True) 

到:

If ($DailyOpen) 
If ($DailyProduct) 

由于现在如果没有与您的模式匹配的内容,这些变量将不会被初始化。

您也可以废除了$文件变量现在

:-)

享受

1

变化

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

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

和所有SI类似病例。

PS给你一个单一的东西,或一个数组。你需要它永远是一个数组,即使有一件事,所以你需要使用@()数组构造函数。

这是因为此

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

变化取决于$files是否是一个数组(-like充当过滤器,并返回一个过滤阵列)与它是否是一个单一的东西(-like充当布尔运算符并返回true /假)。

相关问题