我有一个脚本可以从机器中提取.log文件列表并将它们保存到共享中。我正在尝试编写一个powershell脚本来解析错误代码的.txt文件,然后在日志文件中查找它们。这就是我到目前为止所做的:
Param(
[Parameter (Mandatory=$true,HelpMessage="Enter full original file name (ex: HYPERLINK "C:\MyFiles\MyFile.log)"\\*\blahblah.log)")][string]$FilePath
$a = Get-Content "Problem_codes.txt"
foreach ($i in $a)
{$i}
)
(Select-String -Pattern $a -Path $FilePath).Count
如果我(Select-String -Pattern 0x00080b -Path $FilePath).Count
它似乎有效,当我尝试从文件中读取barfs:
Select-String : Cannot bind argument to parameter 'Pattern' because it is null.
At line:1 char:25
+ (Select-String -Pattern $a -Path $FilePath).Count
+ ~~
+ CategoryInfo : InvalidData: (:) [Select-String], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SelectStringCommand
答案 0 :(得分:0)
您应该使用ISE或PowerGui编写代码(语法着色,匹配括号等等)。这是我尝试将您的代码更改为正确的PowerShell,但我不尝试...
Param([Parameter (Mandatory=$true,HelpMessage="Enter full original file name (ex: HYPERLINK `"C:\MyFiles\MyFile.log)`"\\*\blahblah.log)")][string]$FilePath)
$a = Get-Content "Problem_codes.txt"
foreach ($i in $a)
{
$i
(Select-String -Pattern $i -Path $FilePath).Count
}