Very true. Thank you for your help!
- Stumped23
Does it still return the correct path if you let the watcher process build the$localfilepath
variable instead of manually specifying?
$localfilepath
variable instead of manually specifying?
Edition: Windows Server 2019 Standard
Version: 1809
OS Build: 17763.4010
TlsHostCertificateFingerprint
line, as that won't work), I get:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\Administrator> cd C:\foo
PS C:\foo> powershell -file test.ps1
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 FileCreated NotStarted False ...
Local path: D:\EDI-Upload\Files\Source\test2.xlsx
Remote Path: /XXX/Co-Packer/Orders/Source/test2.xlsx
C:\Program Files (x86)\WinSCP
in my script.
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$localFilePath = "D:\EDI-Upload\Files\Source\test2.xlsx"
$remotePath = "/XXX/Co-Packer/Orders/Source/"
$localPath = "D:\EDI-Upload\Files\Source"
$remoteFilePath =
[WinSCP.RemotePath]::TranslateLocalPathToRemote(
$localFilePath, $localPath, $remotePath)
Write-Host $remoteFilePath
$remotepath
, $localpath
, AND $localfilepath
variables, still returns the incorrect remote file path. I've removed the upload aspect of the script to test and just requested it write-host $remotefilepath. I'm not sure how to simplify further without entirely removing the watcher aspect.
$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 = "D:\EDI-Upload\Files\Source\test2.xlsx"
# $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
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
} #end of first try
catch
{
Write-Host "Error: $($_.Exception.Message)"
}
} #end of action
while ($True) {sleep 5}
/XXX/Co-Packer/Orders/Source/test2.xlsx
$localfilepath
variable and manually specified instead.
$localFilePath = "D:\EDI-Upload\Files\Source\test2.xlsx"
# $localFilePath = $event.SourceEventArgs.FullPath
Write-Host "Local path: $localFilePath"
Local path: D:\EDI-Upload\Files\Source\test2.xlsx
Remote Path: D:/EDI-Upload/Files/Source/test2.xlsx
WinSCPnet.dll
in PowerShell ISE on Windows Server. I've getting "Could not load file or assembly ... Operation is not supported". I know the error, but I do not have the "Unblock" button on Windows Server.
Add-Type -Path ".\WinSCPnet.dll"
$localFilePath = "D:\EDI-Upload\Files\Source\test2.xlsx"
$remotePath = "/XXX/Co-Packer/Orders/Source/"
$localPath = "D:\EDI-Upload\Files\Source"
$remoteFilePath =
[WinSCP.RemotePath]::TranslateLocalPathToRemote(
$localFilePath, $localPath, $remotePath)
Write-Host $remoteFilePath
RemotePath.TranslateLocalPathToRemote
code could be returning different results on different systems. It's purely a string computation code. It does not use any system functions.
TranslateLocalPathToRemote
portion of the script. The RemoteFilePath
is translated differently depending on how the PowerShell script is ran.
# 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}