我有很多要做的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文件。区域的格式为XXX-WS或XXX-SRV,其中XXX可以介于3到17个字母之间。
我希望摘要表看起来像这样
ABC ###
ABC-WS ##
ABC-SRV ##
DEF ###
DEF-WS ##
DEF-SRV ##
我的想法是对原始CSV文件进行计数,或者对每个CSV文件中的行数进行计数,然后为标题行减去1。
现在区域是动态的,所以我不能只说我要ZONE 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 ..\
答案 0 :(得分:1)
找到了解决方案
$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