<# .SYNOPSIS Usuniecie wszystkich .bak / .preurlencode.bak / .preanchor.bak z FTP serwera i lokalnie. Uzywa .NET FtpWebRequest (szybsze + lepsze error handling niz curl). #> [CmdletBinding()] param( [string]$LocalDir = "C:\visual studio code\projekty\cmsPRO\updates", [string]$RemoteRoot = "/public_html/updates", [string]$FtpHost = "host117523.hostido.net.pl", [string]$FtpUser = "www@cmspro.project-dc.pl", [string]$FtpPass = "aNDCvhA6cnHSQfM24vUE", [switch]$DryRun ) $ErrorActionPreference = "Continue" $baks = Get-ChildItem $LocalDir -Recurse -File | Where-Object { $_.Name -like '*.bak' } | Sort-Object FullName Write-Host "Plikow do skasowania: $($baks.Count)" if ($DryRun) { Write-Host "DRY-RUN" -ForegroundColor Yellow } $cred = New-Object System.Net.NetworkCredential($FtpUser, $FtpPass) $deleted = 0 $notFound = 0 $failed = 0 $failedList = @() $i = 0 function Delete-Ftp { param([string]$RemotePath) $url = "ftp://${FtpHost}${RemotePath}" $req = [System.Net.FtpWebRequest]::Create($url) $req.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile $req.Credentials = $cred $req.UsePassive = $true $req.UseBinary = $true $req.KeepAlive = $false $req.Timeout = 30000 try { $resp = $req.GetResponse() $resp.Close() return @{ ok = $true; status = 'deleted' } } catch [System.Net.WebException] { $msg = $_.Exception.Message if ($msg -match '550' -or $msg -match 'No such file') { return @{ ok = $true; status = 'not-found' } } return @{ ok = $false; status = 'error'; msg = $msg } } } foreach ($f in $baks) { $i++ $rel = $f.FullName.Substring($LocalDir.Length).TrimStart('\','/').Replace('\','/') $remoteFull = "$RemoteRoot/$rel" if (($i % 50) -eq 0) { Write-Host " $i / $($baks.Count) ... (deleted=$deleted notFound=$notFound failed=$failed)" } if ($DryRun) { Write-Host " [DRY] DELE $remoteFull" continue } $r = Delete-Ftp -RemotePath $remoteFull if ($r.ok) { if ($r.status -eq 'deleted') { $deleted++ } else { $notFound++ } Remove-Item -LiteralPath $f.FullName -Force } else { $failed++ $failedList += "$rel -- $($r.msg)" } } Write-Host "" Write-Host "Zakonczono." -ForegroundColor Green Write-Host " Usuniete (FTP+lokal): $deleted" Write-Host " Brak na serwerze (lokal usuniete): $notFound" Write-Host " Bledy: $failed" if ($failedList) { Write-Host "Pierwsze 10 bledow:" $failedList | Select-Object -First 10 | ForEach-Object { Write-Host " $_" } $logPath = "C:\visual studio code\projekty\cmsPRO\.paul\phases\04h-hotfix-https-updates\cleanup-errors.log" $failedList | Out-File -FilePath $logPath -Encoding utf8 Write-Host "Pelny log bledow: $logPath" }