powershell - Select-String -Pattern $variable issue -


i'm trying create report cross referencing 2 text documents. have c:\formeremployees.txt , c:\shareaudit.txt. can guess formeremployees.txt has list of former employee usernames only. no headers; usernames. c:\shareaudit.txt contains list of every folder on share acl info on same line next folder path.

here attempt @ creating report lists lines have user accounts formeremployees.txt:

$users = get-content c:\formeremployees.txt  foreach ($user in $users) {   $output = select-string -path "c:\shareaudit.txt" -pattern "$user"   $output.line | out-file c:\completereport.txt -append } 

but unfortunately, following error:

select-string : cannot bind argument parameter 'pattern' because empty string. @ line:7 char:71 + $output = select-string -path "c:\shareaudit.txt" -pattern "$user" +                                                            ~~~~~~~     + categoryinfo          : invaliddata: (:) [select-string], parameterbindingvalidationexception     + fullyqualifiederrorid : parameterargumentvalidationerroremptystringnotallowed,microsoft.powershell.commands.selectstringcommand  select-string : cannot bind argument parameter 'pattern' because empty string. @ line:7 char:71 + $output = select-string -path "c:\shareaudit.txt" -pattern "$user" +                                                            ~~~~~~~     + categoryinfo          : invaliddata: (:) [select-string], parameterbindingvalidationexception     + fullyqualifiederrorid : parameterargumentvalidationerroremptystringnotallowed,microsoft.powershell.commands.selectstringcommand

then sad, empty completereport.txt file. can't seem work or know if it's possible.

edit________________________

here's else i've tried , results:

$users = get-content c:\formeremployees.txt|?{!([string]::isnullorwhitespace($_))}  foreach ($user in $users) {   $output = select-string -path "c:\shareaudit.txt" -pattern "$user"   $output.line | out-file c:\completereport.txt -append } 

this gave me blank c:\completereport.txt document.

$users = get-content c:\formeremployees.txt|?{!([string]::isnullorwhitespace($_))} $pattern = ($users|foreach{[regex]::escape($_)}) -join '|' get-content "c:\shareaudit.txt" | where{$_ -match $pattern} | set-content c:\completereport.txt  

this far can tell didn't anything. there no completereport.txt document created when finished.

$users=(get-content c:\formeremployees.txt) -ne ''  foreach ($user in $users) {   $output = select-string -path "c:\shareaudit.txt" -pattern "$user"   $output.line | out-file c:\completereport.txt -append } 

this gave me blank text document.

 $output = select-string -path "c:\shareaudit.txt" -pattern "<single username formeremployeee.txt>"   $output.line | out-file c:\completereport.txt -append 

when put in username knew still had permissions folders in share , in formeremployee.txt, script worked intended , gave me list of folders needed there's nothing wrong bottom part of script, i'm guessing formeremployee.txt or way used $users variable.

to test further, tried this:

$users=(get-content c:\formeremployees.txt) -ne ''  foreach ($user in $users) {   select-string -path "c:\shareaudit.txt" -pattern "$user" } 

this didn't output results. text formeremployee.txt file lists usernames follows:

username1 username2 username3 username4 

is in wrong format this?

the obvious answer have blank lines in formeremployee.txt file. simplest solution update first line:

$users = get-content c:\formeremployees.txt|?{!([string]::isnullorwhitespace($_))} 

what speed things make regex pattern out of users, , run select-string once, instead of once per user:

$users = get-content c:\formeremployees.txt|?{!([string]::isnullorwhitespace($_))} $pattern = ($users|foreach{[regex]::escape($_)}) -join '|' get-content "c:\shareaudit.txt" | where{$_ -match $pattern} | set-content c:\completereport.txt 

Comments