ainder
February 27, 2019, 1:50pm
1
Hi everyone,
I’m trying to compare two CSV files. If something has changed, I would like to create a new file that displays the old value and the new value for “Logical_CPU_Count”.
This is how I tried to solve this problem. But somehow it does not work. I have a problem with the columns.
The other thing is that “System_Name” is shown twice. At present, the server name is shown once for the condition “<=” and once for the condition “=>”.
I’m trying to have an output with three columns.
System_Name, CPU_Count_Old, CPU_Count_New
Thanks
[pre]
#Importing CSV
$lm= Import-Csv LMCPU.csv
#Importing CSV
$tm = Import-Csv TMCPU_1.csv
#Compare both CSV files - column SamAccountName
$Results = Compare-Object $lm $tm -Property System_Name, Logical_CPU_Count -IncludeEqual
$Array = @()
Foreach($R in $Results)
{
If( $R.sideindicator -ne “==” )
{
$Object = [pscustomobject][ordered] @{
System_Name = $R.System_Name
CPU_Count_Old = {$R.sideIndicator -eq “<=”} |select $R.Logical_CPU_Count
CPU_Count_New = $R.sideindicator -eq “=>” | select-object $R.Logical_CPU_Count
}
$Array += $Object
}
}
#Count users in both files
($Array | sort-object System_Name | Select-Object * -Unique).count
#Display results in console
$Array
[/pre]
Please provide sample data
ainder
February 28, 2019, 5:42am
3
Here you go: OneDrive Link
$OldFile = '..\in\LMCPU.csv'
$NewFile = '..\in\TMCPU_1.csv'
$NewSystemList = Import-Csv $NewFile
$myOutput = Foreach($OldSystem in (Import-Csv $OldFile)) {
if ($Found = $NewSystemList | where { $_.System_Name -eq $OldSystem.System_Name }) {
if ($Found.Logical_CPU_Count -ne $OldSystem.Logical_CPU_Count) {
Write-Host "Checked system '$($OldSystem.System_Name)', Logical_CPU_Count changed from $($OldSystem.Logical_CPU_Count) to $($Found.Logical_CPU_Count)" -Fore Cyan
[PSCustomObject][ordered] @{
System_Name = $OldSystem.System_Name
CPU_Count_Old = $OldSystem.Logical_CPU_Count
CPU_Count_New = $Found.Logical_CPU_Count
}
} else {
Write-Host "Checked system '$($OldSystem.System_Name)', no change in Logical_CPU_Count" -Fore Green
}
} else {
Write-Host "Checked system '$($OldSystem.System_Name)', not found in file $((Get-Item $NewFile).FullName)" -Fore Yellow
}
}
"Old file record count: $((Import-Csv $OldFile).Count)"
"New file record count: $($NewSystemList.Count)"
$myOutput | FT -a
$file1 = Import-Csv -Path \\path\to\LMCPU.csv
$file2 = import-csv -Path \\path\to\TMCPU_1.csv
# Splatting Compare-Object parameters
$cparam = @{
ReferenceObject = $file1
DifferenceObject = $file2
Property = 'Logical_CPU_Count'
PassThru = $true
}
# Group objects via system_name
$count = $null
Compare-Object @cparam -IncludeEqual | Group-Object -Property System_Name |
ForEach-Object {
$count++
# Create custom object for groups with old and new cpu_count values
$new = IF ($_.group[0].SideIndicator -eq '=>'){$_.group[0].Logical_CPU_Count}
$old = IF ($_.group[1].SideIndicator -eq '<='){$_.group[1].Logical_CPU_Count}
If ($_.count -ge 2){
New-Object PSObject -Property @{
'System_Name' = $_.Name
'CPU_Count_Old' = $old
'CPU_Count_New' = $new
}
}
}
Write-Verbose "Total = $count" -Verbose
ainder
February 28, 2019, 4:54pm
6
@Sam Boutros Thanks a lot for this awesome solution.
@random Many thanks to you as well for your solution.
I will for sure test out both solutions.