I am trying to enter data into one csv using a comparison of the two csvs. The data is a result of the comparison between the two csvs.
For Example:
CSV 1 says
Host,Type
Host1,Server
Host2,Switch
Host3,Hub
Host4,Router
CSV 2 says:
List of Types, Hosts
,Host1 random letters Host2
,random letters Host3 Host2
,Host4 Host3 Host2
The output should be
List of Types,Hosts
server switch,Host1 random letters Host2
hub switch,random letters Host3 Host2
router hub switch,Host4 Host3 Host2
This is what I have:
My problem seems to be the comparison part
$CSV1 = Import-Csv "Pathname1.csv"
Foreach ($title in $CSV1)
{
$Hosts =$title."Hosts"
$Type=$title."Type"
}
$csv2 = Import-Csv "Pathname2.csv"
foreach($title in $CSV2)
{
$Typelist = $title."List of Types"
$Hostlist = $title."Hosts"
if ($Hostlist -contains $Hosts)
{
$title.'List of Types'= $Type
}
}
$csv2 | export-csv "export.csv"
which outputs absolutely nothing except for what was originally there.
Your csv files really do not make much sense to me in how they are organized, but that does not mean your problem cannot be solved with some thought and learning how to break your problem down and step through the debugger to get it working.
I have whipped up some code to give the desired output, and even added a few lines of potentially helpful debug statements (output) that can be uncommented to see progress.
CSV1
Host,Type
Host1,Server
Host2,Switch
Host3,Hub
Host4,Router
CSV2
List of Types,Hosts
,Host1 random1 letters1 Host2
,random2 letters2 Host3 Host2
,Host4 Host3 Host2
Code
"-=-=-=-=-=-=-= start"
$content1 = Get-Content d:\junk\c1.csv
$data1 = $content1 | ConvertFrom-Csv -Header @('Host','Type')
"c1===="
$content1
"csv1===="
$data1 | ft
$content2 = Get-Content d:\junk\c2.csv
$data2 = $content2 | ConvertFrom-Csv -Header @('Type','Hosts')
"c2===="
$content2
"csv2===="
$data2 | ft
#"data===="
$output = @()
foreach ($hostList in ($data2.Hosts | select -Skip 1)){
# don't use variable name $host. it is a reserved variable.
# see about_Automatic_Variables
# https://technet.microsoft.com/en-us/library/hh847768.aspx
$subHosts = $hostList -split ' '
#"subhosts===="
#$subHosts
$types = @()
foreach ($subHost in $subHosts)
{
#$("sh = $subHost")
$typeMatch = (($data1 | select -Skip 1) | where {$_.Host -eq $subHost}).Type
if ($typeMatch)
{
$types += $typeMatch
}
}
$types = $types -join ' '
#"types===="
#$types
$matches = @{}
$matches.Add($types, $hostList)
$output += $matches
}
"final list===="
$output
"-=-=-=-=-=-=-=- end"
Output
-=-=-=-=-=-=-= start
c1====
Host,Type
Host1,Server
Host2,Switch
Host3,Hub
Host4,Router
csv1====
Host Type
---- ----
Host Type
Host1 Server
Host2 Switch
Host3 Hub
Host4 Router
c2====
List of Types,Hosts
,Host1 random1 letters1 Host2
,random2 letters2 Host3 Host2
,Host4 Host3 Host2
csv2====
Type Hosts
---- -----
List of Types Hosts
Host1 random1 letters1 Host2
random2 letters2 Host3 Host2
Host4 Host3 Host2
final list====
Name Value
---- -----
Server Switch Host1 random1 letters1 Host2
Hub Switch random2 letters2 Host3 Host2
Router Hub Switch Host4 Host3 Host2
-=-=-=-=-=-=-=- end