I have a powershell script that is using curl to upload a file to a website.
I put in error handling, but it doesn’t seem to do anything.
Whenever I attempt to upload a particular file I get back the following error:
EXCEPTION: Internal Server Error. This would be fine if I could catch the error and do something about it. What I’d like to do is to move it to a quarantine folder, because each time we restart the script, it finds that same file and barfs again, over and over and over.
Why is powershell crashing and not catching the error?
Here is the code:
while($true) {
Start-Sleep -s 1
$files = Get-ChildItem "C:UsersPublicCURLin*" -Include *.csv, *.xlsx, *.xls, *.tmp
$errorPath = "C:UsersPublicCURLerror"
foreach ($f in $files) {
if ($null -eq $f) { continue }
Write-Host $f.Name
$name = "qqfile=@" + $f.FullName
try {
$ret = ./curl -s -F "org_ID=1948" -F "ID=000" -F "token=1234" -F $name https://www.somewhere.com/auto_upload
if ($ret -match "EXCEPTION") {
Write-Host $ret -BackgroundColor Red
return
}
if ($ret -match "SUCCESS" -or $ret -match "HEADER" -or $ret -match "PARSING" ) {
if ($ret -match "SUCCESS") {
Write-Host $ret -ForegroundColor Green
}
if ($ret -match "HEADER") {
Write-Host $ret -ForegroundColor Yellow
}
if ($ret -match "PARSING") {
Write-Host $ret -ForegroundColor Red
}
Move-Item $f.FullName "V:Inspect ProgramsuploadsCMM" -force
}
else {
Write-Host "Waiting for connection ..."
Start-Sleep -s 10
}
}
catch {
$message = "ERROR! File " + $f.Name + "caused an error. It is being moved to the error directory. Date: " + $current_date
Add-Content -Path (Join-Path $PSScriptRoot "log.txt") -Value $message
Move-Item $f.FullName $errorPath -force
}
}
}
I was trying to have powershell catch the exception instead of blow chunks.