0

For monitoring backups written to a remote FTPS server, I was able to request a list of files from the server in Powershell. However, I am not sure how to work with the output so that I can get the timestamp from the newest file.

Maybe there is a better approach to this but this is my code so far, output below.

# Create an FTPWebRequest object to handle the connection to the FTP server
$sourceuri = "ftp://192.168.1.4/ROOT/CobianBackups"
$ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri)

# Set the request's network credentials for an authenticated connection
$username = "admin" #Change username when done
$password = "xxx"
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true } #ignore unsigned SSL cert, UNSECURE! Get signed SSL
# Set FTPWebRequest method to ListDirectory
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
$ftprequest.EnableSsl = $True
$ftprequest.UseBinary = $False
$ftprequest.UsePassive = $True
$ftprequest.KeepAlive = $False

$ftpresponse = $ftprequest.GetResponse()

#Write-Host $ftpresponse.StatusCode
#Write-Host $ftpresponse.StatusDescription


$reader = New-Object IO.StreamReader $ftpresponse.GetResponseStream() 
    $reader.ReadToEnd()
    $reader.Close()

echo $reader

Output:

-rw-rw-rw-   1 admin    users       137233604 Sep 30 13:41  OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-30 13;36;52 (Full).zip
-rw-rw-rw-   1 admin    users       138556255 Sep 30 19:48  OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-30 19;36;18 (Full).zip
-rw-rw-rw-   1 admin    users       139217601 Oct  1 00:12  OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-10-01 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       698303741 Sep  3 00:05 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-03 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       699470009 Sep  4 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-04 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       700639919 Sep  5 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-05 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       698361150 Sep  6 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-06 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       699553508 Sep  7 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-07 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       700758345 Sep  8 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-08 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       698512347 Sep  9 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-09 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       699752013 Sep 10 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-10 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       701003563 Sep 11 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-11 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       698883380 Sep 12 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-12 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       700251730 Sep 13 00:06 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-13 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       701612056 Sep 14 00:08 HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-14 00;00;04 (Full).zip
-rwxrwxrwx   1 admin    users              79 Sep 14 00:15 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-14 00;14;59 (Full).zip
-rwxrwxrwx   1 admin    users       137231884 Sep 14 07:38 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-14 07;35;22 (Full).zip
-rwxrwxrwx   1 admin    users       138496696 Sep 14 14:59 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-14 14;55;24 (Full).zip
-rwxrwxrwx   1 admin    users       136790964 Sep 15 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-15 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       138052042 Sep 16 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-16 00;00;04 (Full).zip
-rwxrwxrwx   1 admin    users       139355293 Sep 17 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-17 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       137115162 Sep 18 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-18 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       138536791 Sep 19 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-19 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       139959405 Sep 20 00:04 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-20 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       137663847 Sep 21 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-21 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       138945285 Sep 22 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-22 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       140230350 Sep 23 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-23 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       137699171 Sep 24 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-24 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       138988930 Sep 25 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-25 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       140287732 Sep 26 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-26 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       137764807 Sep 27 00:04 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-27 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       139063256 Sep 28 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-28 00;00;03 (Full).zip
-rwxrwxrwx   1 admin    users       140365349 Sep 29 00:03 OpenVPN + HTDOCS + Apache conf+  exported databases backup to NAS01 2017-09-29 00;00;03 (Full).zip


CurrentEncoding BaseStream EndOfStream
--------------- ---------- -----------

One ore more sequential spaces could be used as the delimeter which would result in the date at the 6th (month), 7th (day) and 8th (time) column but I am not sure how to do this and I don't think it would be the best solution as it will break as soon as the output changes.

Any ideas? I really just need the datetimestamp of the newest file so I can calculate time since last backup. Getting the output in an array would be even greater as I can use the script for other purposes in the future.

Hope you can help me.

-Jason

3 Answers3

0

Try String.Split method

$reader.Split([char[]]"`n") |
ForEach-Object {, $_.Split([char[]]$null, [StringSplitOptions]::RemoveEmptyEntries)} |
ForEach-Object {"$($_[5..7])"}

More control can be achieved thru regular expression

$reader |
Select-String '\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|Sep)\s(\d\d)\s(\d\d:\d\d)\s' -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {, ($_.Groups[1..3] | Select-Object -ExpandProperty Value)} |
ForEach-Object {$_[0] + ' ' + $_[1] + ' ' + $_[2]}
Andrei Odegov
  • 2,925
  • 2
  • 15
  • 21
