diff --git a/Test-Login.ps1 b/Test-Login.ps1 index f0457d1..2f95897 100644 --- a/Test-Login.ps1 +++ b/Test-Login.ps1 @@ -415,8 +415,48 @@ if (-not $connected -and (Test-Path $logFile)) { Write-Host "" Write-Host ("=" * 64) -ForegroundColor Cyan if ($connected) { - Write-Host " Login works. The account is fine - so if a script using these" -ForegroundColor Green - Write-Host " same details still fails, the script is mangling one of them." -ForegroundColor Green + Write-Host " Login works. These details are correct." -ForegroundColor Green + Write-Host ("=" * 64) -ForegroundColor Cyan + + # Getting a working password into a script is its own trap, so hand back + # the literal to paste rather than leaving the quoting to chance. + $needsCare = $password -match '[\$`]' + if ($needsCare -or $password -match "'" -or $password -match '%') { + Write-Host "" + Write-Host " Putting this password into a PowerShell script" -ForegroundColor Cyan + Write-Host (" " + ("-" * 60)) -ForegroundColor Cyan + Write-Host "" + Write-Host " Use exactly this line, single quotes included:" -ForegroundColor White + Write-Host "" + Write-Host " `$Password = $("'" + ($password -replace "'", "''") + "'")" -ForegroundColor Green + Write-Host "" + if ($needsCare) { + Write-Host " This password contains a `$ or a backtick. Inside DOUBLE" -ForegroundColor Yellow + Write-Host " quotes PowerShell treats that as a variable and silently" -ForegroundColor Yellow + Write-Host " sends something shorter - the login then fails even though" -ForegroundColor Yellow + Write-Host " the password is right. Single quotes send it untouched." -ForegroundColor Yellow + } + if ($password -match "'") { + Write-Host " The password contains an apostrophe, so it is doubled in" -ForegroundColor Yellow + Write-Host " the line above. That is correct - it stays one character." -ForegroundColor Yellow + } + if ($password -match '%') { + Write-Host " The password contains a %. That is fine in PowerShell, but" -ForegroundColor Yellow + Write-Host " in a .bat file it must be written as %% instead." -ForegroundColor Yellow + } + Write-Host "" + Write-Host " Safer still: keep it out of the script. Put the password on" -ForegroundColor Gray + Write-Host " the first line of its own file and read it back - then no" -ForegroundColor Gray + Write-Host " quoting rules apply to it at all:" -ForegroundColor Gray + Write-Host ' $Password = Get-Content pw.txt -TotalCount 1' -ForegroundColor Gray + } else { + Write-Host "" + Write-Host " Nothing in this password needs special quoting in a script." -ForegroundColor Green + } + Write-Host "" + Write-Host ("=" * 64) -ForegroundColor Cyan + Write-Host " If a script using these same details still fails, the script is" -ForegroundColor Green + Write-Host " changing one of them on the way through." -ForegroundColor Green } else { Write-Host " Login failed. Compare against the WinSCP window: File > Session" -ForegroundColor Yellow Write-Host " > Generate Session URL/Code > .NET assembly code. That shows the" -ForegroundColor Yellow