【问题标题】:Select-String -Pattern $variable issueSelect-String -Pattern $variable 问题
【发布时间】:2016-09-01 21:26:27
【问题描述】:

我正在尝试通过交叉引用两个文本文档来创建报告。我有 C:\formeremployees.txt 和 C:\shareaudit.txt。你可以猜到,formeremployees.txt 只列出了前雇员的用户名。没有标题;只有用户名。 C:\shareaudit.txt 包含共享上每个文件夹的列表,ACL 信息位于文件夹路径旁边的同一行。

这是我创建报告的尝试,该报告仅列出了具有来自原employees.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
}

但不幸的是,我收到以下错误:

Select-String : 无法将参数绑定到参数“模式”,因为它是
空字符串。
在行:7 字符:71
+ $Output = Select-String -Path "C:\Shareaudit.txt" -Pattern "$User"
+ ~~~~~~~
    + CategoryInfo : InvalidData: (:) [Select-String], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SelectStringCommand

Select-String:无法将参数绑定到参数“模式”,因为它是
空字符串。
在行:7 字符:71
+ $Output = Select-String -Path "C:\ShareAudit.txt" -Pattern "$User"
+ ~~~~~~~
    + CategoryInfo : InvalidData: (:) [Select-String], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SelectStringCommand

然后我得到一个悲伤的、空的 completereport.txt 文件。我似乎无法让它工作或知道它是否可能。

编辑________________________

这是我尝试过的其他方法和结果:

$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
}

这给了我一个空白的 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 

据我所知,这没有做任何事情。完成时没有创建 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
}

这给了我一个空白的文本文档。

 $Output = Select-String -Path "C:\Shareaudit.txt" -Pattern "<single username from formeremployeee.txt>"
  $Output.Line | Out-File C:\completereport.txt -Append

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

为了进一步测试,我尝试了这个:

$Users=(Get-Content C:\formeremployees.txt) -ne ''

foreach ($User in $Users) {
  Select-String -Path "C:\Shareaudit.txt" -Pattern "$User"
}

这没有输出任何结果。文本formeremployee.txt 文件列出用户名如下:

username1
username2
username3
username4

它的格式是否错误?

【问题讨论】:

  • 检查是否真的有空元素:foreach ($User in $Users) { "-$User-" }
  • 这将用户列为:-username - -username1 - -username2 -
  • 那些元素不会产生你所说的错误。请提供样本输入数据,以便我们重现问题。

标签: powershell


【解决方案1】:

最明显的答案是您的FormerEmployee.txt 文件中有空行。最简单的解决方案是更新您的第一行:

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

为了加快速度,我可能会做的是从用户中创建一个正则表达式模式,然后运行一次 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

【讨论】:

  • 为什么不(Get-Content C:\formeremployees.txt) -ne ''
  • 如果文件不存在,'Set-Content C:\completereport.txt' 会创建该文件吗?我跑了这个看看它是否有效。它现在已经运行了大约 2 分钟,还没有 C:\completereport.txt 文件,但我会等到脚本完成。不过感谢您的帮助。我还在学习,这让我有很多东西可以研究。
  • 虽然我从上面使用您的脚本没有收到任何错误,但没有使用结果创建 C:\completereport.txt 文件。
  • 没有样本数据来复制问题,很难解决您遇到的任何问题。
  • 我在之前的employee.txt 文件中的每个用户名后面都有空格。只是用空替换空间,我得到了我需要的报告。你一开始是对的,我想在应得的地方给予赞扬。我的菜鸟错误。
猜你喜欢
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多