Script translates local path to remote differently. Depending on how the script is initiated.
I'm running into an issue adapting the script located below to my environment. I suspect the issue lies in the
Is this a bug? If not, what is the best way to handle this situation?
Thanks!
TranslateLocalPathToRemote
portion of the script. The RemoteFilePath
is translated differently depending on how the PowerShell script is ran.
- Running locally on my machine, Windows 11, it returns what I expect and works fine.
- Running it via PowerShell ISE on Server 2019, it returns what I expect and works fine.
- Running it via PowerShell, on Server 2019 returns a different path and fails.
Is this a bug? If not, what is the best way to handle this situation?
Thanks!
# Watches a directory for new files and # fires off the batch file to push to connection $processedfiles = "D:\EDI-Upload\Files\ProcessedFiles" $remotePath = "/XXX/Co-Packer/Orders/Source/" $localPath = "D:\EDI-Upload\Files\Source" $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = $localPath $watcher.Filter = "*.xlsx" $watcher.IncludeSubdirectories = $False $watcher.EnableRaisingEvents = $True ### LISTEN FOR CREATE Register-ObjectEvent $watcher Created -SourceIdentifier FileCreated -Action { try { $localFilePath = $event.SourceEventArgs.FullPath Write-Host "Local path: $localFilePath" $assemblyPath = "C:\Program Files (x86)\WinSCP" Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll") # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::ftp FtpSecure = [WinSCP.FtpSecure]::Implicit HostName = "" UserName = "" Password = "" TlsHostCertificateFingerprint = "" } $session = New-Object WinSCP.Session try { $remoteFilePath = [WinSCP.RemotePath]::TranslateLocalPathToRemote( $localFilePath, $localPath, $remotePath) Write-Host "Remote path: $remoteFilePath" # Connect $session.Open($sessionOptions) $session.PutFiles($localFilePath, $remoteFilePath).Check() Write-Host "Upload of $localFilePath succeeded" move-item "$localfilepath" "$ProcessedFiles" } finally { # Disconnect, clean up $session.Dispose() } } #end of first try catch { Write-Host "Error: $($_.Exception.Message)" } } #end of action while ($True) {sleep 5}