0
votes

I have a script that uploads files to FTP servers, I have a list of servers IP $FTPServer and directory from files are uploaded \Upload. It uses the PSFTP module.

Here is my code of reading file $item and uploading to IP $line:

foreach ($line in $FTPServer)
{
    foreach ($item in (Get-ChildItem .\Upload))
    {
        Start-Transcript -Path $results
        Write-Host -Object "ftp url: $line" 
        Set-FTPConnection -Credentials $FTPCredential -Server $line -Session MySession -UsePassive 
        $Session = Get-FTPConnection -Session MySession 
        Write-Host -Object "Uploading $item..."
        Add-FTPItem -Session $Session -Path $FTPPlace -LocalPath .\Upload\$item -Confirm:$false
        Stop-Transcript
    }
}

The problem is, sometimes not all files can be upload or someone server is down. Therefore there is need to save results of uploading. I think about something like table which would be stored in logs.txt and looks like that:

FTP Server   File       Connected   Uploaded
----------   ---------  --------    --------
192.168.1.2  test.txt   OK          OK #when connected and uploaded file
192.168.1.2  conf.dll   OK          FAIL #when connected but did not upload
192.168.1.20 test.txt   FAIL        FAIL #when did not connect and no uploaded

I was thinking about hash table:

$array = $line, $item, $array[2], $array[3]
$FailTable=New-Object -TypeName PSObject -Property ([ordered]@{"FTP Server"=$array[0]; "File"=$array[1];"Connected"=$array[2];"Uploaded"=$array[3]})
$FailTable|Out-File '.\stats.txt'

But I don't know how to put there status of connections and uploads to $array[2] and $array[3] I will glad for any help.

2

2 Answers

0
votes

You could build the array as a combination of server and file name, and then store the results pass, fail, etc.

So, your has table for:

192.168.1.2  test.txt   OK          OK #when connected and uploaded file
192.168.1.2  conf.dll   OK          FAIL #when connected but did not upload
192.168.1.20 test.txt   FAIL        FAIL #when did not connect and no uploaded

would look like:

"192.168.1.2\test.txt","OK"
"192.168.1.2\conf.dll","OK"
"192.168.1.20\test.txt","FAIL"

If you used a non-allowable character (say * between the IP and the name, you could then do a split("*") to break them back apart.

Depending on the module your using for FTP, you might be able to say:

$results = Add-FTPItem -Session $Session -Path $FTPPlace -LocalPath .\Upload\$item -Confirm:$false

and then look at the value of $results to see if there were issues(maybe?)

-1
votes

I add answer to my own question yesterday:

I have made a ftp script file, with ftp.ini file.
The file was in c:\hotbox\ftpbat.bat

I was in wrong directory. The program worked fine, but I run the bat-program in different index of drive. I was testing it in my VB6-index

Sorry.