为什么排除子句无法按预期工作.. powershell?

时间:2019-04-05 15:31:19

标签: powershell

我有一个Powershell脚本,旨在处理一些备份文件,存档月末备份(如果是月初),然后删除9天内的所有内容。我试图告诉它排除存档文件夹,但它似乎忽略了这一点,我不确定为什么。

我尝试使用-notlike $ Exlcude +%,我尝试过-notmatch -NE ..即使我知道那都不行,我什至尝试了notcontains。.我尝试将$ exclude应用于代码的每个部分可以访问该文件夹的位置,并且仍在复制该文件夹。

#If first of the month copy latest backups to Monthly Backup Folder - will copy full folder path
PARAM($BackupPath="\\SomeServer\sqltestbackups\",
$Exclude= "\\SomeServer\sqltestbackups\Archive")



   #Grab a recursive list of all subfolders
$SubFolders = dir $BackupPath -Recurse | Where-Object {$_.PSIsContainer} | ForEach-Object -Process {$_.FullName}

#Iterate through the list of subfolders and grab the first file in each


$Year = (get-date).Year
$Month = (get-date).Month
$StartOfMonth = Get-Date -Year $Year -Month $Month -Day 1
$Today = (Get-Date).day
$Date = (GET-DATE).AddDays(-9)
$PrevMonth = (GET-DATE).year.ToString()+(Get-Culture).DateTimeFormat.GetMonthName((Get-Date).AddMonths(-1).month)
$Destination=$Exclude + "\" + $Prevmonth +"\"

IF (!(Test-Path -path $destination)) {New-Item $destination -Type Directory}

IF($Today -eq '5')
{
$Path = $BackupPath #Root path to look for files
$DestinationPath = $Destination #Remote destination for file copy

#Grab a recursive list of all subfolders
$SubFolders = dir $Path -Recurse | Where-Object {$_.PSIsContainer -and $_.DirectoryName -notmatch $Exclude} | ForEach-Object -Process {$_.FullName}

#Iterate through the list of subfolders and grab the first file in each
ForEach ($Folder in $SubFolders)
    {
    $FullFileName = dir $Folder | Where-Object {!$_.PSIsContainer -and $_.DirectoryName -notmatch $Exclude} | Sort-Object {$_.LastWriteTime} -Descending | Select-Object -First 1

    #For every file grab it's location and output the robocopy command ready for use
    ForEach ($File in $FullFileName)
        {
        $FilePath = $File.DirectoryName
        $ArchFolder = $File.DirectoryName
        $ArchFolder=$ArchFolder.Replace($BackupPath,"")+"\"
        $FinalPath=$destinationPath+$ArchFolder
        $FileName = $File.Name
        robocopy $FilePath $FinalPath $FileName /A-:SH /R:6 /W:30 /Z 
        }
    }
}


# Delete files older than 9 days that are not contained within the month end folder
Get-ChildItem $BackupPath -Recurse |
  Where-Object { !($_.PSIsContainer) -and
                   $_.LastWriteTime -lt $Date -and
                   $_.Directory -notlike $Exclude+"%" } |
    Remove-Item

除复制月份结束部分外,该代码有效。.在此部分中,它包括存档文件夹,而我最终复制了上个月。.该脚本旨在将文件放置在archive / YM / FullPath中备份,所以即使我试图通过SO HARD从文件夹中排除该路径,发生的事情仍是归档/ YM / ARchive / YM / FullPath。

自动复制出现问题的示例

来源: \ SomeServer \ sqltestbackups \ ARCHIVE \ 2019March \ SomeOtherServer \ SQLBackups \ SomeDatabase \ master \

目标: \ SomeServer \ sqltestbackups \ Archive \ 2019March \ ARCHIVE \ 2019March \ SomeOtherServer \ SQLBackups \ SomeDatabase \ master \

2 个答案:

答案 0 :(得分:0)

类型DirectoryInfo没有属性DirectoryName。请尝试使用属性BaseName

# dir, gci are aliases for Get-ChildItem
Get-ChildItem $Path -Recurse `
    | Where-Object { $_.PSIsContainer -and $_.BaseName -notmatch $Exclude }

有效。

要查看返回的类型以及存在的成员类型

Get-ChildItem .\ | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.GetType() }
# and
Get-ChildItem .\ | Where-Object { $_.PSIsContainer } | Get-Member

但是要小心:在包含许多文件和子目录的目录上,您会得到很多输出。

也请查看关于stackoverflow的答案How can I exclude multiple folders using Get-ChildItem -exclude?。这个答案提供了从v1.0到v5.0的许多精美的PowerShell解决方案。

答案 1 :(得分:0)

问题是

$Exclude= "\\SomeServer\sqltestbackups\Archive" I needed to double up the \s to be

$Exclude= "\\\\SomeServer\\sqltestbackups\\Archive"

完成此脚本后,效果很好。