我有一个脚本,它通过 JSON 配置文件通过一堆不同的帐户 ftp 凭据进行 foreach。如果用户在第三方站点上更改 FTP 密码,我完全希望出现“找不到文件”类型的错误,甚至有时会出现“错误,密码错误”。
脚本,当它自己(通过调度程序或通过 ISE)运行时,只会捕获任何错误,将它们写入我们的数据库并通过团队提醒适当的人,然后脚本继续并转到 foreach 中的下一个 JSON 对象循环。
问题是,当我将它作为 SQL 代理 powershell 作业(或 CMDExec)运行时,单个错误会停止整个脚本。我读到的关于此的所有内容似乎都在说 sql 实际上应该继续执行脚本,但事实并非如此...
如果 JSON 配置的“帐户”之一遇到问题,我会尝试让脚本继续运行。这是脚本:
#region setup ================================== SETUP BLOCK ==================================#
#Email Recipients
$Receivers = @{
"teams channel" = "7e8bfd86.MYDOMAIN.com@amer.teams.ms"
}
#Logging
$LogSourceID = 50
$LogSourceMachineName = 'CAFeed_UO_DB02'
#JsonPull
$json = Get-Content -raw -encoding "UTF8" -path "D:\Data\feeds\CA\cfg\CAFeedConfig_migratetest.JSON" | convertfrom-json
$accounts = $json
#StaticVariables
$uo_path = 'D:\data\feeds\CA\uo\'
#VerboseLogging
Start-Transcript -path "D:\data\feeds\CA\scripts\uo_verboselog.txt" -Append
#endregion setup #================================== SETUP BLOCK ==================================#
foreach ($account in $accounts) #Enable This Line to Run through all accounts
#foreach ($account in $accounts | Where-Object {$_.account -eq "ACCOUNTNAME"}) #Enable This line to run through only specified Accounts
#This IF Statement tests to see if the update file exists, if it does not, a database log is written
{
if ((Test-Path -PathType Leaf $account.uo_sourcefile) -eq $false)
{write-host ($account.uo_sourcefile + "not detected")
#Assemble the Query
$sqlresult = "CA_UO File Not Detected:" + $account.uo_sourcefile
$sqlcontext = ("NoFile")
$starttime = (get-date).ToString()
$endtime = (get-date).ToString()
write-BelamiDBLog -resultRecord $sqlresult -contextRecord $sqlcontext -machineNameRecord $LogSourceMachineName -sourceID $logSourceID -starttime $starttime -endtime $endtime
}
else
{
#region ftp
$ErrorActionPreference = "Continue"
write-host $account.uo_ftpdest $account.account, $account.username, $account.password, $account.uo_sourcefile
# create the FtpWebRequest and configure it
$ftp = [System.Net.FtpWebRequest]::Create($account.uo_ftpdest)
$ftp = [System.Net.FtpWebRequest]$ftp
# build authentication and connection
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential($account.username,$account.password)
$ftp.UseBinary = $true
$ftp.UsePassive = $true
$ftp.timeout = -1
#start a timer and error handling
$starttime = (get-date).ToString()
write-host "starting at $starttime"
# read in the file to upload as a byte array
$content = [System.IO.File]::ReadAllBytes($account.uo_sourcefile)
$ftp.ContentLength = $content.Length
write-host ("File Size: " + ($content.Length / 1000000)+"MB" )
# get the request stream, and write the bytes into it
$rs = $ftp.GetRequestStream()
$rs.Write($content, 0, $content.Length)
$rs.Close()
$rs.Dispose()
$endtime = (get-date).ToString()
write-host "done at $endtime"
$totaltime = NEW-TIMESPAN -Start $starttime -End $endtime
$xferrate = ($content.length / $totaltime.TotalSeconds / 1000000)
$xferrate = [math]::Round($xferrate,2)
write-host $xferrate "MB/Sec"
#endregion ftp
#region error handle
if ($error)
{
$sqlresult = ('CA_UO Transport Failed'+ $xferrate + 'MB/sec')
$sqlcontext = ($account.account + ' | ' + ($content.Length / 1000000) +'MB')
write-BelamiDBLog -resultRecord $sqlresult -contextRecord $sqlcontext -machineNameRecord $LogSourceMachineName -sourceID $logSourceID -starttime $starttime -endtime $endtime
$Parameters = @{
FromAddress = "CAFeedAlerts@MYDOMAIN.com"
ToAddress = "placeholder"
Subject = "CA_UO Failed"
Body = "CA_UO Failed with error $error"
Token = "REDACTED"
FromName = "CAFeed Alerts"
ToName = "placeholder"
}
Foreach ($Receiver in $Receivers.GetEnumerator()) {
$Parameters.ToName = $Receiver.name
$Parameters.ToAddress = $Receiver.value
Send-PSSendGridMail @Parameters
$error.Clear()
}
}
else
{
$sqlresult = ('CA_UO Transport Success '+ $xferrate + 'MB/sec')
$sqlcontext = ($account.account + ' | ' + ($content.Length / 1000000) +'MB')
write-BelamiDBLog -resultRecord $sqlresult -contextRecord $sqlcontext -machineNameRecord $LogSourceMachineName -sourceID $logSourceID -starttime $starttime -endtime $endtime
}
#ArchiveRemainingFile
$datestr = get-date -Format MMddhhmm
Compress-Archive -Path $account.uo_sourcefile -DestinationPath ('D:\data\feeds\CA\uo_archive\' + $account.account + $datestr + '.zip') -ErrorAction SilentlyContinue
write-host ("Deleting" + $account.uo_sourcefile)
remove-item -LiteralPath $account.uo_sourcefile -ErrorAction SilentlyContinue
}
# be sure to clean up after ourselves and get ready for next block
Clear-Variable -Name starttime,endtime,sqlresult,sqlcontext,ftp,content,datestr -ErrorAction SilentlyContinue
$error.Clear()
start-sleep -s 2
}
似乎在此引发的任何错误都完全停止了作业,并且脚本完全退出而不继续。我将 erroractionpreference 设置为在几乎所有地方继续或静默继续。我不明白为什么会这样。
想法?