there are a lot of postings here about how to avoid automatic reconnect. Some of the postings say, one has to user the option "reconnect". But as you can see in documentation, in PowerShell it's only possible to set a TimeSpan and it is not possible to set the option to off, like in batch mode. I tried many ways to get it somehow to work, but it did not (even tried
AdditionalExecutableArguments, which always should be null). Here is my testcase using SFTP to a Ubuntu Server that I'm able to control:
The script first opens the connection and than waits for user input. I can see on the server auth.log the process id under which the connection is opened. Than, on server side, I kill all processes of the user (
sudo killall -u <username goes here>)
I then go back to the script and do the input, so it continues. I can see, that it is doing a reconnect.
$session.Open($sessionOptions) Read-Host "Script waiting. Press enter to go on" $transferResult = $session.PutFiles($localPath, $remotePath, $false, $transferOptions)