.net - 脚本中忽略的 else 部分
问题描述
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"
}
}
解决方案
推荐阅读
- ios - 如何在 Swift 中更改 UICollectionView 的 targetContentOffset?
- javascript - 如何将自定义类添加到ckeditor上传的图像
- virtual-machine - 当我单击桌面时,我在 VMWare VSphere Client 中有鼠标偏移
- python-3.x - ML 模型无法正确预测
- mysql - 如何查询出现在一个表中的记录,但不在其他两个表中
- javascript - golang javascript对象等效
- python - 如果 name/id 是前几年的条目,如何检查每个时间序列条目?
- python - Python / pynput:如何在变化时获得相对鼠标位置?
- html - 如何在使用表单和脚本发送电子邮件后隐藏确认屏幕
- sql - FROM 子句中的 Delphi 语法错误,但是 - 没有 FROM 子句