OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Variables in Try-Catch blocks with Powershell?

  • Thread starter Thread starter Evan
  • Start date Start date
E

Evan

Guest
I have been trying to write a script in powershell that renames a file if it does not get transferred correctly through WinSCP. The script looks something like this:

Code:
# Variables
$error = 0
$currentFile = "test"

# Functions
function upload-files {
    param(
        $WinScpSession,
        $LocalDirectory,
        $FileType,
        $RemoteDirectory
    )
    
    get-childitem $LocalDirectory -filter $FileType |
        foreach-object {
            # $_ gets the current item of the foreach loop. 
            write-output "Sending $_..."
            $currentFile = "$($LocalDirectory)$($_)"
            upload-file -WinScpSession $session -LocalDirectory "$($LocalDirectory)$($_)" -RemoteDirectory "$RemoteDirectory"
        }
}

try
{
    # Upload files
    upload-files -WinScpSession $Session -LocalDirectory [PathToLocalDirectory] -FileType [FileType] -RemoteDirectory [PathToRemoteDirectoy]
}    
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    write-output $currentFile
    $errorMoveLocation = copy-item -path "$currentFile" -destination "$currentFile.err" -passthru
    Write-Host "Error File has been saved as $errorMoveLocation"
    $error = 1
}

I have removed the paths for ease of reading and some WinSCP lines that don't have anything to do with the issue.

When adding some script-breaking code after the upload-file function, it would go to the catch statement where I expected the $currentFile variable to be the $($LocalDirectory)$($_) since it was caught after setting the variable again. However, the actual value of the variable is the original "test" value that it was initiated with. I have tried changing the scope of $currentFile to both script, and global the same issue still happens. I am still relatively new at powershell and this is beyond my expertise. Any help would be greatly appreciated, thanks!
<p>I have been trying to write a script in powershell that renames a file if it does not get transferred correctly through WinSCP. The script looks something like this:</p>
<pre><code># Variables
$error = 0
$currentFile = "test"

# Functions
function upload-files {
param(
$WinScpSession,
$LocalDirectory,
$FileType,
$RemoteDirectory
)

get-childitem $LocalDirectory -filter $FileType |
foreach-object {
# $_ gets the current item of the foreach loop.
write-output "Sending $_..."
$currentFile = "$($LocalDirectory)$($_)"
upload-file -WinScpSession $session -LocalDirectory "$($LocalDirectory)$($_)" -RemoteDirectory "$RemoteDirectory"
}
}

try
{
# Upload files
upload-files -WinScpSession $Session -LocalDirectory [PathToLocalDirectory] -FileType [FileType] -RemoteDirectory [PathToRemoteDirectoy]
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
write-output $currentFile
$errorMoveLocation = copy-item -path "$currentFile" -destination "$currentFile.err" -passthru
Write-Host "Error File has been saved as $errorMoveLocation"
$error = 1
}
</code></pre>
<p>I have removed the paths for ease of reading and some WinSCP lines that don't have anything to do with the issue.</p>
<p>When adding some script-breaking code <strong>after</strong> the <strong>upload-file</strong> function, it would go to the catch statement where I expected the $currentFile variable to be the $($LocalDirectory)$($_) since it was caught after setting the variable again. However, the actual value of the variable is the original "test" value that it was initiated with. I have tried changing the scope of $currentFile to both script, and global the same issue still happens. I am still relatively new at powershell and this is beyond my expertise. Any help would be greatly appreciated, thanks!</p>
Continue reading...
 

Latest posts

Top