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.