I’m new in powershell but I need to create a file download from mysite.
The main point is that only download the file if the file exists in the given folder with the given name.
If it doesn’t exist, don’t download.
instead of the System.Net.WebClient and wrap it with a proper
to detect errors if the file is missing.
And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
I can download several files from the website successfully. (cv, csv, mot) . But I want to download the mot file ($MOTuri), only if it actually exists.
Currently, it always creates the mot file in the destination folder, even if it does not exist on my website.
How can I check the availability of the file, instead of Test-Path?
# Download all CV and CSV and MOT
foreach ($apply in $applies) {
try {
$CVuri = 'https://mysite.hu/public/karrieroneletrajz/' + $apply.file_id + '.' + $apply.file_extension
$MOTuri = 'https://mysite.hu/public/karrieroneletrajz/' + 'mot-' + $apply.file_id + '.' + $apply.file_extension
$CSVuri = 'https://mysite.hu/public/karrieroneletrajz/_csv/' + $apply.id + '.csv'
$webRequest = [System.Net.HttpWebRequest]::Create($CVuri);
$webRequest.Credentials = $cred;
$webResponse = $webRequest.GetResponse()
$redirectedUri = $webResponse.ResponseUri.AbsoluteUri;
$webResponse.Dispose() # Connection must be closed, before downloading process
# The file does not exist, it's been redirected. In this server, the 404 error does not exist.. In this case, it redirect to main page, and it gives back 200 OK
if (!($CVuri -eq $redirectedUri)) {
$notFoundEx = [Net.WebException]::new("$CVuri was not found! (Custom redirect...)", [System.Net.WebExceptionStatus]::UnknownError)
throw $notFoundEx;
}
$webClient.DownloadFile($CVuri, 'C:\portal\uploads\cv\cv-' + $apply.name + '-' + $apply.id + '.' + $apply.file_extension);
$WantFile = $MOTuri
$FileExists = Test-Path $WantFile
If ($FileExists -eq $True) { $webClient.DownloadFile($MOTuri, 'C:\portal\uploads\cv\mot-' + $apply.name + '-' + $apply.id + '.' + $apply.file_extension);}
$webClient.DownloadFile($CSVuri, 'C:\portal\uploads\csv\csv-' + $apply.id + '.csv');
$MOTuri | Out-File c:\temp\debug.txt
$FileExists | Out-File c:\temp\exist.txt
}
catch [Net.WebException] {
$errorObj = New-Object -Type PSObject -Property @{
'description' = $_.ToString()
'apply_id' = $apply.id
'status_code' = $_.Exception.Response.StatusCode # $errorObj.status_code = $_.Exception.Status
}
$errors.Add($errorObj);
}
catch {
$errorObj = New-Object -Type PSObject -Property @{
'description' = 'default catch: ' + $_.ToString()
'apply_id' = $apply.id
}
$errors.Add($errorObj);
}
}
Have you tried my suggestion? With using Invoke-WebRequest you’ll get a proper response with a statuscode. And when you wrap this SINGLE command additionally in a try catch block you can tell exactly where the error occurs if it does.