When I run the below script, nothing happens for uploading arriving files to FTP
I just get this in poweschell
PS C:\Users\Administrator\Desktop\SCRIPT> .\NEWR4.PS1 Loaded WinSCP .NET assembly Enter your FTP username: **** Enter your FTP password: ******** Enter the UNC path of the source directory: \HOPE5\MEDIA Session options set up Session and debug log paths set Connected to FTP server File watcher set up
Id Name PSJobTypeName State HasMoreData Location Command
1 f1cabfda-26c... NotStarted False ... File watcher event registered
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
Write-Host "Loaded WinSCP .NET assembly"
# Prompt for username and password
$UserName = Read-Host -Prompt 'Enter your FTP username'
$Password = Read-Host -Prompt 'Enter your FTP password' -AsSecureString
# Prompt for source directory
$SourceDirectory = Read-Host -Prompt 'Enter the UNC path of the source directory'
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.host.com"
UserName = $UserName
Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
}
Write-Host "Session options set up"
$session = New-Object WinSCP.Session
# Set session and debug log paths
$session.SessionLogPath = "C:\temp\WinSCP.log"
$session.DebugLogPath = "C:\temp\WinSCP-debug.log"
Write-Host "Session and debug log paths set"
try {
# Connect
$session.Open($sessionOptions)
Write-Host "Connected to FTP server"
# Watcher
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $SourceDirectory
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
Write-Host "File watcher set up"
$action = {
$path = $Event.SourceEventArgs.FullPath
$extension = [System.IO.Path]::GetExtension($path)
if ($extension -eq ".mxf" -or $extension -eq ".mp4" -or $extension -eq ".xml" -or $extension -eq ".oseq") {
if ($path -notlike "*.$TMP") {
try {
# Check if file is still growing
$size = (Get-Item $path).Length
Start-Sleep -Seconds 60
$newSize = (Get-Item $path).Length
if ($size -eq $newSize) {
# Check if file is newer than 1 day
$creationTime = (Get-Item $path).CreationTime
$timeDifference = New-TimeSpan -Start $creationTime -End (Get-Date)
if ($timeDifference.TotalDays -le 1) {
# Check if remote directory exists
$remotePath = "/$(Split-Path $path -Leaf)"
if (-not $session.FileExists($remotePath)) {
# Create remote directory
$session.CreateDirectory($remotePath)
Write-Host ("Created remote directory {0}" -f $remotePath)
}
# Upload files
Write-Host ("Attempting to upload file {0}" -f $path)
$transferResult = $session.PutFiles($path, $remotePath)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers) {
Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
Add-Content -Path "C:\temp\WinSCP-success.log" -Value ("Upload of {0} succeeded" -f $transfer.FileName)
}
} else {
Write-Host ("File {0} is older than 1 day, skipping this round" -f $path)
}
} else {
Write-Host ("File {0} is still growing, skipping this round" -f $path)
}
} catch {
Write-Host ("Error: {0}" -f $_.Exception.Message)
Add-Content -Path "C:\temp\WinSCP-error.log" -Value ("Error: {0}" -f $_.Exception.Message)
if ($_.Exception.InnerException -ne $null) {
Write-Host ("Inner Exception: {0}" -f $_.Exception.InnerException.Message)
Add-Content -Path "C:\temp\WinSCP-error.log" -Value ("Inner Exception: {0}" -f $_.Exception.InnerException.Message)
}
}
}
}
}
Register-ObjectEvent $watcher "Created" -Action $action
Write-Host "File watcher event registered"
while ($true) { sleep 5 }
} finally {
# Disconnect, clean up
$session.Dispose()
Write-Host "Disconnected from FTP server"
}