0

I have a script that extracts Excel reports (runs every 15 minutes) that get sent to a mailbox and copies them to a file share and emails different distribution groups based on the name of the report.

If more than one report is downloaded to the folder in the 15 minute period, the script work fine and emails correctly with the name of the report in the email.

The issue is if there is only one report downloaded during the 15 minute period the script still works and sends an email, but instead of the name of the report in the body of the email it says 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.
}
0

2 Answers 2

1

Change

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

to

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

and all similar cases.

PS is giving you either a single thing, or an array. You need it to always be an array, even if there's one thing, so you need to use the @() array constructor.

That's because this

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

changes depending on whether $files is an array (-like acts as a filter and returns a filtered array) vs whether it's a single thing (-like acts as a boolean operator and returns true/false).

Sign up to request clarification or add additional context in comments.

Comments

1

I am sure there is a more elegant way of doing this, ignore your $files variable and reuse the code inside it:

Replace

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

With

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

On my Test it worked with a single file and two files.

So in your example you would need to replace:

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

with

# 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*"

Note, you will also have to change:

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

to:

If ($DailyOpen)
If ($DailyProduct)

As now these variables will not be initialized if there is no content matching your pattern.

You can also do away with the $file variable now

:-)

enjoy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.