Author Posts

July 13, 2016 at 3:08 am

I am looking for some assistance on how i can filter certian lines out of an array.

Here is an example of what i am trying to do

I have an exclude array

$dbexclude = '.master.','.model.','.AdminDB.'

I have an array filled by a command

$dblist = & "C:\Program Files\Veritas\NetBackup\bin\bplist" -C $BackupHost -t 15 -s $BackupDate -R / | unique

Here is some sample lines from that array:

dbserver1.MSSQL7.dbserver1.db.old_MasterAuditDatabase.~.7.001of001.20160712114338..C:\
dbserver1.MSSQL7.dbserver1.db.msdb.~.7.001of001.20160712114310..C:\
dbserver1.MSSQL7.dbserver1.db.model.~.7.001of001.20160712114247..C:\
dbserver1.MSSQL7.dbserver1.db.mhilltest.~.7.001of001.20160712114226..C:\
dbserver1.MSSQL7.dbserver1.db.MASTER_AUDIT_DATABASE.~.7.001of001.20160712114149..C:\
dbserver1.MSSQL7.dbserver1.db.master.~.7.001of001.20160712114126..C:\
dbserver1.MSSQL7.dbserver1.db.Kronos_Punches.~.7.001of001.20160712114105..C:\

What i would like to see :

I would like to have a final array with values that would exclude matches form the exclude array so, from the example above the results should be:

dbserver1.MSSQL7.dbserver1.db.old_MasterAuditDatabase.~.7.001of001.20160712114338..C:\
dbserver1.MSSQL7.dbserver1.db.msdb.~.7.001of001.20160712114310..C:\
dbserver1.MSSQL7.dbserver1.db.mhilltest.~.7.001of001.20160712114226..C:\
dbserver1.MSSQL7.dbserver1.db.MASTER_AUDIT_DATABASE.~.7.001of001.20160712114149..C:\
dbserver1.MSSQL7.dbserver1.db.Kronos_Punches.~.7.001of001.20160712114105..C:\

Any thoughts on how i could accomplish this?

July 13, 2016 at 3:39 am

$dblist = #....# |
Where-Object {
    $result = $true
    Foreach ($item in $dbexclude) {
        If($_ -like "*$item*") {
            $result = $false
            Break
        }
    }
    $result
} 

I'm on a tablet so there are probably typos. Where-object can have a script block that does whatever comparisons you want. Have the script block return true for objects that need to pass down the pipeline and false for the ones to filter out.

  • This reply was modified 2 years, 1 month ago by  Craig Duff.

July 13, 2016 at 12:58 pm

This will perform a case-sensitive comparison.

$array -cnotmatch '(\.master|\.model|\.AdminDB)'