使if
/ else
语句无法正常工作会遇到一些麻烦。当我准备好它时,循环永远不会继续到else
,而只会处理基于if
的所有文件。有什么我想念的吗?
我想做的是,将扩展201和205的所有录音复制并不通过电子邮件发送,将要通过电子邮件发送的其余部分复制。下面的代码将遍历所有文件,但是如果其中一个文件存在某些内容,它将为所有后续文件保留相同的$ext
变量。
#Setting up log rotation
$LogDate = Get-Date -Format yyyy-MM-dd
$Logfile = "C:\Scripts\Inbound-$LogDate.log"
$logcount = 10
function LogWrite {
Param ([string]$logstring)
Add-Content $Logfile -Value $logstring
}
function Get-TimeStamp {
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}
#$ErrorActionPreference = "Stop"
$SourceFolder = '\\fileserver\Recordings\autorecords'
# Delimiters for use on breaking apart the text strings
$delim = '-'
$delim2 = '@'
#email Information
$Port = "25"
$SMTPUsername = "domain\username"
#Secure string for password to avoid plaintext passwords
$EncryptedPasswordFile = "C:\Scripts\password.txt"
$EmailCredential = New-Object -TypeName Management.Automation.PSCredential($SMTPUsername, (Get-Content $EncryptedPasswordFile | ConvertTo-SecureString))
#Once sent, send emailed files here to be purged at a later date
$ProcFolder = '\\fileserver\Recordings\processed'
#List of extensions & Associated email addresses
$extensions = @{
'999' = 'bill@test.com';
'201' = 'Dude@test.com';
'203' = 'PersoN@test.com';
'204' = 'emailaddress';
'205' = 'emailaddress';
}
Push-Location $SourceFolder
$File = Get-ChildItem $SourceFolder -Name *.wav -Recurse -File
$File | Foreach-object {
$nameArray = $_.Split($delim)
$newName = $nameArray[2]+" "+($nameArray[0].substring(0,8))
$ext = $nameArray[3]
#If statement will just move recordings to 201 or 205 to the processed folder instead of leaving them in the recordings
if (($ext = '201') -or ($ext = '205')) {
Move-Item -Path $SourceFolder\$_ -Destination $ProcFolder
LogWrite "$(Get-TimeStamp) Moved recording $_ for $ext to processed"
} else {
$callerID = $nameArray[2]
$Datestamp = ($nameArray[0].Substring(0, 8))
$emailAddress = $extensions[$ext]
$FirstNameArray = $emailAddress.Split($delim2)
$FirstName = $FirstNameArray[0]
$SMTPProperties = @{
To = $emailAddress
From = 'voicemails@test.com'
Body = 'New Call Recording'
BodyAsHtml = $true
subject = "Call Recording on $Datestamp from ($CallerID) to $FirstName"
smtpserver = 'mailserver'
attachment = $_
Port = '25'
}
Send-MailMessage @SMTPProperties
LogWrite "$(Get-TimeStamp) Sent email to $emailAddress"
Move-Item -Path $SourceFolder\$_ -Destination $ProcFolder
LogWrite "$(Get-TimeStamp) Moved recording $_ for $ext to processed"
}
}