I have two files being compared, one a CSV and one a txt file. The CSV file has the information for around 5,000 servers and the txt file has only server names for around 3,000. The columns within the CSV are Name, OS, and Type. This is what I did to compare the objects:
$compareObject = Compare-Object -ReferenceObject $txtFile -DifferenceObject $csvFile.Name -IncludeEqual
After this, I was then given three options. Ones that are on both lists ==
, ones that are only on the txt file =>
, and ones that are only on the csv file =<
.
What I'm trying to do is take the .SideIndicator
for values that equal ==
, and put that as a column within the $csvFile
so I can eventually do If ($csvFile.SideIndicator -eq "==")...
So basically I'm trying to figure out how to write:
If (($csvFile.Name -like $compareObject.InputObject) -and ($compareObject.InputObject -eq "==") {
(add .SideIndicator to CSV file)
}
I've tried placing a variable $count++
where I currently have add .SideIndicator...
within my script to see how many results return, and it always returns 0.
Can someone help me out with this or give me some ideas?
This may be way too complicated, but here you go:
Suppose this is your CSV file:
and this is the content of your text file:
Then this code:
produces this result:
If you want this info to be written to a new CSV file, change the
Format-Table -AutoSize
intoExport-Csv -Path 'D:\blah_updated.csv' -NoTypeInformation
You want to pipe the results of
Compare-Object
to endable the filtering ofSideIndicator
like so:Where
$fields
contain your CSV-Headers-Header Name
)-PassThru
approach,-Reference
It's as easy as this:
to get this desired output:
So both get a +1 from me.