0

Edit: Andrei Odegov Updated their solution with a new regex, so I wanted to offer a variation on the solution based on that instead of my previous answer.

Once you have $dirlist it seems you are taking extra steps you may not need to in order to achieve your means. Try the following instead:

# Create an FTPWebRequest object to handle the connection to the FTP server
clear
$sourceuri = "ftp://192.168.1.4/ROOT/CobianBackups"
$ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri)

# Set the request's network credentials for an authenticated connection
$username = "admin" #Change username when done
$password = "xxx"
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true } #ignore unsigned SSL cert, UNSECURE! Get signed SSL
# Set FTPWebRequest method to ListDirectory
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
$ftprequest.EnableSsl = $True
$ftprequest.UseBinary = $False
$ftprequest.UsePassive = $True
$ftprequest.KeepAlive = $False

Remove-Variable ftpresponse
$ftpresponse = $ftprequest.GetResponse()
#Write-Host $ftpresponse.StatusCode
#Write-Host $ftpresponse.StatusDescription

Remove-Variable reader
$reader = New-Object IO.StreamReader $ftpresponse.GetResponseStream() 
    $dirlist = $reader.ReadToEnd()
    $reader.Close()

echo "raw output:"
$dirlist

$dateArray = $dirlist | Select-String -Pattern "((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|Sep)\s(s|\d)\d\s\d{2}:\d{2})" | % {$_.Matches} |  % {[datetime]::parseexact($_.Value, 'MMM dd HH:mm', $null)} | Sort-Object

echo "DateTime Sorted:"
$dateArray

echo "Last file:"
$dateArray[-1]

The results from Select-String can be converted to [DateTime] in the pipe, and also sorted in that same pipe using Sort-Object, which by default sorts ascending.

Once you get to the Last file, $array[-1] will return the last item in the array, there's no need to get the length in order to achieve this.

David Wall
  • 42
  • 8
0

After fiddling with the Select I gave up and tried another approach. (I kept getting errors about the regex not being correct..)

Using the snippet from Andrei Odegov I splitted $dirlist and selected the date and time from the filename instead of the actual modify date. For this I deleted some older backups so that all files had the date and time on the 19th and 20th object. This date includes the year which might prevent problems when there are files from more than one year.

Then I process these dates into $dateArray, sort them and finally got a good result!

Below is the final code, since the purpose of this script was to get the time since last backup (in minutes) to my PRTG network monitor, I included that code too.

# Create an FTPWebRequest object to handle the connection to the FTP server
clear
$sourceuri = "ftp://192.168.1.4/ROOT/CobianBackups" #Full path to folder
$ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri)

# Set the request's network credentials for an authenticated connection
$username = "username"
$password = "password"
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true } #ignore unsigned SSL cert, UNSECURE! Get signed SSL
# Set FTPWebRequest method to ListDirectory
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
$ftprequest.EnableSsl = $True
$ftprequest.UseBinary = $False
$ftprequest.UsePassive = $True
$ftprequest.KeepAlive = $False

Remove-Variable ftpresponse
$ftpresponse = $ftprequest.GetResponse()
#Write-Host $ftpresponse.StatusCode
#Write-Host $ftpresponse.StatusDescription

Remove-Variable reader
$reader = New-Object IO.StreamReader $ftpresponse.GetResponseStream() 
    $dirlist = $reader.ReadToEnd()
    $reader.Close()

Remove-Variable rawdates
$rawdates = $dirlist.Split([char[]]"`n") |
ForEach-Object {, $_.Split([char[]]$null, [StringSplitOptions]::RemoveEmptyEntries)} |
ForEach-Object {"$($_[19..20])"}


Remove-Variable dateArray
foreach ($element in $rawdates){
$date = [datetime]::parseexact($element, 'yyyy-MM-dd HH;mm;ss', $null)
[DateTime[]]$dateArray += $date
}

[Array]::Sort($dateArray)

$dateArray[-1]
$DateNow = Get-Date
$DateDif = $DateNow - $dateArray[-1]
$DateDif.TotalMinutes

#Final output to PRTG (write-host VALUE:MESSAGE)
write-host $DateDif.TotalMinutes: Last backup executed at $dateArray[-1] which is ($DateDif.Days) days, ($DateDif.Hours) hours, ($DateDif.Minutes) minutes ago

Thanks all for contributing!

-Jason