检查 WinSCP 是否在两个日期之间的每一天下载了文件。

huangapple go评论105阅读模式
英文:

Check if WinSCP downloaded files for every day between two dates

问题

我有这个脚本来收集两个日期之间的日志文件。通常我在远程机器上每天都有创建日志,但是否有可能显示存在或不存在的文件呢?

例如,如果我尝试在两个日期25.06 - 30.06之间下载文件,而在远程机器上没有来自28.06的日志,就在控制台中打印出来。

25.06,26.06,27.06,29.06,30.06 日志下载成功
[!] 警告 28.06 远程机器上的日志不存在!

  1. # 设置会话选项
  2. $options = @{
  3. Protocol = [WinSCP.Protocol]::Sftp
  4. HostName = $entry.IP
  5. UserName = $User
  6. Password = $Password
  7. GiveUpSecurityAndAcceptAnySshHostKey = $true
  8. }
  9. try {
  10. # 使用第一个密码设置会话选项
  11. $sessionOptions = New-Object WinSCP.SessionOptions -Property $options
  12. $session = New-Object WinSCP.Session
  13. # 尝试连接
  14. $session.Open($sessionOptions)
  15. }
  16. catch {
  17. # 使用第二个设置设置会话选项
  18. $options['HostName'] = $vpnIP
  19. $options['UserName'] = $User
  20. $options['Password'] = $Password
  21. try {
  22. $sessionOptions = New-Object WinSCP.SessionOptions -Property $options
  23. $session = New-Object WinSCP.Session
  24. # 尝试连接
  25. $session.Open($sessionOptions)
  26. }
  27. catch {
  28. Write-Error "无法打开 WinSCP 会话: $($_.Exception.Message)"
  29. throw
  30. }
  31. }
  32. # 日期1 起始
  33. do {
  34. $date = $null
  35. $today = Read-Host -Prompt ('输入起始日期(包括在内)(例如 {0})[yyyy.MM.dd]' -f (Get-Date -Format "yyyy.MM.dd"))
  36. try {
  37. $date = Get-Date -Date $today -Format "yyyy-MM-dd" -ErrorAction Stop
  38. '[OK] {0} 有效日期 - OK!' -f $date
  39. }
  40. catch {
  41. '[X] {0} 无效日期!' -f $today
  42. }
  43. }
  44. until ($date)
  45. # 日期2 停止
  46. do {
  47. $date1 = $null
  48. Write-Host "添加 +1 天" -ForegroundColor Red
  49. $today1 = Read-Host -Prompt ('输入结束日期(不包括在内)(例如 {0})[yyyy.MM.dd]' -f (Get-Date -Format "yyyy.MM.dd"))
  50. try {
  51. $date1 = Get-Date -Date $today1 -Format "yyyy-MM-dd" -ErrorAction Stop
  52. '[OK] {0} 有效日期 - OK!' -f $date1
  53. }
  54. catch {
  55. '[X] {0} 无效日期!' -f $today1
  56. }
  57. }
  58. until ($date1)
  59. # ----- 日期结束
  60. $session = New-Object WinSCP.Session
  61. $file = "*.log"
  62. $localPath = "\temp_files"
  63. $remotePath = "/C:/log", "/C:/Back_up"
  64. try {
  65. # 连接
  66. $session.Open($sessionOptions)
  67. # 检查文件夹是否存在
  68. foreach ($remotePath in $remotePath)
  69. {
  70. if ($session.FileExists($remotePath))
  71. {
  72. Write-Host "[OK] 文件夹 '$remotePath' 存在" -ForegroundColor Green
  73. # 传输文件
  74. Write-Host "[i] '$date' - '$date1' > '$inputID' 正在下载..." -ForegroundColor Cyan
  75. $session.GetFilesToDirectory($remotePath, $localPath, "*.log>=$date<=$date1").Check();
  76. }
  77. else
  78. {
  79. Write-Host "[X] INFO: 文件夹: '$remotePath' 不存在" -ForegroundColor Red
  80. }
  81. }
  82. }
  83. finally {
  84. $session.Dispose()
  85. }

谢谢。

英文:

I have this script to collect logs file between two dates. Normally I have logs created for everyday on remote machine but is there any possibility to show what file exist or not?

For example If I try to download file between two dates 25.06 - 30.06 and on remote machine doesn't exist log from 28.06 print in console.

