Private/Get-CorrectGuidForEntity.ps1

Function Get-CorrectGuidForEntity{
    param
    (
        [SolutionComponent]$Component,
        [hashtable]$Metadata
    )

    $targetMetadataId = ($Metadata.Target.BySchemaName.All[$component.SchemaName] | Select-Object -Property MetadataId).MetadataId

    if ($targetMetadataId -and ($targetMetadataId -ne $component.ObjectId)) {
        $component.OriginalObjectId = $component.ObjectId
        $component.ObjectId = $targetMetadataId
        Write-Verbose ("Set objectid {0} to target metadataid {1} for Entity {2}" -f $component.OriginalObjectId, $targetMetadataId, $component.SchemaName)
    }
    Write-Output $component
}