Reputation: 29
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.
Upvotes: 1
Views: 1665
Reputation: 200193
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}}, ...
Upvotes: 3