find unique file server in multiple .csv files in diffrent folders

det103 81 Reputation points
2022-05-12T22:23:07.887+00:00

Hello:
how do i get unique file server names which has "ns" in their fqdn on multiple csv files in one folder, which has mulitple subfolders via powershell
thanks

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 31,571 Reputation points
    2022-05-13T16:14:34.247+00:00

    So you have tab separated values.

    cls
    $Servers = @()
    Get-ChildItem "C:\temp\*.csv" -Recurse | foreach {
        ""
        $_.FullName                             # display the file name that we are processing 
        $csv = Get-Content $_.FullName          # read the file contents 
        foreach ($entry in $csv) {
            $values = $entry.split("`t")        # tab separated columns, split into an array  
            foreach ($v in $values) {
                if ($v.contains(":\\")) {             # drive letter : and unc path
                    $server = $v.split("\")[2]       # get the server name 
                    $server                          # display the name that we found
                    if ($server -match "-ns"){
                        $Servers += $server          # add it to the results
                    } 
                }
            }
        }
    }
    ""
    "Here is what I found."
    $Servers | Sort-Object -Unique
    

3 additional answers

Sort by: Most helpful
  1. MotoX80 31,571 Reputation points
    2022-05-13T01:14:58.343+00:00

    Assuming that each csv has a column named "server"...... and that the names are in the format "something.ns.somethingelse"...

    cls
    $Servers = @()
    Get-ChildItem "C:\temp\*.csv" -Recurse | foreach {
        $_.FullName                      # show the file name that we are processing. 
        $csv = Import-Csv $_.FullName
        foreach ($entry in $csv) {
            if ($entry.server -match "\.ns\."){                 # use "ns" to look for just those 2 characters 
                $entry.server             # show the server name that we found.
                $Servers += $entry.server
            }
        }
    }
    ""
    "Here is what I found."
    $Servers | Sort-Object -Unique
    

  2. det103 81 Reputation points
    2022-05-13T15:25:34.457+00:00

    201851-book1.txt

    so in this example i just need anything with ns i need parsed. i was going to upload .csv but it doesnt allow me import .csv so i converted into .txt
    thanks

    0 comments No comments

  3. Limitless Technology 39,351 Reputation points
    2022-05-18T07:31:17.743+00:00

    Hi there,

    This example might help you with your requirements.

    Import all CSV files.

    Get-ChildItem $PSScriptRoot\csv*.csv -File -PipelineVariable file | Import-Csv |

    # Add new column "FileName" to distinguish the files.
    Select-Object *, @{ label = 'FileName'; expression = { $file.Name } } |
    
    # Group by ServiceCode to get a list of files per distinct value. 
    Group-Object ServiceCode |
    
    # Filter by ServiceCode values that exist only in a single file.
    # Sort-Object -Unique takes care of possible duplicates within a single file.
    Where-Object { ( $_.Group.FileName | Sort-Object -Unique ).Count -eq 1 } |
    
    # Expand the groups so we get the original object structure back.
    ForEach-Object Group |
    
    # Format-Table requires sorting by FileName, for -GroupBy.
    Sort-Object FileName |
    
    # Finally pretty-print the result.
    Format-Table -Property ServiceCode, Foo -GroupBy FileName 
    

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments