I’m currently working on a script that will take object names from a txt file, compare them to entries in a CSV file, ID which objects from the txt already have entries in the CSV, update existing entries, and add new ones. The CSV has four columns: “Object Name”, “Times Flagged”, “Date Last Flagged”, and “Notes”. The last one is meant for manual entry, so I only intend for the script to touch the other three.
Currently, I’m trying to figure out how to get the script to compare object names from the txt file to entries under the “Object Name” column in the CSV file. Here’s what I have as of right now:
$listFile = Get-Content "\File PathTest List.txt"
$csvFile = Import-Csv -Path '\File PathTest Report.csv'
$Now = [datetime]::Now
$exists = $false
foreach($object in $listFile)
{
$csvFile | where {$_.objectname -eq $object} | % {$exists = $true}
if($exists)
{
Write-Host $object "exists in CSV file!"
}
else
{
Write-Host $object "does not exist in CSV file!"
}
}
For testing purposes, there are 10 entries in the CSV file and 9 items in the txt file, with 4 objects existing on both. When I run the script, instead of the host saying “exists” for those four objects, it reads “does not exist” for all objects. Am I comparing the values incorrectly here, or is there something else I’m doing wrong?