DacpacComparator.psm1

<#
     .SYNOPSIS
         This command will compare the 2 Dacpac Files and generate a result in XML format in either Console Window or on a Source File path
 
     .DESCRIPTION
         The Result can be generated on either Console or File. This can be decided by The options provided in ResultOuput Property
         The Result is a comparision of the XML files generated of the Dacpacs.
         The Result contains 2 sets with the Line Number and the text for which there is a mismatch
         The first set contains the Objects that are either Extra or Changed from OriginalDacpac
         The second set contains the Objects that are either Extra or Changed from ChangedDacpac
         The Results sometimes might not be straight forward, as a result when File Option is chosen , the XML's for both the Dacpacs are available on the path where Result is generated.
         This XML can now be used along with the Results XML as they can be useful to understand the differnces more clearly
         The OriginalDacpac has it's XML File as '1DacpacXML' and ChangedDacpac has its Dacpac as '2DacpacXML'
         Use "Verbose" Option to get more details about the Operation / Process performed, as Comparision might take some time
 
     .PARAMETER OriginalDacpacPath
     Accepts the path where First / Original Dacpac is placed
 
     .PARAMETER ChangedDacpacPath
     Accepts the path where Second / Changed Dacpac is placed.
 
     .PARAMETER ResultOutput
     Accepts either Console or File, to determine where result is to be shown / placed.
 
     .EXAMPLE
      Compare-Dacpac -OriginalDacpacPath 'C:\Users\Databases\AdventureWorksDev.dacpac' -ChangedDacpacPath 'C:\Users\Databases\AdventureWorksTest.dacpac' -ResultOutput Console -Verbose
      Compare-Dacpac -OriginalDacpacPath 'C:\Users\Databases\AdventureWorksDev.dacpac' -ChangedDacpacPath 'C:\Users\Databases\AdventureWorksTest.dacpac' -ResultOutput File -Verbose
 
#>

Function Compare-Dacpac
{

    Param 
    (
    [Parameter(Mandatory=$true)]
    [string] $OriginalDacpacPath ,
    [Parameter(Mandatory=$true)]
    [string] $ChangedDacpacPath,
    [Parameter(Mandatory=$true)]
    [ValidateSet('Console','File')]
    [string] $ResultOutput

    )

    $Stopwatch = [system.diagnostics.stopwatch]::StartNew()
    
    Write-Verbose 'Starting Process of Comparing Dacpac'
    
    $Counter = 1
    
    Write-Verbose 'Reading Dacpac Files'
    
    $Dacpac1Path = $OriginalDacpacPath
    $Dacpac2Path = $ChangedDacpacPath

    Write-Verbose "Time Elapsed in Seconds : $($StopWatch.Elapsed)"

    FUNCTION GenerateXML ( [string] $BacpacPATH ,  [int] $Cnt )
    {
        $CopyPath = $BacpacPATH.Substring(0, $BacpacPATH.lastIndexOf('\'))
        $CopyPathM = $CopyPath + '\' + $Cnt + 'logs.zip'
        
        Copy-Item -Path $BacpacPATH -Destination $CopyPathM -Force
        
        $UnZipnewPth = $CopyPathM.Substring(0, $CopyPathM.lastIndexOf('\'))
        $UnZipnewPth = $UnZipnewPth + '\' + $Cnt + '\logs'
        
        Expand-archive -path $CopyPathM -destinationpath $UnZipnewPth -Force
        
        $XMLAPath = $UnZipnewPth + '\model.xml'

        $XMLAPath
        
        Copy-Item -Path $XMLAPath -Destination ( $CopyPath + '/' + $Cnt + 'DacapcXML.xml' ) -Force

        $XMLA  = Get-Content -Path $XMLAPath | %{$i = 1} { new-object psobject -prop @{LineNum=$i;Text=$_}; $i++}
        Return $XMLA
    
    }
    
    $Dacpac1 = GenerateXML -BacpacPATH $OriginalDacpacPath -Cnt $Counter 
    
    $Counter = $Counter + 1 
    
    $Dacpac2 = GenerateXML -BacpacPATH $ChangedDacpacPath -Cnt $Counter
    
    Write-Verbose "Time Elapsed in Seconds : $($StopWatch.Elapsed)"
    
    $Difference = Compare-Object $Dacpac1 $Dacpac2 -Property Text -PassThru 
    
    $Result_File1 = $Difference | Where-Object -Property SideIndicator -EQ "<=" 
    $Result_File2 = $Difference | Where-Object -Property SideIndicator -EQ "=>"

    $ResultPath = $OriginalDacpacPath.Substring(0, $OriginalDacpacPath.lastIndexOf('\'))

    IF($ResultOutput -eq 'Console')
    {
        Write-Host 'Changes / Addtions in Original Dacpac'
        $Result_File1 | Select-Object LineNum,Text

        Write-Host 'Changes / Addtions in Changed Dacpac'
        $Result_File2 | Select-Object LineNum,Text
    }
    
    Else

    {

        $Result1Path = $ResultPath + '\' + 'Results_Changed_Or_Additional_Components_in_OriginalDacpac.txt'
        $Result2Path = $ResultPath + '\' + 'Results_Changed_Or_Additional_Components_in_ChangedDacpac.txt'

        $Result_File1 | Select-Object LineNum,Text | Out-File -FilePath $Result1Path
        $Result_File2 | Select-Object LineNum,Text | Out-File -FilePath $Result2Path

        Write-Verbose 'Results are generated in XML Format , However if a comparision is not clear 2 Extra XML Files of Dacpacs for Original and Changed are also created with name as 1DacpacXML and 2DacpacXML Respectively along with results on the same path as that of Original Dacpac '
        Write-Verbose "Results for Changes / Addtion in Original Dacpac is placed on : $($Result1Path)"
        Write-Verbose "Results for Changes / Addtion in Changed Dacpac is placed on : $($Result2Path)"

    }
    
    Write-Verbose 'Deleteing Excess FilesCreated'

    While($Counter -ne 0 )
    {
        Remove-Item -Path ($ResultPath + '\' + $Counter ) -Force -Recurse
        Remove-Item -Path ($ResultPath + '\' + $Counter + 'logs.zip') -Force -Recurse
    
        $Counter = $Counter - 1 
    
    }

    Write-Verbose 'Process Complete'


    Write-Verbose "Time Elapsed in Seconds : $($StopWatch.Elapsed)"
}

Export-ModuleMember -Function Compare-Dacpac