首页 > 解决方案 > 脚本中忽略的 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"  
    }
}

标签: .netpowershell

解决方案


推荐阅读