Powershell - Using variables in -Include filter

Posted by TheD on Stack Overflow See other posts from Stack Overflow or by TheD
Published on 2012-11-17T23:34:21Z Indexed on 2012/11/18 11:00 UTC
Read the original article Hit count: 236

Filed under:

(again!)

My final question for the night. I have a function which uses the Get-ChildItem to work out the newest file within a folder. The reason being I need to find the latest incremental backup in a folder and script an EXE to mount it. However, within this same folder there are multiple backup chains for all different servers: i.e.

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)

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

I have all these seperate components: the drive letter, the Server Name, the b00X number stored in array.

How then can I go and use the Get-ChildItem with the -Include filter to only look at:

.spi
SERVER1
C_VOL
b001

Given I have all of these separate components in an array taken from a text file:

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" }

I then go onto try and filter then and then get stuck:

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

I have the .spi filtered, but how can I also just include the C (for volume), the number for the b00x and the server name.

Thanks!

© Stack Overflow or respective owner

Related posts about powershell