Re-TRy after "unexpectedly closed network connection"
Hi, i'm using PowerShell with Winscpnet.dll, mi goal is get a file from an AIX server 6.1 , so i found an script in yours Examples (Thanks for that, becouse i'm newbie with PS) and adapted, so ramdomly times i got the error message "server unexpectedly closed network connection", my question is, i could have an example of re-connect script after that message with an x quantities of retries (sorry for my spell) for example re-try 3 more times?.
This is part of my script
try
{
#Cargar La Dll
Add-Type -path "d:\xyzn\xyz\WSCP\WinSCPnet.dll"
#Configurar datos de sesion
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "1.1.1.1"
$sessionOptions.UserName = "xyz"
$SessionOptions.SshPrivateKeyPath = "d:\xyzn\xyz\WSCP\t_acme.ppk"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 ba:3d:99:dddd:0:4d:34:6c:121:1e:75:90"
$session = New-Object WinSCP.Session
try
{
#Conectarse
$session.Open($sessionOptions)
#Bajar archivo
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Automatic
$transferResult = $session.GetFiles( $PRemote + $NomArch, $Plocal, $False, $transferOptions)
#Comprobamos si hay errores
$transferResult.Check()
#Muestra Resultado
foreach ($transfer in $transferResult.Transfers)
{
if ( $transfer.Error -eq $Null )
{
#Write-Host ("Bajada de {0} succeeded" -f $transfer.FileName)
LogWrite ("Bajada de {0} succeeded" -f $transfer.FileName)
#Enviamos mail
if (test-path $sfile)
{
$From = """AXC"" acme@grupoacme.com"
[string[]]$Dest = "xyz@xxx.com"
[string[]]$Cco = "xyz@xxx.com"
[string[]]$Bmil = "xyz@xxx.com"
$smtpserver = "197.24.23.1"
$Subject = "Estado ACME"
#Con Out-String formateamos el texto
$body = Get-Content $sfile | Out-String;
$Cant = Get-Content $sfile | Measure-Object -line
# write-host $Cant.lines
$body2 = $style + (Get-Content $sfile | Select-Object -First 1 ) + "</FONT></P> "
# $body2 = $body2 + $style + (Get-Content $sfile)[2] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[3] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[4] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[5] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[6] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[7] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[8] + "</FONT></P> "
for ( $i=2; $i -le 8; $i++)
{
$body2 = $body2 + $style + (Get-Content $sfile)[$i] + "</FONT></P>"
}
$body2 = $body2 + $style + (Get-Content $sfile)[10] + "</FONT></P>"
for ( $i=11; $i -le $Cant.lines; $i++)
{
$body2 = $body2 + $sstyle + ((Get-Content $sfile)[$i] -replace " ", " ") + "</FONT></P>"
}
$body2 = $body2 + $style3 + $style3
$body2 = $body2 + $style4 + "<STRONG>Acme</STRONG></FONT></P>"
$body2 = $body2 + $style4 + "IT Acme deparment"
$body2 = $body2 + "monitor</FONT></P>"
$body2 = $body2 + $style4 + "Acme</FONT></P>"
$body2 = $body2 + $style4 + "Tel: (+52)(11) 888888888 Int: "
$body2 = $body2 + "7193/81/82</FONT></P>"
$body2 = $body2 + $style3
$body2 = $body2 + $style5 + "<STRONG>:: Por favor, piense en el medio ambiente "
$body2 = $body2 + "antes de imprimir este mensaje ::</STRONG></FONT></P>"
$enc = New-Object System.Text.utf8encoding
# foreach ($recipient in $to)
#{
#write-host "Enviando mail a $to"
LogWrite "Enviando mail a $to"
#Send-MailMessage -smtpServer $smtpserver -from $from -to $recipient -cc $Cco -bcc $Bmil -subject $subject -body $body2 –BodyAsHtml -encoding $enc
Send-MailMessage -smtpServer $smtpserver -from $from -to $Dest -cc $Cco -bcc $Bmil -subject $subject -body $body2 –BodyAsHtml -encoding $enc
LogWrite "Eliminando archivo"
Remove-Item $sfile
# }
}
else
{
#write-host "Archivo " + $file + " no encontrado "
LogWrite "Archivo " + $file + " no encontrado "
}
}
else
{
#writ-host ("Bajada de Archivo {0} fallo: {1}" -f
LogWrite ("Bajada de Archivo {0} fallo: {1}" -f
$transfer.FileName, $transfer.Error.Message )
#$transfer.FileName, $transfer.Error.Message )
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
LogWrite $_.Exception.Message
exit 1
}
This is part of my script
try
{
#Cargar La Dll
Add-Type -path "d:\xyzn\xyz\WSCP\WinSCPnet.dll"
#Configurar datos de sesion
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "1.1.1.1"
$sessionOptions.UserName = "xyz"
$SessionOptions.SshPrivateKeyPath = "d:\xyzn\xyz\WSCP\t_acme.ppk"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 ba:3d:99:dddd:0:4d:34:6c:121:1e:75:90"
$session = New-Object WinSCP.Session
try
{
#Conectarse
$session.Open($sessionOptions)
#Bajar archivo
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Automatic
$transferResult = $session.GetFiles( $PRemote + $NomArch, $Plocal, $False, $transferOptions)
#Comprobamos si hay errores
$transferResult.Check()
#Muestra Resultado
foreach ($transfer in $transferResult.Transfers)
{
if ( $transfer.Error -eq $Null )
{
#Write-Host ("Bajada de {0} succeeded" -f $transfer.FileName)
LogWrite ("Bajada de {0} succeeded" -f $transfer.FileName)
#Enviamos mail
if (test-path $sfile)
{
$From = """AXC"" acme@grupoacme.com"
[string[]]$Dest = "xyz@xxx.com"
[string[]]$Cco = "xyz@xxx.com"
[string[]]$Bmil = "xyz@xxx.com"
$smtpserver = "197.24.23.1"
$Subject = "Estado ACME"
#Con Out-String formateamos el texto
$body = Get-Content $sfile | Out-String;
$Cant = Get-Content $sfile | Measure-Object -line
# write-host $Cant.lines
$body2 = $style + (Get-Content $sfile | Select-Object -First 1 ) + "</FONT></P> "
# $body2 = $body2 + $style + (Get-Content $sfile)[2] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[3] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[4] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[5] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[6] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[7] + "</FONT></P>"
# $body2 = $body2 + $style + (Get-Content $sfile)[8] + "</FONT></P> "
for ( $i=2; $i -le 8; $i++)
{
$body2 = $body2 + $style + (Get-Content $sfile)[$i] + "</FONT></P>"
}
$body2 = $body2 + $style + (Get-Content $sfile)[10] + "</FONT></P>"
for ( $i=11; $i -le $Cant.lines; $i++)
{
$body2 = $body2 + $sstyle + ((Get-Content $sfile)[$i] -replace " ", " ") + "</FONT></P>"
}
$body2 = $body2 + $style3 + $style3
$body2 = $body2 + $style4 + "<STRONG>Acme</STRONG></FONT></P>"
$body2 = $body2 + $style4 + "IT Acme deparment"
$body2 = $body2 + "monitor</FONT></P>"
$body2 = $body2 + $style4 + "Acme</FONT></P>"
$body2 = $body2 + $style4 + "Tel: (+52)(11) 888888888 Int: "
$body2 = $body2 + "7193/81/82</FONT></P>"
$body2 = $body2 + $style3
$body2 = $body2 + $style5 + "<STRONG>:: Por favor, piense en el medio ambiente "
$body2 = $body2 + "antes de imprimir este mensaje ::</STRONG></FONT></P>"
$enc = New-Object System.Text.utf8encoding
# foreach ($recipient in $to)
#{
#write-host "Enviando mail a $to"
LogWrite "Enviando mail a $to"
#Send-MailMessage -smtpServer $smtpserver -from $from -to $recipient -cc $Cco -bcc $Bmil -subject $subject -body $body2 –BodyAsHtml -encoding $enc
Send-MailMessage -smtpServer $smtpserver -from $from -to $Dest -cc $Cco -bcc $Bmil -subject $subject -body $body2 –BodyAsHtml -encoding $enc
LogWrite "Eliminando archivo"
Remove-Item $sfile
# }
}
else
{
#write-host "Archivo " + $file + " no encontrado "
LogWrite "Archivo " + $file + " no encontrado "
}
}
else
{
#writ-host ("Bajada de Archivo {0} fallo: {1}" -f
LogWrite ("Bajada de Archivo {0} fallo: {1}" -f
$transfer.FileName, $transfer.Error.Message )
#$transfer.FileName, $transfer.Error.Message )
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
LogWrite $_.Exception.Message
exit 1
}