> 25.06,26.06,27.06,29.06,30.06 log downloaded successfully
> [!] warning 28.06 log doesn't exist on remote machine!

  1. # Set up session options
  2. $options = @{
  3. Protocol = [WinSCP.Protocol]::Sftp
  4. HostName = $entry.IP
  5. UserName = $User
  6. Password = $Password
  7. GiveUpSecurityAndAcceptAnySshHostKey = $true
  8. }
  9. try {
  10. # Set up session options using first password
  11. $sessionOptions = New-Object WinSCP.SessionOptions -Property $options
  12. $session = New-Object WinSCP.Session
  13. # Try Connect
  14. $session.Open($sessionOptions)
  15. }
  16. catch {
  17. # Set up session options using second settings
  18. $options[&#39;HostName&#39;] = $vpnIP
  19. $options[&#39;UserName&#39;] = $User
  20. $options[&#39;Password&#39;] = $Password
  21. try {
  22. $sessionOptions = New-Object WinSCP.SessionOptions -Property $options
  23. $session = New-Object WinSCP.Session
  24. # Try Connect
  25. $session.Open($sessionOptions)
  26. }
  27. catch {
  28. Write-Error &quot;Could not open WinSCP session: $($_.Exception.Message)&quot;
  29. throw
  30. }
  31. }
  32. # Date 1 START
  33. do {
  34. $date = $null
  35. $today = Read-Host -Prompt (&#39;Enter START date (inclusive) (e.g. {0}) [yyyy.MM.dd]&#39; -f (Get-Date -Format &quot;yyyy.MM.dd&quot;))
  36. try {
  37. $date = Get-Date -Date $today -Format &quot;yyyy-MM-dd&quot; -ErrorAction Stop
  38. &#39;[OK] {0} Valid date - OK!&#39; -f $date
  39. }
  40. catch {
  41. &#39;[X] {0} Invalid date!&#39; -f $today
  42. }
  43. }
  44. until ($date)
  45. # Date 2 STOP
  46. do {
  47. $date1 = $null
  48. Write-Host &quot;Add +1 day&quot; -ForegroundColor Red
  49. $today1 = Read-Host -Prompt (&#39;Enter END date (exclusive) (e.g. {0}) [yyyy.MM.dd]&#39; -f (Get-Date -Format &quot;yyyy.MM.dd&quot;))
  50. try {
  51. $date1 = Get-Date -Date $today1 -Format &quot;yyyy-MM-dd&quot; -ErrorAction Stop
  52. &#39;[OK] {0} Valid date - OK!&#39; -f $date1
  53. }
  54. catch {
  55. &#39;[X] {0} Invalid date!&#39; -f $today1
  56. }
  57. }
  58. until ($date1)
  59. # ----- Date END
  60. $session = New-Object WinSCP.Session
  61. $file = &quot;*.log&quot;
  62. $localPath = &quot;\temp_files&quot;
  63. $remotePath = &quot;/C:/log&quot;, &quot;/C:/Back_up&quot;
  64. try {
  65. # Connect
  66. $session.Open($sessionOptions)
  67. # Check exists folder
  68. foreach ($remotePath in $remotePath)
  69. {
  70. if ($session.FileExists($remotePath))
  71. {
  72. Write-Host &quot;[OK] Folder &#39;$remotePath&#39; exist&quot; -ForegroundColor Green
  73. # Transfer file
  74. Write-Host &quot;[i] &#39;$date&#39; - &#39;$date1&#39; &gt; &#39;$inputID&#39; downloading...&quot; -ForegroundColor Cyan
  75. $session.GetFilesToDirectory($remotePath, $localPath, &quot;*.log&gt;=$date&lt;=$date1&quot;).Check();
  76. }
  77. else
  78. {
  79. Write-Host &quot;[X] INFO: Folder: &#39;$remotePath&#39; doesn&#39;t exist&quot; -ForegroundColor Red
  80. }
  81. }
  82. }
  83. finally {
  84. $session.Dispose()
  85. }

Thank you.

答案1

得分: 2

你可以分析Session.GetFilesToDirectory返回的传输文件列表,以找出在范围内的所有日期是否有下载(你已经知道了)。但在我看来,更简单的方法是逐日进行,分别下载每天的日志并简单地检查是否下载了任何日志。

  1. $d = $date
  2. while ($d -le $date1)
  3. {
  4. $f = "yyyy-MM-dd"
  5. $start = $d.ToString($f)
  6. $end = $d.AddDays(1).ToString($f)
  7. $mask = "*.log>=$start<$end"
  8. Write-Host "Downloading logs for $start..."
  9. $transferResult = $session.GetFilesToDirectory($remotePath, $localPath, $mask)
  10. $transferResult.Check()
  11. $c = $transferResult.Transfers.Count
  12. if ($c -gt 0)
  13. {
  14. Write-Host "$c logs for $start were downloaded"
  15. }
  16. else
  17. {
  18. Write-Host "NO logs for $start were downloaded"
  19. }
  20. $d = $d.AddDays(1)
  21. }

这段代码假设$date$date1是实际的DateTime对象,而不是字符串。因此,你需要从Get-Date调用中移除-Format "yyyy-MM-dd"

英文:

You can analyze the list of transferred files returned by Session.GetFilesToDirectory to find out if there any download for all days in the range (you know that already).

But imo, way easier would be to go day-by-day, downloading respective day logs separately and simply checking, if any log was downloaded at all.

  1. $d = $date
  2. while ($d -le $date1)
  3. {
  4. $f = &quot;yyyy-MM-dd&quot;
  5. $start = $d.ToString($f)
  6. $end = $d.AddDays(1).ToString($f)
  7. $mask = &quot;*.log&gt;=$start&lt;$end&quot;
  8. Write-Host &quot;Downloading logs for $start...&quot;
  9. $transferResult = $session.GetFilesToDirectory($remotePath, $localPath, $mask)
  10. $transferResult.Check()
  11. $c = $transferResult.Transfers.Count
  12. if ($c -gt 0)
  13. {
  14. Write-Host &quot;$c logs for $start were downloaded&quot;
  15. }
  16. else
  17. {
  18. Write-Host &quot;NO logs for $start were downloaded&quot;
  19. }
  20. $d = $d.AddDays(1)
  21. }

The code assumes that $date and $date1 are actual DateTime objects, not strings. So you need to remove the -Format &quot;yyyy-MM-dd&quot; from the Get-Date calls.

huangapple
  • 本文由 发表于 2023年7月23日 16:02:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定