[英]Powershell format FTPS list output to get newest file stamp
為了監視寫入遠程FTPS服務器的備份,我能夠從Powershell中的服務器請求文件列表。 但是,我不確定如何使用輸出,以便從最新文件中獲取時間戳。
也許有一種更好的方法,但這是到目前為止的代碼,輸出如下。
# 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
輸出:
-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
--------------- ---------- -----------
一個或多個連續的空格可以用作分隔符,這將導致第6(月),7(天)和8(時間)列的日期,但是我不確定如何執行此操作,我也不認為將是最好的解決方案,因為它會在輸出更改時立即中斷。
有任何想法嗎? 我真的只需要最新文件的datetimestamp,以便可以計算自上次備份以來的時間。 由於將來我可以將腳本用於其他目的,因此將輸出輸出到數組中甚至會更大。
希望您能夠幫助我。
-傑森
$reader.Split([char[]]"`n") |
ForEach-Object {, $_.Split([char[]]$null, [StringSplitOptions]::RemoveEmptyEntries)} |
ForEach-Object {"$($_[5..7])"}
通過正則表達式可以實現更多控制
$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用新的正則表達式更新了他們的解決方案,因此我想基於該解決方案而不是我以前的回答提供一個變體。
一旦有了$dirlist
,看來您正在采取多余的步驟來實現自己的目的。 請嘗試以下操作:
# 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]
Select-String的結果可以在管道中轉換為[DateTime]
,也可以使用Sort-Object
在同一管道中進行Sort-Object
,默認情況下,該Sort-Object
以升序排序。
到達最后一個文件后, $array[-1]
將返回$array[-1]
的最后一個項目,無需獲取長度即可實現此目的。
在選擇之后,我放棄了嘗試另一種方法。 (我不斷收到有關正則表達式不正確的錯誤。)
使用Andrei Odegov的代碼片段,我拆分了$ dirlist並從文件名中選擇了日期和時間,而不是實際的修改日期。 為此,我刪除了一些較舊的備份,以便所有文件的日期和時間都位於第19和20個對象上。 該日期包括一年以上的文件可能防止出現問題的年份。
然后,我將這些日期處理到$ dateArray中,對其進行排序,最后得到一個不錯的結果!
下面是最終代碼,因為此腳本的目的是獲得自上次備份以來到PRTG網絡監視器的時間(以分鍾為單位),所以我也包含了該代碼。
# 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
謝謝大家的貢獻!
-傑森
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.