I have a project here that I am trying to automate. I have some load tests i need to run on a machine and during that time I need to have a constant in and out of FTP traffic. I have been trying to write a Powershell script to do it but have been coming up short. I can get the file to download every time but the upload always fails. I also need to make sure my looping mechanism is correct.
I need this to download and upload a single file until I tell it to stop. Here is the script.
$FTPServer = '172.20.2.124'
$FTPUsername = 'ftp'
$FTPPassword = 'ftpTransfers'
$FTPSecurePassword = ConvertTo-SecureString -String $FTPPassword -asPlainText -Force
$FTPCredential = New-Object System.Management.Automation.PSCredential($FTPUsername,$FTPSecurePassword)
$Path = '/'
$LocalPath = 'C:\Downloads'
#Open session
Set-FTPConnection -Credentials $FTPCredential -Server $FTPServer -Session MySession -UseBinary -KeepAlive
$Session = Get-FTPConnection -Session MySession
#download file
Get-FTPItem -Path /File1.mxf -LocalPath $LocalPath -Overwrite -Session $Session | out-null
#Upload the file
Add-FTPItem -LocalPath $LocalPath/File1.mxf -Path $Path -Overwrite -Session $Session | out-null
do {
response = Read-Host "repeat?"
}
while ($response -eq "Y")
This is the error I get whenever I try to do an upload with the script. I have tried to figure out how to close the stream but I have not had any luck.
Add-FTPItem : Exception calling "GetRequestStream" with "0" argument(s): "Object reference not set to an instance of an object." At C:\Powershell Scripts\FTPUploadandDownload.ps1:22 char:1 + Add-FTPItem -LocalPath $LocalPath/File1.mxf -Path $Path -Overwrite -Session ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Add-FTPItem
Any help would be appreciated, I am fairly new to this level of powershell.