Bulk test many Servers in WinSCP.ini - How to?

Advertisement

Acen
Joined:
Posts:
1
Location:
DE

Bulk test many Servers in WinSCP.ini - How to?

Does anyone know how to test servers which are saved in WinSCP.ini in bulk? I have made a script with ChatGPT but maybe there is a better solution to this?!

I attach the script to this message :)

WinSCP_Bulk_tester.ps1:
# Set paths
$iniPath = "C:\Users\acen\Documents\WinSCP.ini"
$winscpPath = "C:\Program Files (x86)\WinSCP\winscp.com"  # Adjust if needed
$logFile = "C:\Users\acen\Documents\WinSCP_bulk_check.log"
$newIniPath = "C:\Users\acen\Documents\WinSCP_successful.ini"
$successTxtPath = "C:\Users\acen\Documents\WinSCP_successful.txt"
 
# Clear output files at start
Clear-Content $logFile -ErrorAction SilentlyContinue
Clear-Content $newIniPath -ErrorAction SilentlyContinue
Clear-Content $successTxtPath -ErrorAction SilentlyContinue
 
$iniLines = Get-Content $iniPath
 
$sessionSections = $iniLines | Select-String '^\[Sessions\\(.+?)\]' | ForEach-Object {
    @{
        Name = $_.Matches[0].Groups[1].Value
        Start = $_.LineNumber
    }
}
 
for ($i = 0; $i -lt $sessionSections.Count; $i++) {
    if ($i -lt $sessionSections.Count - 1) {
        $sessionSections[$i].End = $sessionSections[$i+1].Start - 1
    } else {
        $sessionSections[$i].End = $iniLines.Count
    }
}
 
foreach ($section in $sessionSections) {
    $sessionName = $section.Name
    Write-Host "Testing $sessionName..."
 
    $sessionLines = $iniLines[($section.Start)..($section.End-1)]
 
    # Extract details
    $hostname = ($sessionLines | Where-Object { $_ -match "^HostName=" }) -replace "^HostName=", ""
    $user = ($sessionLines | Where-Object { $_ -match "^UserName=" }) -replace "^UserName=", ""
    $password = ($sessionLines | Where-Object { $_ -match "^Password=" }) -replace "^Password=", ""
 
    # Always use SCP protocol
    $protocolStr = "scp"
 
    # Build open command with hostkey=*
    if ($password -ne "") {
        $openCmd = "open ${protocolStr}://${user}:${password}@${hostname}/ -hostkey=*"
    } else {
        $openCmd = "open ${protocolStr}://${user}@${hostname}/ -hostkey=*"
    }
 
    # Run WinSCP and capture output
    $result = & "$winscpPath" /command "$openCmd" "exit" 2>&1
 
    # Append session header and result to log file
    Add-Content $logFile "`n===== $sessionName ====="
    Add-Content $logFile $result
 
    # Improved error handling and output
    if ($result -match "Authentication failed" -or $result -match "Access denied" -or $result -match "Zugriff verweigert" -or $result -match "Anmeldung fehlgeschlagen") {
        Write-Host "${sessionName}: Credential FAILED" -ForegroundColor Red
    } elseif ($result -match "Connection failed" -or $result -match "Timeout" -or $result -match "Host not found" -or $result -match "Network error" -or $result -match "Connection refused" -or $result -match "Could not resolve hostname" -or $result -match "No route to host") {
        Write-Host "${sessionName}: Server UNREACHABLE" -ForegroundColor Red
    } elseif ($result -match "Session opened" -or $result -match "Sitzung gestartet") {
        Write-Host "${sessionName}: Connection OK" -ForegroundColor Green
        # Write session section to new INI file
        Add-Content $newIniPath "`n[Sessions\$sessionName]"
        foreach ($line in $sessionLines) {
            Add-Content $newIniPath $line
        }
        # Write session name to TXT file
        Add-Content $successTxtPath $sessionName
    } elseif ($result -match "Die Gegenstelle sendet seit mehr als 15 Sekunden keine Daten mehr") {
        Write-Host "${sessionName}: Connection hangs, possible network/server issue" -ForegroundColor Red
    } elseif ($result -match "SFTP kann nicht initialisiert werden") {
        Write-Host "${sessionName}: SFTP not available, SCP should work if server supports it" -ForegroundColor Red
    } else {
        Write-Host "${sessionName}: Unknown result, check log file" -ForegroundColor Red
    }
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
42,772
Location:
Prague, Czechia

Re: Bulk test many Servers in WinSCP.ini - How to?

Sorry, there's no functionality like this in WinSCP.
And unfortunately, I do not think your code works, as the Password field is encoded.

Reply with quote

Advertisement

You can post new topics in this forum