Skip to content

Instantly share code, notes, and snippets.

@joaopgrassi
Created August 17, 2018 12:39
Show Gist options
  • Save joaopgrassi/b6ca54ab7f98c765c68f625f6b629621 to your computer and use it in GitHub Desktop.
Save joaopgrassi/b6ca54ab7f98c765c68f625f6b629621 to your computer and use it in GitHub Desktop.
param (
[Parameter(Mandatory = $true)]
[string]
$compareConfigFile
)
# Gladly copied from: https://info.sapien.com/index.php/scripting/scripting-how-tos/how-to-find-installation-directory
function Get-InstallPath
{
[CmdletBinding()]
[OutputType([PSCustomObject])]
Param
(
[Parameter(Mandatory = $true)]
[SupportsWildcards()]
[string]
$ProgramName
)
$result = @()
if ($inst = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\*\Products\*\InstallProperties" -ErrorAction SilentlyContinue)
{
$inst | Where-Object {
($DisplayName = $_.getValue('DisplayName')) -like "*$ProgramName*"
} |
ForEach-Object {
$result += [PSCustomObject]@{
'DisplayName' = $displayName
'Publisher' = $_.getValue('Publisher')
'InstallPath' = $_.getValue('InstallLocation')
}
}
}
else
{
Write-Error "Cannot get the InstallProperties registry keys.";
}
if ($result)
{
return $result;
}
else
{
Write-Error "Cannot get the InstallProperties registry key for $ProgramName";
}
}
try {
if (!(Test-Path $compareConfigFile))
{
Throw "Could not find the specified xml config file: $compareConfigFile";
}
Write-Host "################ Initilizing SQL Data Compare Script #################";
Write-Host "";
Write-Host "Info: " -ForegroundColor Blue -NoNewline; Write-Host "Looking for SQL Data Compare on the machine...";
$sqlDataCompareFolderName = "SQL Data Compare";
$sqlDataCompareExecutableName = "SQLDataCompare.exe";
# Gets the installation path from registry for SQL Data Compare (the version number at the end makes impossible to hardcode)
$sqlCompareInstallPath = (Get-InstallPath -ProgramName $sqlDataCompareFolderName).InstallPath;
$fullExecutablePath = Join-Path -path $sqlCompareInstallPath -childpath $sqlDataCompareExecutableName;
# Should be there, but just as a sanity check
if (!(Test-Path "$fullExecutablePath"))
{
Throw "SQL Data Compare was not found in the machine.";
}
Write-Host "Success: " -ForegroundColor Green -NoNewline; Write-Host "SQL Data Compare found at: $fullExecutablePath";
Write-Host "";
Write-Host "Info: " -ForegroundColor Blue -NoNewline; Write-Host "Starting SQL Data Compare using the following XML config file: $compareConfigFile";
Write-Host "";
& "$fullExecutablePath" /Argfile:$compareConfigFile;
if ($LASTEXITCODE -eq 0)
{
Write-Host "Database updated successfully." -ForegroundColor Green;
}
else
{
Write-Host "Error: Database updated failed. See the above errors. " -ForegroundColor Red;
}
}
Catch {
$ErrorMessage = $_;
Write-Output $ErrorMessage;
exit 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment