I’ve been working on a script to automate configuration of some network template files. Due to the way I’ve kind of been forced to dealing with searching and replacing in the files, I have some weird criteria that I’m trying my best to streamline.
As it stands because some of the files are vastly different, or mostly similar with parts that don’t overlap very well, I’ve been stuck with using a command like this one:
$array | Select-String $SearchString | where {$FilterString}
The best way I’ve come up with so far to do this is to use Invoke-Expression on the $FilterString, but I know that I should try to avoid using IEX at all costs. The filter string varies quite a bit, with variations being along the lines of:
$_ -Match '[.*]' $_ -NotMatch '[.*]'
and combinations of -Match and/or -NotMatch. I’ve been able to get away from IEX for some other functions now that I’ve discovered the & operator, but that doesn’t work for the case above. Is there some similar way to get a full string to evaluate inside of the Where {} statement or am I going to be stuck using IEX?
submitted by /u/Jaymzkerten
[link] [comments]
The post Using a string variable for the entire contents of a Where {} statement appeared first on How to Code .NET.