|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Downloads all files from /var/log/modbus on an FTP data logger to a local folder. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + Uses curl.exe (built into Windows 11) rather than ftp.exe or Invoke-WebRequest, |
| 7 | + because curl handles FTP LIST/RETR more predictably and gives clear exit codes |
| 8 | + per file. Requires plain anonymous-free FTP auth (user:pass), as confirmed |
| 9 | + working in your ftp.exe test. |
| 10 | +
|
| 11 | +.PARAMETER FtpHost |
| 12 | + IP or hostname of the data logger. Defaults to 192.168.40.50 |
| 13 | +
|
| 14 | +.PARAMETER Username |
| 15 | + FTP username. Deafults to admin. |
| 16 | +
|
| 17 | +.PARAMETER Password |
| 18 | + FTP password. Defaults to admin. If omitted, you will be prompted (not echoed to screen). |
| 19 | +
|
| 20 | +.PARAMETER RemotePath |
| 21 | + Remote directory to mirror. Defaults to /var/log/modbus. |
| 22 | +
|
| 23 | +.PARAMETER LocalPath |
| 24 | + Local destination directory. Created if it does not exist. |
| 25 | +
|
| 26 | +.EXAMPLE |
| 27 | + .\Get-ModbusLogs.ps1 -Username svc_ftp -LocalPath C:\Logs\modbus |
| 28 | +
|
| 29 | +.NOTES |
| 30 | + - Explicitly calls curl.exe (not the PowerShell 5.1 alias for Invoke-WebRequest). |
| 31 | + - Skips directory entries when parsing the LIST output (lines starting with 'd'). |
| 32 | + - Existing local files with the same name are overwritten. |
| 33 | +#> |
| 34 | + |
| 35 | +[CmdletBinding()] |
| 36 | +param( |
| 37 | + [string]$FtpHost = "192.168.40.50", |
| 38 | + [string]$Username = "admin", |
| 39 | + [securestring]$Password = (ConvertTo-SecureString "admin" -AsPlainText -Force), |
| 40 | + [string]$RemotePath = "/var/log/modbus", |
| 41 | + [string]$LocalPath = ".\modbus_logs" |
| 42 | +) |
| 43 | + |
| 44 | +$ErrorActionPreference = "Stop" |
| 45 | + |
| 46 | +# Confirm curl.exe (the real one) is present, not just the IWR alias. |
| 47 | +$curl = Get-Command curl.exe -ErrorAction SilentlyContinue |
| 48 | +if (-not $curl) { |
| 49 | + throw "curl.exe not found. It ships with Windows 11 by default (System32\curl.exe) — check PATH." |
| 50 | +} |
| 51 | + |
| 52 | +if (-not $Password) { |
| 53 | + $Password = Read-Host -Prompt "FTP password for $Username" -AsSecureString |
| 54 | +} |
| 55 | +$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password) |
| 56 | +$plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr) |
| 57 | +[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) |
| 58 | + |
| 59 | +$userArg = "${Username}:${plainPassword}" |
| 60 | +$remoteDirUrl = "ftp://$FtpHost$RemotePath/" |
| 61 | + |
| 62 | +if (-not (Test-Path -LiteralPath $LocalPath)) { |
| 63 | + New-Item -ItemType Directory -Path $LocalPath -Force | Out-Null |
| 64 | +} |
| 65 | + |
| 66 | +Write-Host "Listing $remoteDirUrl ..." |
| 67 | +$listingRaw = & $curl.Source -s --user $userArg $remoteDirUrl |
| 68 | +if ($LASTEXITCODE -ne 0) { |
| 69 | + throw "curl LIST failed against $remoteDirUrl (exit code $LASTEXITCODE). Check path/credentials." |
| 70 | +} |
| 71 | + |
| 72 | +if (-not $listingRaw) { |
| 73 | + Write-Warning "Directory listing came back empty. Nothing to download." |
| 74 | + return |
| 75 | +} |
| 76 | + |
| 77 | +# Parse standard Unix-style LIST output. Skip directory entries (leading 'd') |
| 78 | +# and anything that doesn't look like a regular file line. |
| 79 | +$files = @() |
| 80 | +foreach ($line in $listingRaw -split "`r?`n") { |
| 81 | + if ([string]::IsNullOrWhiteSpace($line)) { continue } |
| 82 | + if ($line.StartsWith("d")) { continue } # subdirectory, not a file |
| 83 | + if ($line.StartsWith("l")) { continue } # symlink, skip unless you want it followed |
| 84 | + $name = ($line -split '\s+', 9)[-1] |
| 85 | + if ($name) { $files += $name } |
| 86 | +} |
| 87 | + |
| 88 | +if ($files.Count -eq 0) { |
| 89 | + Write-Warning "No regular files parsed out of the listing. Raw output below for inspection:" |
| 90 | + Write-Host $listingRaw |
| 91 | + return |
| 92 | +} |
| 93 | + |
| 94 | +Write-Host "Found $($files.Count) file(s). Downloading to $LocalPath ..." |
| 95 | + |
| 96 | +$failed = @() |
| 97 | +foreach ($name in $files) { |
| 98 | + $remoteUrl = "$remoteDirUrl$name" |
| 99 | + $localFile = Join-Path $LocalPath $name |
| 100 | + Write-Host " -> $name" |
| 101 | + & $curl.Source -s --user $userArg -o "$localFile" "$remoteUrl" |
| 102 | + if ($LASTEXITCODE -ne 0) { |
| 103 | + Write-Warning " FAILED (curl exit $LASTEXITCODE): $name" |
| 104 | + $failed += $name |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +if ($failed.Count -gt 0) { |
| 109 | + Write-Warning "$($failed.Count) of $($files.Count) file(s) failed: $($failed -join ', ')" |
| 110 | +} else { |
| 111 | + Write-Host "All $($files.Count) file(s) downloaded successfully to $LocalPath" |
| 112 | +} |
0 commit comments