Public Access
- Unblock-File the extracted WinSCP files after unzip: Windows tags downloaded files with Mark-of-the-Web, which makes Add-Type refuse to load WinSCPnet.dll (FileLoadException 0x80131515) until unblocked. - run.bat passed -EntityDir "%~dp0", but %~dp0 always ends in a trailing backslash. cmd's argument parser treats a lone backslash before a closing quote as an escaped literal quote rather than the string terminator, so PowerShell received the path with a stray " character baked in (Entities\CAMBS" instead of Entities\CAMBS), breaking Test-Path. Strip the trailing backslash before quoting it.
59 lines
2.7 KiB
PowerShell
59 lines
2.7 KiB
PowerShell
# Downloads and unpacks the WinSCP automation package engine.ps1 needs.
|
|
# Called by setup.bat. Safe to re-run.
|
|
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
|
|
|
$url = "https://winscp.net/download/WinSCP-6.3.6-Automation.zip"
|
|
$zip = Join-Path $env:TEMP "winscp.zip"
|
|
$dest = Join-Path $PSScriptRoot "WinSCP"
|
|
|
|
if (Test-Path $zip) { Remove-Item $zip -Force }
|
|
|
|
Write-Host "Downloading WinSCP from $url ..."
|
|
try {
|
|
# winscp.net serves a human "please wait" landing page instead of the real
|
|
# file unless the User-Agent looks like a known download tool (curl/wget) -
|
|
# PowerShell's own default UA gets the landing page every time.
|
|
Invoke-WebRequest -Uri $url -OutFile $zip -UseBasicParsing -UserAgent "curl/8.0.1"
|
|
} catch {
|
|
Write-Host "ERROR: Download failed - $($_.Exception.Message)" -ForegroundColor Red
|
|
Write-Host "Check your internet connection, or ask IT if winscp.net / sourceforge.net are blocked." -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
$fileInfo = Get-Item $zip
|
|
if ($fileInfo.Length -lt 1MB) {
|
|
Write-Host "ERROR: Downloaded file is only $($fileInfo.Length) bytes - too small to be WinSCP." -ForegroundColor Red
|
|
Write-Host "This is usually winscp.net serving its 'please wait' landing page instead of the real file, or antivirus/a firewall intercepting the download." -ForegroundColor Yellow
|
|
|
|
$preview = Get-Content -Path $zip -TotalCount 15 -ErrorAction SilentlyContinue
|
|
if ($preview) {
|
|
Write-Host ""
|
|
Write-Host "--- What was actually downloaded (first 15 lines, for troubleshooting) ---" -ForegroundColor Yellow
|
|
$preview | ForEach-Object { Write-Host $_ }
|
|
Write-Host "--- end ---" -ForegroundColor Yellow
|
|
Write-Host ""
|
|
}
|
|
|
|
Write-Host "Try running setup.bat again. If it keeps happening, ask IT whether winscp.net / sourceforge.net downloads are being blocked, or download WinSCP-6.3.6-Automation.zip manually in a browser and unzip it into:" -ForegroundColor Yellow
|
|
Write-Host " $dest" -ForegroundColor Yellow
|
|
Remove-Item $zip -Force
|
|
exit 1
|
|
}
|
|
|
|
try {
|
|
Expand-Archive -Path $zip -DestinationPath $dest -Force
|
|
} catch {
|
|
Write-Host "ERROR: Could not unzip the download - $($_.Exception.Message)" -ForegroundColor Red
|
|
Write-Host "The downloaded file was likely corrupted or cut short in transit. Try running setup.bat again." -ForegroundColor Yellow
|
|
Write-Host "Left the bad file at $zip in case you want to look at it." -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
# Windows tags downloaded files as untrusted ("Mark of the Web"), which blocks
|
|
# Add-Type from loading WinSCPnet.dll later. Unblock everything we just extracted.
|
|
Get-ChildItem -Path $dest -Recurse -File | Unblock-File
|
|
|
|
Remove-Item $zip -Force
|
|
Write-Host "WinSCP ready." -ForegroundColor Green
|