question

det-1666 avatar image
0 Votes"
det-1666 asked LimitlessTechnology-2700 answered

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered det-1666 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

one step closer.. it also parse all file server names(duplicates as well).. is it possible to remove duplicates..

0 Votes 0 ·

That's what the last line does. Note that there are only 3 entries in the "Here is what I found." section.


201846-capture.png


0 Votes 0 ·
capture.png (131.5 KiB)

thank you very much

0 Votes 0 ·
MotoX80 avatar image
0 Votes"
MotoX80 answered MotoX80 edited

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



· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

there is no column name "server" also name is in this format ab-ns01-cd..

0 Votes 0 ·

there is no column name "server"

I have no idea what contents are in any of your csv files. If you don't share details, then I can only take an educated guess and provide an example which you can then modify to suit your needs.



also name is in this format ab-ns01-cd..


Then just look for the 2 letters ns or dash ns.

 if ($entry.server -match "ns"){     
 if ($entry.server -match "-ns"){     




0 Votes 0 ·

hi sorry for being vague. but those files have unc mapping to file servers.. so basically like this

usera P:\\ab-ns01-cd\groups\finance g:\\zd-ns10-de\public
so on and on

there is no column headers on those files. and i just need to collect unique file server names

0 Votes 0 ·
Show more comments
det-1666 avatar image
0 Votes"
det-1666 answered

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


book1.txt (266 B)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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–

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.