Make the login test work next to any uploader, not just this one

Melissa is going with the script she built with ChatGPT, so the
diagnostic has to stand on its own. It now finds WinSCPnet.dll wherever
it is - a WinSCP folder beside the script, one folder up, a normal
WinSCP install under Program Files, or anywhere below the script - and
asks for the path only if all of that misses.

Unblock-File is registered but throws on Linux, so Get-Command was not a
usable guard for it; wrapped instead.
This commit is contained in:
2026-08-30 20:33:03 -05:00
parent d499721132
commit 0747612810
+42 -5
View File
@@ -3,7 +3,6 @@
# with it. Called by test-login.bat.
$scriptRoot = Split-Path -Parent $PSCommandPath
$winscpDll = Join-Path $scriptRoot "WinSCP\WinSCPnet.dll"
function Read-Answer($question, $default) {
while ($true) {
@@ -31,18 +30,56 @@ Write-Host ("=" * 64) -ForegroundColor Cyan
Write-Host ""
Write-Host "Type the details exactly as they work in WinSCP or FileZilla." -ForegroundColor Gray
if (-not (Test-Path $winscpDll)) {
# Find WinSCPnet.dll wherever it happens to live - this script is meant to be
# dropped next to whatever uploader is in use, not just this one.
function Find-WinSCPDll {
$parent = Split-Path -Parent $scriptRoot
$candidates = @(
(Join-Path $scriptRoot "WinSCP\WinSCPnet.dll"),
(Join-Path $scriptRoot "WinSCPnet.dll"),
(Join-Path $parent "WinSCP\WinSCPnet.dll"),
(Join-Path $parent "WinSCPnet.dll"),
"C:\Program Files (x86)\WinSCP\WinSCPnet.dll",
"C:\Program Files\WinSCP\WinSCPnet.dll"
)
foreach ($c in $candidates) {
if (Test-Path $c) { return $c }
}
# Anywhere under the folder this script sits in.
$found = Get-ChildItem -Path $scriptRoot -Filter "WinSCPnet.dll" -Recurse -File -ErrorAction SilentlyContinue |
Select-Object -First 1
if ($found) { return $found.FullName }
return $null
}
$winscpDll = Find-WinSCPDll
if (-not $winscpDll) {
Write-Host ""
Write-Host "WinSCPnet.dll not found at $winscpDll" -ForegroundColor Red
Write-Host "Run setup.bat first, or copy this script next to the WinSCP folder." -ForegroundColor Yellow
Write-Host "Could not find WinSCPnet.dll automatically." -ForegroundColor Yellow
Write-Host "It sits either in a WinSCP folder next to your upload script, or" -ForegroundColor Gray
Write-Host "in C:\Program Files (x86)\WinSCP\ if WinSCP is installed normally." -ForegroundColor Gray
Write-Host ""
$winscpDll = Read-Answer "Full path to WinSCPnet.dll" $null
if (-not (Test-Path $winscpDll)) {
Write-Host "Nothing at that path." -ForegroundColor Red
exit 1
}
Get-ChildItem -Path (Join-Path $scriptRoot "WinSCP") -Recurse -File |
}
Write-Host ""
Write-Host "Using $winscpDll" -ForegroundColor DarkGray
# Windows tags files copied from elsewhere as untrusted, which stops Add-Type
# loading them. Harmless everywhere else, so failure here is not worth a word.
try {
Get-ChildItem -Path (Split-Path -Parent $winscpDll) -Recurse -File -ErrorAction SilentlyContinue |
Unblock-File -ErrorAction SilentlyContinue
} catch { }
try {
Add-Type -Path $winscpDll
} catch {
Write-Host "Could not load WinSCPnet.dll - $($_.Exception.Message)" -ForegroundColor Red
Write-Host "If it says the operation is not supported, Windows has the file" -ForegroundColor Yellow
Write-Host "blocked: right-click it, Properties, tick Unblock, OK." -ForegroundColor Yellow
exit 1
}