I have a list of switches in CSV and a list of data spaces where these switches are. In my list of Data Spaces, I have a DataSpace_ID field which represents its associated DataSpace_Name.
My list of switches has a Host_Name and IP_Address fields. What I want is using PowerShell and regex matching using Wildcards, I want to match the DataSpace field example, "ABC-COM" to the switch listing Host_Name which would be ABC-COM-3750-SW1. I only want to match up to ABC-COM...
Then for my result I want the output, based on the matches found, to associate the DataSpace_ID value found and include it in the output of the switch listing.
Let's say I match ABC-COM = DATASPACE_ID 1 and DEF-COM = DataSpace_ID 2, and my switch data is:
Host_Name IP_Address ABC-COM-3750-SW1 IP 192.168.1.2 ABC-COM-3750-SW2 IP: 192.168.1.3 DEF-COM-3750-SW1 IP: 192.168.3.5 DEF-COM-3750-SW2 IP: 192.168.3.6
So, in the end you would have this output from the switch listing based on comparison of the dataspace listing, except it would add the DataSpace_ID Column from the other comparison listing of data space names... Switch listing Output would look like this:
DataSpace_ID Host_Name IP_Address 1 ABC-COM-3750-SW1 IP 192.168.1.2 1 ABC-COM-3750-SW2 IP: 192.168.1.3 2 DEF-COM-3750-SW1 IP: 192.168.3.5 2 DEF-COM-3750-SW2 IP: 192.168.3.6
Here is my latest code revised based on some of your input, I am not getting errors any longer, however my output is not returning any results either.
clear-host $hash.clear() $dataSpacesExport = Import-Csv -Path .\DataSpaces_Export.csv -Header 'DataSpace_ID', 'DataSpace_Name' -Delimiter "," $accessSwitchesForExport = Import-Csv -Path .\AccessSwitchesForExport.csv -Header 'Host_Name', 'IP_Address' -Delimiter "," # create hashtable $hash = @{} # Create Regex criteria $re = [regex]".+(?=-\d+)" $dataSpacesExport | ConvertFrom-Csv | % { $hash.Add($_,”$_”) } # output $accessSwitchesForExport | ConvertFrom-Csv | Select-Object @{ n = "DataSpace_ID"; e = { $hash[$re.Match($_.Host_Name).Value] } },* | Where-Object { $_.DataSpace_ID -ne $null }
My CSV files as some have asked for, example data would be: DataSpaces and switches output examples are below in the post. DataSpaces contain a DataSpace_ID and DataSpace_Name, and switches csv contain a Host_Name and IP_Address fields.
Output, like below, based on comparison of two csv's should show: Matching DataSpace_ID with matching Host_Name, and its associated IP Address in final table.
Import-Csv
should already have done that for you (and transformed each row into a custom object). And why do you specify the parameter-Header
? Your input files seem to have a header row.Imort-Csv
will automatically determine column names from the first row in the file unless you tell it otherwise. Also, comma is the default delimiter, so-Delimiter ','
is not needed. – Ansgar Wiechers