1

I need some help. I am trying to get a script working which uses a text file full of words that need filtering from the pipeline of a command.

This is the $filters text file I have created (NB - The values are server volume names):

('75ab4a4776bc11e09ae1806e6f6e6963','') 
('296b67b2c1f111e298d6806e6f6e6963','') 
('e3636932b95111e2a4be806e6f6e6963','')
('296b67b2c1f111e298d6806e6f6e6963','') 
('3f149289a83911e380b3806e6f6e6963','') 
('61aee3ccd72911e4ad8e806e6f6e6963','')
('97c80f3d9b7911e2b24e806e6f6e6963','') 
('3f149289a83911e380b3806e6f6e6963','') 
('181BCC0400D34ED0BC61CB7B1AC1FB9E','') 
('52120b028f8011dfb1bd806e6f6e6963','') 
('dfff9b1f76a911e09259806e6f6e6963','') 
('d4843b91172311e4ba95806e6f6e6963','') 
('668b283173711e4bfdb806e6f6e6963','') 
('d68e8a9cc5d211e680b4806e6f6e6963','') 
('d68e8a9cc5d211e680b4806e6f6e6963','') 
('3f149289a83911e380b3806e6f6e6963','')

What I want to do is parse all those values in the text file into the following command:

Get-BEAgentServer | Where-Object {
    ($_.BackupStatus -eq "BackupSucceeded")
} | Where-Object {
    ($_.BackupStatus -ne "unknown")
}

If I add this, I get the filtered output I want:

Select-Object @{n='Server Name';e={
    $_.Name -replace ('3f149289a83911e380b3806e6f6e6963','') `
        -replace ('61aee3ccd72911e4ad8e806e6f6e6963','') `
        -replace  ('97c80f3d9b7911e2b24e806e6f6e6963','') `
        -replace ('3f149289a83911e380b3806e6f6e6963','') `
        -replace ('181BCC0400D34ED0BC61CB7B1AC1FB9E','') `
        -replace ('52120b028f8011dfb1bd806e6f6e6963','') `
        -replace ('dfff9b1f76a911e09259806e6f6e6963','') `
        -replace ('d4843b91172311e4ba95806e6f6e6963','') `
        -replace ('668b283173711e4bfdb806e6f6e6963','') `
        -replace ('d68e8a9cc5d211e680b4806e6f6e6963','') `
        -replace ('d68e8a9cc5d211e680b4806e6f6e6963','') `
        -replace ('3f149289a83911e380b3806e6f6e6963','') `
        -replace ('Volume', 'SysVol') `
        -replace ('7ECA562AF49C475EB10168CF5C0D302C','') `
        -replace ('e3636932b95111e2a4be806e6f6e6963','') `
        -replace ('296b67b2c1f111e298d6806e6f6e6963','') `
        -replace ('75ab4a4776bc11e09ae1806e6f6e6963','')
}}, starttime, setendtime | Sort-Object "Server Name" -Unique | select -Skip 1

My question is, is it possible to simplfy the command to replace using $filters instead of manually adding each string to filter? This will make it easier for me to add filter strings in the future.

1 Answer 1

3

Put just the strings you want removed in the input file:

75ab4a4776bc11e09ae1806e6f6e6963
296b67b2c1f111e298d6806e6f6e6963
e3636932b95111e2a4be806e6f6e6963
296b67b2c1f111e298d6806e6f6e6963
3f149289a83911e380b3806e6f6e6963
61aee3ccd72911e4ad8e806e6f6e6963
97c80f3d9b7911e2b24e806e6f6e6963
3f149289a83911e380b3806e6f6e6963
181BCC0400D34ED0BC61CB7B1AC1FB9E
52120b028f8011dfb1bd806e6f6e6963
dfff9b1f76a911e09259806e6f6e6963
d4843b91172311e4ba95806e6f6e6963
668b283173711e4bfdb806e6f6e6963
d68e8a9cc5d211e680b4806e6f6e6963
d68e8a9cc5d211e680b4806e6f6e6963
3f149289a83911e380b3806e6f6e6963

Read the file and build one regular expression from it:

$filter = (Get-Content 'C:\path\to\filters.txt' | ForEach-Object {
    [regex]::Escape($_)
}) -join '|'

Then remove the content with a single replacement:

Select-Object @{n='Server Name';e={$_.Name -replace $filter}}, ...
Sign up to request clarification or add additional context in comments.

2 Comments

What's the purpose of that Escape method versus just joining the strings with a pipe?
@TheIncorrigible1 To ensure that the strings from the file are treated as literal matches, not regular (sub)expressions. With the sample input it doesn't make a difference, but I perfer to err on the safe side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.