I have a script that runs against a directory and looks for files in it. Based on a portion of the filename containing the internal extension, it will then email the file to the associated user. I have it 90% working, it will email the correct person the correct file, but the variables in the email subject line are wrong. For example, a file intended for Bob goes to Bob, but the email subject line indicates Joe as well as the CallerID of Joe's call.
$Path = 'c:\test2'
$delim = '-'
$delim2 = '@'
$Port = "587"
$SMTPUsername = "[email protected]"
$EncryptedPasswordFile = "c:\test\voicemails.com.securestring"
$SecureStringPassword = Get-Content -Path $EncryptedPasswordFile | ConvertTo-SecureString
$EmailCredential = New-Object -TypeName Management.Automation.PSCredential($SMTPUsername,$SecureStringPassword )
$DestFolder = 'C:\test2\Processed'
#phone extensions & associated email address
$extensions = @{
'1000'= '[email protected]';
'23' = '[email protected]';
'765'= '[email protected]'
}
$File = Get-ChildItem $Path -Name
$File | Foreach-object {
$FirstName = $null
$nameArray = $_.Split($delim)
$newName = $nameArray[2]+" "+($nameArray[0].substring(0,8))
$ext = $nameArray[3]
$callerID = $nameArray[2]
$Datestamp = ($nameArray[0].substring(0,8))
$emailAddress = $extensions[$ext]
$FirstNameArray = $emailAddress.Split($delim2)
$FirstName = $FirstNameArray[0]
$SMTPMessage = @{
To = $emailAddress
From = '[email protected]'
Subject = $Subject
Smtpserver = '192.168.1.9'
}
$SMTPBody = 'Voicemail redirected message'
$Subject = "Call Recording on $Datestamp from ($CallerID) to $FirstName"
Send-MailMessage -Credential $EmailCredential @SMTPMessage -Body $SMTPBody -Attachments $_ -Port $Port
}
Some sample filenames I'm running it against:
20180705072823-1530804495.22-15555552367-1000-Inbound.wav
20180705072823-1530804495.22-15555551234-23-Inbound.wav
20180705072823-1530804495.22-15555557791-765-Inbound.wav
I've tried to set the values to $null for each loop but doesn't make a difference.