简体   繁体   English

Select-String -Pattern $ variable问题

[英]Select-String -Pattern $variable issue

I'm trying to create a report by cross referencing two text documents. 我正在尝试通过交叉引用两个文本文档来创建报告。 I have C:\\formeremployees.txt and C:\\shareaudit.txt. 我有C:\\ formeremployees.txt和C:\\ shareaudit.txt。 As you can guess the formeremployees.txt has a list of former employee usernames only. 如您所料,agodaemployees.txt仅包含前雇员的用户名列表。 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. C:\\ shareaudit.txt包含共享中每个文件夹的列表,并且ACL信息位于文件夹路径旁边的同一行。

Here was my attempt at creating a report that only lists the lines that have user accounts from the formeremployees.txt: 这是我尝试创建一个报告的尝试,该报告仅列出了来自employeeemployees.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. 然后,我得到一个悲伤的空的completereport.txt文件。 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. 这给了我一个空白的C:\\ completereport.txt文件。

$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. 完成时没有创建completereport.txt文档。

$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. 当我输入一个我知道仍然对共享中的某些文件夹具有访问权限的用户名时,并且还在前employee.txt中,该脚本按预期工作,并为我提供了我需要的文件夹列表,因此底部没有任何问题脚本,所以我猜这与前employee.txt或我使用$ Users变量的方式有关。

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: 文本previousemployee.txt文件列出了以下用户名:

username1
username2
username3
username4

Is it in the wrong format for this? 格式错误吗?

The most obvious answer is that you have blank lines in your FormerEmployee.txt file. 最明显的答案是您的FormerEmployee.txt文件中有空行。 The simplest solution is to update your first line: 最简单的解决方案是更新第一行:

$Users = Get-Content C:\formeremployees.txt|?{!([string]::IsNullOrWhitespace($_))}

What I would probably do to speed things up is make a regex pattern out of the users, and run the Select-String once, instead of once per user: 为了加快速度,我可能要做的是在用户之外创建一个正则表达式模式,并运行一次Select-String ,而不是每个用户运行一次:

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM