diff --git a/Test-Login.ps1 b/Test-Login.ps1 index 5d0ea50..b371845 100644 --- a/Test-Login.ps1 +++ b/Test-Login.ps1 @@ -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)) { - 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 - exit 1 +# 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 } -Get-ChildItem -Path (Join-Path $scriptRoot "WinSCP") -Recurse -File | - Unblock-File -ErrorAction SilentlyContinue + +$winscpDll = Find-WinSCPDll +if (-not $winscpDll) { + Write-Host "" + 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 + } +} +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 }