Powershell计数在单个列中匹配变量

Powershell计数在单个列中匹配变量,powershell,csv,count,Powershell,Csv,Count,我有一个CSV文件,我做了很多工作。我最近的任务是添加摘要表 说到这里,我有一个CSV文件,我从一个网站上提取并发送了很多支票。代码如下: $Dups = import-csv 'C:\Working\cylrpt.csv' | Group-Object -Property 'Device Name'| Where-Object {$_.count -ge 2} | ForEach-Object {$_.Group} | Select @{Name="Device Name"; Expressi

我有一个CSV文件,我做了很多工作。我最近的任务是添加摘要表

说到这里,我有一个CSV文件,我从一个网站上提取并发送了很多支票。代码如下:

$Dups = import-csv 'C:\Working\cylrpt.csv' | Group-Object -Property 'Device Name'| Where-Object {$_.count -ge 2} | ForEach-Object {$_.Group} | Select @{Name="Device Name"; Expression={$_."Device Name"}},@{Name="MAC"; Expression={$_."Mac Addresses"}},Zones,@{Name="Agent"; Expression={$_."Agent Version"}},@{Name="Status"; Expression={$_."Is online"}}
$Dups | Export-CSV $working\temp\01-Duplicates.csv -NoTypeInformation
$csvtmp = Import-CSV $working\cylrpt.csv | Select @{N='Device';E={$_."Device Name"}},@{N='OS';E={$_."OS Version"}},Zones,@{N='Agent';E={$_."Agent Version"}},@{N='Active';E={$_."Is Online"}},@{N='Checkin';E={[DateTime]$_."Online Date"}},@{N='Checked';E={[DateTime]$_."Offline Date"}},Policy
$csvtmp | %{
    if ($_.Zones -eq ""){$_.Zones = "Unzoned"}
    }
$csvtmp | Export-Csv $working\cy.csv -NoTypeInformation
import-csv $working\cy.csv | Select Device,policy,OS,Zones,Agent,Active,Checkin,Checked | % {
    $_ | Export-CSV -path $working\temp\$($_.Zones).csv -notypeinformation -Append
    }
  • 第一个检查是重复的,我为此使用了单独的代码行,因为我想为重复创建一个CSV
  • 第二个复选框使用“未分区”回填分区列下的所有空白单元格
  • 第三件事是遍历整个CSV文件,并为每个区域创建一个CSV文件
  • 这就是我的基地。我需要添加另一个CSV文件以获得区域信息的摘要。区域的格式为XXX-WS或XXX-SRV,其中XXX可以是3到17个字母

    我希望汇总表是这样的

    ABC         ###
    ABC-WS      ##
    ABC-SRV     ##
    
    DEF         ###
    DEF-WS      ##
    DEF-SRV     ##
    
    我的想法是对原始CSV文件进行计数,或者对每个CSV文件中的行数进行计数,然后减去1作为标题行

    现在区域是动态的,所以我不能说我想要区域XYZ,因为该区域可能不存在

    因此,我需要的是能够计算原始文件中类似的区域类型,并将其输出到数组或文件,这将是我首选的方法,以提供具有相同区域名称的项目数。我只是不知道如何编写它来查找和计算匹配的变量。下面是我试图用来获取计数的代码:

    import-csv C:\Working\cylrpt.csv | Group-Object -Property 'Zones'| ForEach-Object {$_.Group} | Select @{N='Device';E={$_."Device Name"}},Zones | % {
    $Znum = ($_.Zones).Count
       If ($Znum -eq $null) {
                $Znum = 1
            } else {
                $Znum++
        }
    
    }
    $Count = ($_.Zones),$Znum | Out-file C:\Working\Temp\test2.csv -Append
    
    以下是完整代码减去报告键:

    $cylURL = "https://protect.cylance.com/Reports/ThreatDataReportV1/devices/"
    $working = "C:\Working"
    Remove-item -literalpath "\\?\C:\Working\Cylance Report.xlsx"
    Invoke-WebRequest -Uri $cylURL -outfile $working\cylrpt.csv
    $Dups = import-csv 'C:\Working\cylrpt.csv' | Group-Object -Property 'Device Name'| Where-Object {$_.count -ge 2} | ForEach-Object {$_.Group} | Select @{Name="Device Name"; Expression={$_."Device Name"}},@{Name="MAC"; Expression={$_."Mac Addresses"}},Zones,@{Name="Agent"; Expression={$_."Agent Version"}},@{Name="Status"; Expression={$_."Is online"}}
    $Dups | Export-CSV $working\temp\01-Duplicates.csv -NoTypeInformation
    $csvtmp = Import-CSV $working\cylrpt.csv | Select @{N='Device';E={$_."Device Name"}},@{N='OS';E={$_."OS Version"}},Zones,@{N='Agent';E={$_."Agent Version"}},@{N='Active';E={$_."Is Online"}},@{N='Checkin';E={[DateTime]$_."Online Date"}},@{N='Checked';E={[DateTime]$_."Offline Date"}},Policy
    $csvtmp | %{
        if ($_.Zones -eq ""){$_.Zones = "Unzoned"}
        }
    $csvtmp | Export-Csv $working\cy.csv -NoTypeInformation
    import-csv $working\cy.csv | Select Device,policy,OS,Zones,Agent,Active,Checkin,Checked | % {
        $_ | Export-CSV -path $working\temp\$($_.Zones).csv -notypeinformation -Append
        }
    
    cd $working\temp;
    Rename-Item "Unzoned.csv" -NewName "02-Unzoned.csv"
    Rename-Item "Systems-Removal.csv" -NewName "03-Systems-Removal.csv"
    $CSVFiles = Get-ChildItem -path $working\temp -filter *.csv 
    $Excel = "$working\Cylance Report.xlsx"
    $Num = $CSVFiles.Count
    Write-Host "Found the following Files:  ($Num)"
    
    ForEach ($csv in $CSVFiles) {
        Write-host "Merging $CSVFiles.Name"
    }
        $EXc1 = New-Object -ComObject Excel.Application
        $Exc1.SheetsInNewWorkBook = $CSVFiles.Count
        $XLS = $EXc1.Workbooks.Add()
        $Sht = 1
    ForEach ($csv in $CSVFiles) {
        $Row = 1
        $Column = 1
        $WorkSHT = $XLS.WorkSheets.Item($Sht)
        $WorkSHT.Name = $csv.Name -Replace ".csv",""
        $File = (Get-Content $csv)
        ForEach ($line in $File) {
            $LineContents = $line -split ',(?!\s*\w+")'
            ForEach ($Cell in $LineContents) {
                $WorkSHT.Cells.Item($Row,$Column) = $Cell -Replace '"',''
                $Column++
            }
            $Column = 1
            $Row++
        }
        $Sht++
    }
    $Output = $Excel
    $XLS.SaveAs($Output)
    $EXc1.Quit()
    Remove-Item *.csv
    cd ..\
    
    找到了解决办法

    $Zcount = import-csv C:\Working\cylrpt.csv | where Zones -ne "$null" | select @{N='Device';E={$_."Device Name"}},Zones | group Zones | Select Name,Count
    $Zcount | Export-Csv -path C:\Working\Temp\01-Summary.csv -NoTypeInformation