R
R
r1sh2015-11-10 13:15:57
PowerShell
r1sh, 2015-11-10 13:15:57

Need help with Powershell?

Good afternoon!
Please help me write the script.
We make backups of 2 databases every day, throw them into directories with the structure <year>\<month>\<day>
We need to clean up the backups for the previous month, leave the first from the beginning of the month and the next +15 days.
At the same time, you need to check that there are both backups in the directory (everything happens) I
wrote a Powershell script, but there is a problem:

$year = (Get-Date).Year
#$month = get-date ((get-date).AddMonths(-1)) -format MM
$month = "07"
$temp= "$env:temp" + "\backup\"
$dir="\\files\backup$\8.1\$year\$month"
#
# Создаём временный каталог
#
If (!(Test-Path $temp)){
New-Item $temp -type directory
}
#
# Сбрасываем счётчики
#
$trg=0
$ceh=0
Get-ChildItem -Path $dir| Sort-Object LastAccessTime | Select-Object -First 15 | ForEach-Object {
write-host $_
if ((Test-Path $dir\$_\trg*.dt) -And ($trg -eq 0)){
Copy-Item -recurse -Force $dir\$_\trg* $temp
$trg=1
}
if ((Test-Path $dir\$_\ceh*.dt) -And ($ceh -eq 0)){
#Write-Host $ceh
Copy-Item -recurse -Force $dir\$_\ceh* $temp
$ceh=1
}
if (($trg -eq 1) -And ($ceh -eq 1)){
write-host "All backups are found"
continue
}
}
# Сбрасываем счётчики и ищем второй бэкап
#
$trg=0
$ceh=0
Get-ChildItem -Path $dir| Sort-Object LastAccessTime | Select-Object -Last 12 | ForEach-Object {
if ((Test-Path $dir\$_\trg*.dt) -And ($trg -eq 0)){
Copy-Item -recurse -Force $dir\$_\trg* $temp
$trg=1
}
if ((Test-Path $dir\$_\ceh*.dt) -And ($ceh -eq 0)){
#Write-Host $ceh
Copy-Item -recurse -Force $dir\$_\ceh* $temp
$ceh=1
}
if (($trg -eq 1) -And ($ceh -eq 1)){
write-host "All backups are found"
continue
}
}

But the problem is that the break command terminates the entire script, not just the loop within which it runs.
Please tell me what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cardinalus, 2015-11-12
@cardinalus

For ForEach-Object - use return
for ForEach - use continue

N
nozzy, 2015-11-10
@nozzy

Попробуйте заменить break на continue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question