Private/Get-CorrectGuidForOptionSet.ps1

Function Get-CorrectGuidForOptionSet {
    param
    (
        [SolutionComponent]$component,
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient]$SourceConn,
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient]$TargetConn
    )
    $OptionRequest = [Microsoft.Xrm.Sdk.Messages.RetrieveOptionSetRequest]::new()
    $OptionRequest.MetadataId = $Component.ObjectId
    $OptionResponse = $SourceConn.Execute($OptionRequest)
    $OptionSchemaName = $OptionResponse.OptionSetMetadata.Name
    
    try {
        $TargetOptionMetadata = Get-CrmGlobalOptionSet -Conn $TargetConn -OptionSetName $OptionSchemaName
        if ($targetOptionMetadata.MetadataId -ne $component.ObjectId){
            $component.OriginalObjectId = $component.ObjectId
            $component.ObjectId = $targetOptionMetadata.MetadataId
            Write-Verbose ("Set objectid {0} to target metadataid {1} for OptionSet {2}" -f $Component.ObjectId, $targetOptionMetadata.MetadataId, $OptionSchemaName)
        }
    }
    catch {
        $err = $_.Exception.Message
        Write-Verbose $err
    }

    Write-Output $component
}