param( [Parameter(Mandatory)] [AllowEmptyCollection()] [object[]]$OldReport, [Parameter(Mandatory)] [AllowEmptyCollection()] [object[]]$NewReport ) # VMはVMIdで識別する。VM名変更でも同じVMとして扱う。 # HostOSはComputerNameで識別する。 function New-ReportIndex { param([object[]]$Rows) $index = @{} foreach ($row in $Rows) { $record = $row | Select-Object * if ($record.OwnerType -eq 'HostOS') { $record.VMName = $record.ComputerName $ownerKey = 'HostOS|' + $record.ComputerName } elseif ($record.OwnerType -eq 'VM') { if ([string]::IsNullOrWhiteSpace([string]$record.VMId)) { throw "VMIdがありません: $($record.VMName)" } $ownerKey = 'VM|' + ([guid]$record.VMId).ToString('D') } else { throw "未対応のOwnerTypeです: $($record.OwnerType)" } # MACの区切り文字・大文字小文字の違いを吸収する。 $mac = ([string]$record.MacAddress -replace '[-:\s]', '').ToUpperInvariant() # 桁数によらず、ゼロだけのMACは比較対象外。 if ($mac -match '^0+$') { continue } if ($mac -notmatch '^[0-9A-F]{12}$') { throw "MACアドレスを識別できません: $($record.ComputerName) / $($record.VMName) / $($record.NICName)" } $record.MacAddress = $mac if (-not $index.ContainsKey($ownerKey)) { $index[$ownerKey] = @{} } if ($index[$ownerKey].ContainsKey($mac)) { throw "同じ所有者内にMACアドレスの重複があります: $ownerKey / $mac" } $index[$ownerKey][$mac] = $record } return $index } function Get-ValueText { param($Value) if ($null -eq $Value) { return $null } # CSV由来の文字列と、取得時の数値・Booleanも比較できるようにする。 return (($Value | ForEach-Object { [string]$_ }) -join ',') } function New-Difference { param($Type, $Old, $New, $Property, $OldValue, $NewValue) $record = if ($null -ne $New) { $New } else { $Old } [pscustomobject]@{ ChangeType = $Type ComputerName= $record.ComputerName OwnerType = $record.OwnerType VMName = $record.VMName VMId = $record.VMId MacAddress = $record.MacAddress NICName = $record.NICName Property = $Property OldValue = $OldValue NewValue = $NewValue OldAdapter = $Old NewAdapter = $New } } $oldIndex = New-ReportIndex -Rows $OldReport $newIndex = New-ReportIndex -Rows $NewReport $ownerKeys = @(@($oldIndex.Keys) + @($newIndex.Keys) | Sort-Object -Unique) foreach ($ownerKey in $ownerKeys) { $oldExists = $oldIndex.ContainsKey($ownerKey) $newExists = $newIndex.ContainsKey($ownerKey) $oldNICs = if ($oldExists) { $oldIndex[$ownerKey] } else { @{} } $newNICs = if ($newExists) { $newIndex[$ownerKey] } else { @{} } $macs = @(@($oldNICs.Keys) + @($newNICs.Keys) | Sort-Object -Unique) foreach ($mac in $macs) { $old = $oldNICs[$mac] $new = $newNICs[$mac] if ($null -eq $old) { $type = if (-not $oldExists -and $new.OwnerType -eq 'VM') { 'VMAdded' } else { 'NICAdded' } New-Difference $type $null $new 'NICName' $null $new.NICName } elseif ($null -eq $new) { $type = if (-not $newExists -and $old.OwnerType -eq 'VM') { 'VMRemoved' } else { 'NICRemoved' } New-Difference $type $old $null 'NICName' $old.NICName $null } else { # キー以外の全プロパティを比較。新旧で追加・削除された列も対象。 $properties = @( @($old.PSObject.Properties.Name) + @($new.PSObject.Properties.Name) | Sort-Object -Unique | Where-Object { $_ -notin @('OwnerType', 'VMId', 'MacAddress') } ) foreach ($property in $properties) { $oldProperty = $old.PSObject.Properties[$property] $newProperty = $new.PSObject.Properties[$property] $oldText = Get-ValueText $oldProperty.Value $newText = Get-ValueText $newProperty.Value if (($null -eq $oldProperty) -ne ($null -eq $newProperty) -or $oldText -cne $newText) { New-Difference 'PropertyChanged' $old $new $property $oldText $newText } } } } }