Access is denied error on every script second run on his own folder
Hello, I have strange problem with WinSCP asssembly and powershell script. In script, I want to create tmp folder, upload files, rename old target directory, rename tmp to target and remove old. In first run, it works, in second run, library reports error on folder, which created few moments ago. And on third run, it removes this folder, 4th run access denied, and so on.. Look into attachment, there is console and logs for good and bad run I can't find error, can someone help me?
Here is code:
Access is denied
Here is code:
$FTPHost = 'someserver' $FTPUser = 'someuser' $FTPPass = '*******' $srcFolder = "D:\testUploadData" $destPath = 'testFolder' $destTmpPath = "${destPath}.TMP" $destOldPath = "${destPath}.OLD" try { # Load WinSCP .NET assembly Add-Type -Path (Join-Path $PSScriptRoot "..\winscp_assembly\WinSCPnet.dll") $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Ftp HostName = $FTPHost UserName = $FTPUser Password = $FTPPass } $session = New-Object WinSCP.Session $session.SessionLogPath = 'log.txt' try { Write-Host("Connecting to FTP") $session.Open($sessionOptions) Write-Host("Creating .TMP dir") if($session.FileExists($destTmpPath)) { $session.RemoveFiles($destTmpPath).Check() } $session.CreateDirectory($destTmpPath) Write-Host("Uploading to .TMP dir") $session.PutFiles($srcFolder, $destTmpPath).Check() Write-Host("Renaming live dir to .OLD") if(!$session.FileExists($destPath)) { $session.CreateDirectory($destPath) } if($session.FileExists($destOldPath)) { $session.RemoveFiles($destOldPath).Check() } $session.MoveFile($destPath, $destOldPath) Write-Host("Renaming .TMP dir to live") $session.MoveFile($destTmpPath, $destPath) Write-Host("Removing .OLD dir") $session.RemoveFiles($destOldPath).Check() } finally { $session.Dispose() } exit 0 } catch { Write-Host "Error: $($_.Exception.Message)" exit 1 }