Hello, connect/ get

Advertisement

sys
Joined:
Posts:
3
Location:
Cluj_Napoca

Hello, connect/ get

Hello guys, I have this powershell code.
Works good but when add more line in db.csv doesn't work with return error code:
Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
Using username "username".
Authentication failed."
At C:\Users\me\Desktop\testeScript\CollectLog.ps1:41 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException

When I have per example only 2 line in db.csv the script works very well
HostName,IP
name1,10.10.1.1
name2,10.10.1.2
I try with 19 hostname and Ip addr line in CSV doc and works, but when I add only 1 more stopping works.

19 works
20+ doesn't work..

Any idea? (Thank you and sorry for my english)

Add-Type -Path "WinSCPnet.dll"
 
$Name = @()
$ip = @()
 
Import-Csv db.csv |`
    ForEach-Object {
        $Name += $_.HostName
        $ip += $_.IP
    }
 
$inputID = Read-Host -Prompt "Type ID"
 
if ($Name -contains $inputNumber)
    {
    $Where = [array]::IndexOf($Name, $inputID)
    Write-Host "IP: " $ip[$Where]
    }
 
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "$ip"
    UserName = "username"
    Password = "password"
    GiveUpSecurityAndAcceptAnySshHostKey = "true"
}
 
$session = New-Object WinSCP.Session
 
try
{
    # Connect
    $session.Open($sessionOptions)
 
    # Transfer files
    $session.GetFiles("/C:/Program Files/Common Files/logs/Device.log", "E:\loguri\Log\Arhive\*").Check()
}
finally
{
    $session.Dispose()
}
 
Compress-Archive -Path "E:\loguri\Log\Arhive\Device.log" -DestinationPath "E:\loguri\Log\Arhive\$inputID.zip" -Update
Remove-Item -Path "E:\loguri\Log\Arhive\Device.log" -Force

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

Re: Hello, connect/ get

Please attach logs of the 19th (successful) and 20th (failed) connections.
And then a log of a session with the same settings as the 20th connection, but when used as the only connection (supposedly successful).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

sys
Joined:
Posts:
3
Location:
Cluj_Napoca

Re: Hello, connect/ get

Hello Martin,
First of all , thank you in advance for your time and your reply :D

I Attach logs
1x log with Fail.
1x log with Succeed.

Have a nice day :)
  • winscpOK.log (12.53 KB, Private file)
  • winscpFAIL.log (5.88 KB, Private file)

Reply with quote

martin
Site Admin
martin avatar

Re: Hello, connect/ get

Thanks for the logs.
I have sent you an email with a debug version of WinSCP to the address you have used to register on this forum.

Reply with quote

sys
Joined:
Posts:
3
Location:
Cluj_Napoca

Hello Martin,
Thank you for your interes.
I fix the problem with new method search in db.csv.
I leave my updated script ; maybe can help someone

$db = import-csv -Path "C:\Program Files (x86)\WinSCP\db.csv"
 
$inputID = Read-Host -Prompt "ID"
 
$entry = $db -match $inputID
 
    $User = "username"
    $Password = "password"
    $Command = "command if you want (SSH)"
 
    $secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
    $Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
 
Get-SSHTrustedHost | Remove-SSHTrustedHost
 
$SessionID = New-SSHSession -ComputerName $entry.IP -Credential $Credentials -AcceptKey:$true
 
Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command
 
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = $entry.IP
    UserName = "$User"
    Password = "$Password"
    GiveUpSecurityAndAcceptAnySshHostKey = "true"
}

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum