I'm trying to create a report by cross referencing two text documents. I have C:\formeremployees.txt and C:\shareaudit.txt. As you can guess the formeremployees.txt has a list of former employee usernames only. No Headers; only usernames. The C:\shareaudit.txt contains a list of every folder on a share with the ACL info on the same line next to the folder path.
Here was my attempt at creating a report that only lists the lines that have user accounts from the 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, I get the following error:
Select-String : Cannot bind argument to parameter 'Pattern' because it is an
empty string.
At 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 to parameter 'Pattern' because it is an
empty string.
At 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 I get a sad, empty completereport.txt file. I can't seem to get this to work or know if it's possible.
Edit________________________
Here's what else I've tried and the 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 a 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 as far as I can tell didn't do anything. There was no completereport.txt document created when it 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 a blank text document.
$Output = Select-String -Path "C:\Shareaudit.txt" -Pattern "<single username from formeremployeee.txt>"
$Output.Line | Out-File C:\completereport.txt -Append
When I put in a username that I knew still had permissions to some folders in the share and was also in the formeremployee.txt, the script worked as intended and gave me a list of the folders I needed so there's nothing wrong with the bottom part of the script, so I'm guessing something is up with the formeremployee.txt or the way I used the $Users variable.
To test further, I 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 any results. The text formeremployee.txt file lists the usernames as follows:
username1
username2
username3
username4
Is it in the wrong format for this?
foreach ($User in $Users) { "-$User-" }