This commit is contained in:
RoyceDa
2026-02-18 16:21:26 +02:00
parent 0ac5ecee8a
commit 7472747a9e
2 changed files with 5 additions and 16 deletions

View File

@@ -160,19 +160,8 @@ function Upload-ToSFTP {
[string]$WinSCPExe
)
# If password came URL-encoded (e.g., %23 for #), decode once
if ($Pass -match '%[0-9A-Fa-f]{2}') {
$Pass = [System.Net.WebUtility]::UrlDecode($Pass)
}
# Escape special characters in password that could break URL or WinSCP syntax
$escapedPassword = $Pass
$escapedPassword = $escapedPassword -replace '@', '%40'
$escapedPassword = $escapedPassword -replace ':', '%3A'
$escapedPassword = $escapedPassword -replace '#', '%23'
$escapedPassword = $escapedPassword -replace '\$', '%24'
$escapedPassword = $escapedPassword -replace '`', '%60'
$escapedPassword = $escapedPassword -replace '&', '%26'
# Decode once if URL-encoded; we will pass plain password via -password switch (no extra encoding needed)
$decodedPassword = if ($Pass -match '%[0-9A-Fa-f]{2}') { [System.Net.WebUtility]::UrlDecode($Pass) } else { $Pass }
# Create temporary file paths BEFORE script content (needed for variable expansion)
$timestamp = Get-Date -Format 'yyyyMMdd_HHmmss_fff'
@@ -193,8 +182,8 @@ option echo off
option reconnecttime 3
"@
# Add connection string with auto-accept of host key
$scriptContent += "`r`nopen sftp://$User`:$escapedPassword@$Server`:$PortNum -hostkey=`"*`"`r`n"
# Add connection string with auto-accept of host key; pass password via -password to avoid URL encoding issues
$scriptContent += "`r`nopen sftp://$User@$Server`:$PortNum/ -password=`"$decodedPassword`" -hostkey=`"*`"`r`n"
# Try to clear remote folder by removing all .exe files (ignore if none exist)
$scriptContent += "call rm -f $RemotePath/*.exe`r`n"