0

我有一个函数,它使用Get-ChildItem来计算文件夹中的最新文件。我需要在一个文件夹中找到最新的增量备份并编写一个 EXE 文件来挂载它。但是,在同一个文件夹中,所有不同的服务器都有多个备份链,即

SERVER1_C_VOL_b001_i015.spi
SERVER2_D_VOL_b001_i189.spi
SERVER1_C_VOL_b002_i091.spi
SERVER1_E_VOL_b002_i891.spi (This is the newest file created)

我只想看SERVER1,只看,C_VOL只看b001——别无其他。

我有所有这些单独的组件:驱动器号、服务器名称、存储在数组中的 b00X 编号。

那我怎么能去使用Get-ChildItem过滤-Include器来只看:

.spi
SERVER1
C_VOL
b001

鉴于我将所有这些单独的组件放在一个从文本文件中获取的数组中:

Get-Content .\PostBackupCheck-TextFile.txt | Select-Object -First $i { $a = $_ -split ' ' ; $locationArray += "$($a[0]):\$($a[1])\$($a[2])" ; $imageArray += "$($a[2])_$($a[3])_VOL_b00$($a[4])_i$($a[5]).spi" }

我继续尝试过滤,然后我被卡住了:

$latestIncremental = Get-ChildItem -Path ${shadowProtectDataLocation}\*.* -Include *.spi | Sort-Object LastAccessTime -Descending | Select-Object -First 1 

我已经过滤了 .spi,但我怎样才能只包含 C(用于卷)、b00x 的编号和服务器名称?

4

1 回答 1

1

I want only to look at SERVER1, look at only the C_VOL and look at only b001 - nothing else.

Is this meant to be inclusive or exclusive (the question is not 100% clear)?

Ie. is it SERVER1 and C_VOL and …, or is it any file matching any one of them? Based on the final paragraph it seems you want the former.

I see two approaches, depending how closely the match criteria array matches the filename structuyre..

  1. If your matching files have the a form that matches the filter criteria (eg. as in Q's example with same ordering of name components apart of extension first) then you can build that "like pattern" dynamically:

    $parts = … # the match parts array
    $pattern = (($parts | Select-Object -skip 1) -join '*') + $parts[0]
    $file = Get-ChildItem -Path $basePath -include $pattern | Sort-Object …
    
  2. Apply each part of the pattern incrementally, again assuming the extension comes first in the file:

    $parts = … # the match parts array
    $potentialFiles = Get-ChildItem -Path $basePath -include ('*' + $parts[0])
    $parts = $parts | Select-Object -skip 1
    foreach ($p in $parts) {
      $potentialFiles = $potentialFiles | Where-Objecft { $_.Name -like ('*' + $p + '*') }
    }
    # Now sort and select -first 1 to get single file (if any left to match)
    
于 2012-11-18T10:56:23.810 回答