Counting Number of Times a Server Name Occurs in a CSV File

I am very new at PowerShell, and an intern, so I'm not the best at PowerShell right now.

I am importing a .CSV with a list of servers with vulnerabilities. I am getting only the name of the servers, not any other information. I want to count how many times each server shows up in this list. The reason why the same server will show up multiple times in this list is because it has multiple vulnerabilities. 

So, how can I script this in PowerShell?

So far I have:

$vulnservers= Import-Csv C:\Users\logsdmp\Desktop\vuln_top_10\Windows_Critical_List.csv


$servers = Foreach($server in $vulnservers){

$server = $server.NetBIOS_Name.ToString()

$server = $server.substring($server.length - 8, 8)

Write-host $server

}

I have taken the substrings because part of the name in the CSV file is the domain name, and I just need the server name (last 8 characters). So I need this script to be applied too.

I feel like the Group-Object and Get-Unique cmdlets will be used, and possibly a hashtable. I've been searching and searching and cannot quite get the information I need. Once again, I have only been using PowerShell for 2 weeks now. 

Thank you!

September 14th, 2015 12:24pm

Import-Csv C:\Users\logsdmp\Desktop\vuln_top_10\Windows_Critical_List.csv |
    ForEach-Objec{
        $_.NetBIOS_Name.substring($_.NetBIOS_Name.length - 8, 8)
   } |
   Group | Select Name, Count		
Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 12:31pm

Thank you!!!

Now, would you have any idea how to display those in order by decreasing count? And then I'd like to display the top 10 servers with the most vulnerabilities. 

  • Edited by mplogsdon 13 hours 49 minutes ago
September 14th, 2015 1:17pm

Thank you!!!

Now, would you have any idea how to display those in order by decreasing count?

See Sort-Object:

http://ss64.com/ps/sort-object.html

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 1:19pm

How do I apply the Sort-Object cmdlet and -Descending to the Count? 
September 14th, 2015 1:35pm

How do I apply the Sort-Object cmdlet and -Descending to the Count? 

Don't be helpless.  Try using "help sort-object" to learn how to do this.\

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 1:36pm

How do I apply the Sort-Object cmdlet and -Descending to the Count? 

Read the link I posted and try to work it out. If you can't, you can start a new thread by posting your current code and your errors.

September 14th, 2015 1:37pm

Import-Csv C:\Users\logsdmp\Desktop\vuln_top_10\Windows_Critical_List.csv |
    ForEach-Object{
    $_.NetBIOS_Name.substring($_.NetBIOS_Name.length - 8, 8)} | Group | Select Name, Count | Sort-Object -Descending Count | Select Name, Count -First 10

This code works, thanks!

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 1:52pm

Import-Csv C:\Users\logsdmp\Desktop\vuln_top_10\Windows_Critical_List.csv |
    ForEach-Object{
    $_.NetBIOS_Name.substring($_.NetBIOS_Name.length - 8, 8)} | Group | Select Name, Count | Sort-Object -Descending Count | Select Name, Count -First 10

This code works, thank you!

September 14th, 2015 1:53pm

That's great, you're welcome.
Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 2:02pm