Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell中的绝对新手有一个问题_Powershell - Fatal编程技术网

Powershell中的绝对新手有一个问题

Powershell中的绝对新手有一个问题,powershell,Powershell,我有一个满是日志的目录 我现在的代码是 (Get-ChildItem -Path |Select-string -Pattern -AllMatches).matches.count 这将得到一个8作为输出。但是我想要一个所有日志的列表,其中有多少日志被点击 像这样:“File2020.09.0217” 我该怎么做呢?试试这样的方法: $files = Get-Childitem -Path 'C:\xyz' -File $out = foreach ($file in $files){

我有一个满是日志的目录

我现在的代码是

(Get-ChildItem -Path |Select-string -Pattern -AllMatches).matches.count
这将得到一个8作为输出。但是我想要一个所有日志的列表,其中有多少日志被点击

像这样:
“File2020.09.0217”


我该怎么做呢?

试试这样的方法:

$files = Get-Childitem -Path 'C:\xyz' -File
$out = foreach ($file in $files){
    $content = Get-Content $file
    $matchCount = ($content | Select-String -Pattern 'xyz' -AllMatches).matches.count
    [PSCustomObject]@{
        Filename = $file
        Matches = $matchCount
    }
}
$out
由于使用
Filename
属性返回
MatchInfo
对象,因此可以使用
Group Object
Filename
对对象进行分组。固有地统计任何传递属性的出现次数

Select-String -Path $path -Pattern $pattern -AllMatches |
    Group-Object -Property FileName | Foreach-Objecet {
        "{0} {1}" -f $_.Name,$_.Count
    }
组对象
名称
属性包含分组属性的值。其
Count
属性列出了这些分组属性在管道引入对象中匹配的出现次数


您可以使用哈希表跟踪文件中的命中数

$tracker = @{}
Select-String -Path $path -Pattern $pattern -AllMatches | Foreach-Object {
    if ($tracker.Contains($_.Filename)) {
        $tracker[$_.Filename]++
    } else { 
        $tracker[$_.Filename] = 1
    }
}
$tracker.GetEnumerator() | Foreach-Object {
    "{0} {1}"-f $_.Key,$_.Value
}

您好,欢迎来到苏。请慢慢来读。它的工作并没有考验你的能力,但很难找到你的答案。