Hi,
I am trying to compare two CSV files, if a particular VMDK exists or not, when I manually search, I am able to find a VMDK in another csv but using the below script, I it shows no match found. Please help
In first CSV file, I have VM1.vmdk as name but in second csv, it could in a folder of a datastore or directly on a datastore without a folder
Please help!!!
Example :
In CSV1 file : VM1.vmdk
In CSV2 file : [5200_Datastore1] VM1.vmdk or [5200_Datastore1] one/1463/VM1.vmdk
#import your csv files
$csv1 = Import-Csv ".\DS10_Orphaned_VMDKs_PreCheck.csv"
$csv2 = Import-Csv ".\All_VM_VMDKs_Info.csv"
#Compare both csv files (.Name and .VMDKName corresponds to your column name for your vmdk in the csv file )
$compare = Compare-Object $csv1.Name $(($csv2.VMDKpath).Split('/')[1].TrimStart('[')) $(($csv2.VMDKpath).Split("]")[0].TrimStart("[")) -IncludeEqual | ? {$_.SideIndicator -eq "=="}
if ($compare) {
foreach ($vmdk in $compare.InputObject) {
Write-Output "VMDK $vmdk exist in both csv files"
}
}
Else
{
Write-Output "No VMDK $vmdk Match Found"
}
Try the following.
Note that I use the -match operator, not the -eq operator.
#import your csv files
$csv1 = Import-Csv ".\DS10_Orphaned_VMDKs_PreCheck.csv"
$csv2 = Import-Csv ".\All_VM_VMDKs_Info.csv"
$notFound = @()
$csv1 | ForEach-Object -Process {
if ($csv2.VMDKpath -match $_.Path) {
Write-Output "VMDK $($_.Path) exists in both csv files"
}
else{
$notfound += $_
}
}
$notFound | Export-Csv -Path '.\validation.csv' -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The Compare-Object cmdlet is not really suitable to do this kind of comparison.
It is easier, imho, to use a loop and use the -match operator.
Something like this for example
#import your csv files
$csv1 = Import-Csv ".\DS10_Orphaned_VMDKs_PreCheck.csv"
$csv2 = Import-Csv ".\All_VM_VMDKs_Info.csv"
$csv1 | ForEach-Object -Process {
if($csv2.VMDKpath -match $_.Name){
Write-Output "VMDK $($_.Name) exists in both csv files"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
it is not working even now, I am getting no match found even though there is a valid VMDK in the other file
In CSV1 file Header is : Path = VM1.vmdk
In CSV2 file Header is : VMDKpath = [5200_Datastore1] VM1.vmdk or [5200_Datastore1] one/1463/VM1.vmdk
Attached the script and comparison files
If the name of column is Path, you should change the property name
#import your csv files
$csv1 = Import-Csv ".\DS10_Orphaned_VMDKs_PreCheck.csv"
$csv2 = Import-Csv ".\All_VM_VMDKs_Info.csv"
$csv1 | ForEach-Object -Process {
if($csv2.VMDKpath -match $_.Path){
Write-Output "VMDK $($_.Path) exists in both csv files"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
that worked...one last thing, how can I get the output in csv file found in one worksheet and not found in different worksheet
I tried as below, it is not working
$csv1 = Import-Csv ".\DS10_Orphaned_VMDKs_PreCheck.csv"
$csv2 = Import-Csv ".\All_VM_VMDKs_Info.csv"
$validvmdk = @()
$csv1 | ForEach-Object -Process {
#$r = $_;
if($csv2.VMDKpath -eq $_.Path){
Write-Host "VMDK $($_.Path) exists in both csv files" -Fore Green
$validvmdk = "VMDK $($_.Path) exists in both csv files"
} else {
Write-Host "VMDK $($_.Path) does not exists in both csv files" -Fore Red
$invalidvmdk = "VMDK $($_.Path) does not exists in both csv files" -Fore Red
}
}
$validvmdk | Export-Csv ".\validation.csv" -NoTypeInformation -UseCulture
Try the following.
Note that I use the -match operator, not the -eq operator.
#import your csv files
$csv1 = Import-Csv ".\DS10_Orphaned_VMDKs_PreCheck.csv"
$csv2 = Import-Csv ".\All_VM_VMDKs_Info.csv"
$notFound = @()
$csv1 | ForEach-Object -Process {
if ($csv2.VMDKpath -match $_.Path) {
Write-Output "VMDK $($_.Path) exists in both csv files"
}
else{
$notfound += $_
}
}
$notFound | Export-Csv -Path '.\validation.csv' -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked...Thank you very much 🙂