Running a script a 2nd time deletes the file

Advertisement

Guest

Running a script a 2nd time deletes the file

Hi all!

I'm trying to set up a very simple script to copy a file from a virtual machine to an Azure Web App. Due to some access issues, I've set the script to repeat the copy action 5 times, to ensure it gets copied. Problem is that when I run the script once, it copies the file. I run the script a 2nd time and it removes the file (on the destination).

This is the script:
# Load WinSCP .NET assembly
Add-Type -Path "C:\WS\WinSCPnet.dll"
$index = 0
$counter = 0
 
do
{
    "Index Value: $index"
    $counter++
    $index++
 
    # Set up session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Ftp
        HostName = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        UserName = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        Password = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        FtpSecure = [WinSCP.FtpSecure]::Implicit
    }
 
    $session = New-Object WinSCP.Session
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Transfer files
        $session.PutFiles("C:\Folder\Folder\File.txt", "/site/wwwroot/pki/*").Check()
    }
    finally
    {
        $session.Dispose()
    }
}
until ($index -eq 10)
Any help would be appreciated!

Reply with quote

Advertisement

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

Re: Running a script a 2nd time deletes the file

I cannot imagine how this script might delete the file. The problem must be different/elsewhere. What else did you to to debug the issue? Did try adding some debugging code to check at what point does the file disappear?

If you want us to check anyway, please attach session logs of both sessions.

Please attach a full session log file showing the problem (using the latest version of WinSCP).

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

snakewrathchild
Joined:
Posts:
3

I wrote the post, but I forgot to log in when writing it, apologies for that.

Here are the logs.
Session is the initial upload.
Session-delete is when the script was ran a 2nd time (the connection even failed, yet the file was still deleted).
Session-upload is the re-upload following the deletion.

P.S. Seems to be a problem when the connection fails, as that's when the file disappears. Could it be that it's like partially overwritten, thus when the connection fails, everything is scrapped?
Last edited by snakewrathchild on 2022-08-10 08:28; edited 1 time in total

Reply with quote

martin
Site Admin
martin avatar

Note that session.log contains three sessions. The second of those contains the same problem as session-delete.log – the upload is aborted due to some network problems. You seem to be aware of the problems, as you have attempted to make the script retry the transfer. But I do not think it does. On an error, the Session.PutFiles throws an exception and break your loop. You should catch the exception and trigger a retry. Also, shouldn't you break the loop on a successful upload?

Reply with quote

snakewrathchild

Thank you for the analysis, it does clear it up a bit. I do agree that the loop should stop at a successful upload, however I don't have enough scripting experience to make it work. Any suggestions?

Also – could you clarify about the exception that Session.PutFiles throws?

Reply with quote

Advertisement

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

Something like this? (not tested)
$index = 0
 
do
{
    "Index Value: $index"
    $index++
    $retry = $False
 
    try
    {
        # Set up session options
        $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
            Protocol = [WinSCP.Protocol]::Ftp
            HostName = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            UserName = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            Password = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            FtpSecure = [WinSCP.FtpSecure]::Implicit
        }
     
        $session = New-Object WinSCP.Session
        try
        {
            # Connect
            $session.Open($sessionOptions)
     
            # Transfer files
            $session.PutFiles("C:\Folder\Folder\File.txt", "/site/wwwroot/pki/*").Check()
        }
        finally
        {
            $session.Dispose()
        }
    }
    catch
    {
        Write-Host "Error: $($_.Exception.Message)"
        if ($index -ge 10)
        {
            Write-Host "Tried too many times, aborting"
        }
        else
        {
            Write-Host "Will retry"
            $retry = $True
        }
    }
}
until (-not $retry)

Reply with quote

snakewrathchild

It works! I have ran it a few times, the file gets copied, no issues with the file being deleted. I'll do further testing, but so far, so good. Thank you so much, this was the bane of my existence for some time :D

Reply with quote

Advertisement

You can post new topics in this forum