Powershell脚本返回True而不是变量的值

时间:2016-12-07 07:11:24

标签: powershell

我有一个脚本可以提取发送到邮箱的Excel报告(每15分钟运行一次)并将它们复制到文件共享,并根据报告的名称通过电子邮件发送不同的通讯组。

如果在15分钟内将多个报告下载到该文件夹​​,则该脚本可以正常工作并使用电子邮件中的报告名称正确发送电子邮件。

问题是,如果在15分钟内只下载了一个报告,脚本仍然有效并发送电子邮件,但不是电子邮件正文中的报告名称,而是真实。

$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 个答案:

答案 0 :(得分:1)

更改

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

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

以及所有类似案例。

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

那是因为这个

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

更改取决于$files是否为数组(-like充当过滤器并返回已过滤的数组)与是否为单一事物(-like充当布尔运算符并返回真/假)。