diff --git a/automatic/spotify/tools/ChocolateyInstall.ps1 b/automatic/spotify/tools/ChocolateyInstall.ps1 index 4d9fd33bc13..6aa693d00a2 100644 --- a/automatic/spotify/tools/ChocolateyInstall.ps1 +++ b/automatic/spotify/tools/ChocolateyInstall.ps1 @@ -12,6 +12,24 @@ $arguments = @{ validExitCodes = @(0, 1641, 3010) } +# check we can use Get-AppxPackage +if (Get-Command 'Get-AppxPackage' -ErrorAction SilentlyContinue) { + # there is likely going to be two packages returned for x86 and x64. + # we don't care about the architecture, just the version and they will both be the same. + $allAppxPackages = Get-AppxPackage + $installedAppXPackage = @($allAppxPackages | Where-Object -Property Name -eq 'SpotifyAB.SpotifyMusic') + if ($installedAppXPackage.Count -gt 0) { + if ($env:ChocolateyForce) { + #when you remove a package, you don't remove it per architecture. You just remove it for all architectures. + Write-Warning 'Attempting to remove Spotify installed from the Microsoft Store.' + Remove-AppxPackage -Package $installedAppXPackage[0].PackageFullName + } + else { + throw "Cannot install the Spotify package because the Microsft Store version is installed. Please uninstall this manually or add the '--force' option to the command line." + } + } +} + # Download the installer $arguments['file'] = Get-ChocolateyWebFile @arguments @@ -24,10 +42,10 @@ schtasks.exe /Delete /TN $arguments['packageName'] /F # Wait for Spotify to start, then kill it $done = $false do { - if (Get-Process Spotify -ErrorAction SilentlyContinue) { - Stop-Process -name Spotify - $done = $true - } + if (Get-Process Spotify -ErrorAction SilentlyContinue) { + Stop-Process -name Spotify + $done = $true + } - Start-Sleep -s 10 + Start-Sleep -s 10 } until ($done)