Update Install.ps1

Code optimization
This commit is contained in:
amd64fox
2021-09-28 02:26:22 +03:00
committed by GitHub
parent da4def8d47
commit ee2ceaa916

View File

@@ -239,26 +239,41 @@ If (!(Test-Path $env:USERPROFILE\Desktop\Spotify.lnk)) {
} }
# Block updates # Block updates
$ErrorActionPreference = 'SilentlyContinue' # Команда гасит легкие ошибки $ErrorActionPreference = 'SilentlyContinue' # Команда гасит легкие ошибки
$update_directory = Test-Path -Path $env:LOCALAPPDATA\Spotify $update_directory = Test-Path -Path $env:LOCALAPPDATA\Spotify
$update_directory_file = Test-Path -Path $env:LOCALAPPDATA\Spotify\Update $update_directory_file = Test-Path -Path $env:LOCALAPPDATA\Spotify\Update
$migrator_bak = Test-Path -Path $env:APPDATA\Spotify\SpotifyMigrator.bak $migrator_bak = Test-Path -Path $env:APPDATA\Spotify\SpotifyMigrator.bak
$migrator_exe = Test-Path -Path $env:APPDATA\Spotify\SpotifyMigrator.exe $migrator_exe = Test-Path -Path $env:APPDATA\Spotify\SpotifyMigrator.exe
$Check_folder_file = Get-ItemProperty -Path $env:LOCALAPPDATA\Spotify\Update | Select-Object Attributes $Check_folder_file = Get-ItemProperty -Path $env:LOCALAPPDATA\Spotify\Update | Select-Object Attributes
$folder_update_access = Get-Acl $env:LOCALAPPDATA\Spotify\Update
do {
$ch = Read-Host -Prompt "Want to block updates ? (Y/N), Unlock updates (U)" $ch = Read-Host -Prompt "Want to block updates ? (Y/N), Unlock updates (U)"
if (!($ch -eq 'n' -or $ch -eq 'y' -or $ch -eq 'u')) {
Write-Host "Oops, an incorrect value, " -ForegroundColor Red -NoNewline
Write-Host "enter again through..." -NoNewline
Start-Sleep -Milliseconds 1500
Write-Host "3" -NoNewline
Start-Sleep -Milliseconds 1000
Write-Host ".2" -NoNewline
Start-Sleep -Milliseconds 1000
Write-Host ".1"
Start-Sleep -Milliseconds 1000
Clear-Host
}
}
while ($ch -notmatch '^y$|^n$|^u$')
if ($ch -eq 'y') { if ($ch -eq 'y') {
# Если была установка клиента
# Если была установка клиенте
if (!($update_directory)) { if (!($update_directory)) {
# Создать папку Spotify в Local # Создать папку Spotify в Local
@@ -287,13 +302,22 @@ if ($ch -eq 'y') {
If ($update_directory) { If ($update_directory) {
#Удалить папку Update если она есть. #Удалить папку Update если она есть
if ($Check_folder_file -match '\bDirectory\b') { if ($Check_folder_file -match '\bDirectory\b') {
#Если у папки Update заблокированы права то разблокировать
if ($folder_update_access.AccessToString -match 'Deny') {
($ACL = Get-Acl $env:LOCALAPPDATA\Spotify\Update).access | ForEach-Object {
$Users = $_.IdentityReference
$ACL.PurgeAccessRules($Users) }
$ACL | Set-Acl $env:LOCALAPPDATA\Spotify\Update
}
Remove-item $env:LOCALAPPDATA\Spotify\Update -Recurse -Force Remove-item $env:LOCALAPPDATA\Spotify\Update -Recurse -Force
} }
#Создать файл Update если его нет #Создать файл Update если его нет
if (!($Check_folder_file -match '\bSystem\b' -and $Check_folder_file -match '\bReadOnly\b')) { if (!($Check_folder_file -match '\bSystem\b|' -and $Check_folder_file -match '\bReadOnly\b')) {
New-Item -Path $env:LOCALAPPDATA\Spotify\ -Name "Update" -ItemType "file" -Value "STOPIT" | Out-Null New-Item -Path $env:LOCALAPPDATA\Spotify\ -Name "Update" -ItemType "file" -Value "STOPIT" | Out-Null
$file = Get-ItemProperty -Path $env:LOCALAPPDATA\Spotify\Update $file = Get-ItemProperty -Path $env:LOCALAPPDATA\Spotify\Update
$file.Attributes = "ReadOnly", "System" $file.Attributes = "ReadOnly", "System"
@@ -314,7 +338,6 @@ if ($ch -eq 'y') {
Write-Host "Updates blocked successfully" -ForegroundColor Green Write-Host "Updates blocked successfully" -ForegroundColor Green
} }
@@ -325,8 +348,7 @@ if ($ch -eq 'n') {
if ($ch -eq 'u') { if ($ch -eq 'u') {
If ($migrator_bak -or $Check_folder_file -match '\bSystem\b|\bReadOnly\b') {
If ($migrator_bak -and $Check_folder_file -match '\bSystem\b' -and $Check_folder_file -match '\bReadOnly\b') {
If ($update_directory_file) { If ($update_directory_file) {
@@ -342,23 +364,36 @@ if ($ch -eq 'u') {
} }
If (!($migrator_bak -and $Check_folder_file -match '\bSystem\b' -and $Check_folder_file -match '\bReadOnly\b')) { If (!($migrator_bak -or $Check_folder_file -match '\bSystem\b|\bReadOnly\b')) {
Write-Host "Oops, updates are not blocked" Write-Host "Oops, updates are not blocked"
} }
} }
elseif (!($ch -eq 'n' -or $ch -eq 'y' -or $ch -eq 'u')) {
Write-Host "Oops, unsuccessful" -ForegroundColor Red
}
# automatic cache clearing # automatic cache clearing
do {
$ch = Read-Host -Prompt "Want to set up automatic cache cleanup? (Y/N) Delete script (U)" $ch = Read-Host -Prompt "Want to set up automatic cache cleanup? (Y/N) Delete script (U)"
if (!($ch -eq 'n' -or $ch -eq 'y' -or $ch -eq 'u')) {
Write-Host "Oops, an incorrect value, " -ForegroundColor Red -NoNewline
Write-Host "enter again through..." -NoNewline
Start-Sleep -Milliseconds 1500
Write-Host "3" -NoNewline
Start-Sleep -Milliseconds 1000
Write-Host ".2" -NoNewline
Start-Sleep -Milliseconds 1000
Write-Host ".1"
Start-Sleep -Milliseconds 1000
Clear-Host
}
}
while ($ch -notmatch '^y$|^n$|^u$')
if ($ch -eq 'y') { if ($ch -eq 'y') {
@@ -400,23 +435,30 @@ if ($ch -eq 'y') {
$Shortcut2.Save() $Shortcut2.Save()
do {
$ch = Read-Host -Prompt "Cache files that have not been used for more than XX days will be deleted. $ch = Read-Host -Prompt "Cache files that have not been used for more than XX days will be deleted.
Enter the number of days from 1 to 100" Enter the number of days from 1 to 100"
if ($ch -match "^[1-9][0-9]?$|^100$") {
$file_cache_spotify_ps1 = Get-Content $env:APPDATA\Spotify\cache-spotify.ps1 -Raw
$new_file_cache_spotify_ps1 = $file_cache_spotify_ps1 -replace 'seven', $ch -replace '-7', - $ch
Set-Content -Path $env:APPDATA\Spotify\cache-spotify.ps1 -Force -Value $new_file_cache_spotify_ps1
$contentcache_spotify_ps1 = [System.IO.File]::ReadAllText("$env:APPDATA\Spotify\cache-spotify.ps1")
$contentcache_spotify_ps1 = $contentcache_spotify_ps1.Trim()
[System.IO.File]::WriteAllText("$env:APPDATA\Spotify\cache-spotify.ps1", $contentcache_spotify_ps1)
Write-Host "Clearing the cache has been successfully installed" -ForegroundColor Green
Write-Host "installation completed" -ForegroundColor Green
exit
}
if (!($ch -match "^[1-9][0-9]?$|^100$")) { if (!($ch -match "^[1-9][0-9]?$|^100$")) {
$ch = Read-Host -Prompt "Oops, bad luck, let's try again
Cache files that have not been used for more than XX days will be deleted. Write-Host "Oops, an incorrect value, " -ForegroundColor Red -NoNewline
Enter the number of days from 1 to 100" Write-Host "enter again through..." -NoNewline
Start-Sleep -Milliseconds 1500
Write-Host "3" -NoNewline
Start-Sleep -Milliseconds 1000
Write-Host ".2" -NoNewline
Start-Sleep -Milliseconds 1000
Write-Host ".1"
Start-Sleep -Milliseconds 1000
Clear-Host
}
}
while ($ch -notmatch '^[1-9][0-9]?$|^100$')
if ($ch -match "^[1-9][0-9]?$|^100$") { if ($ch -match "^[1-9][0-9]?$|^100$") {
$file_cache_spotify_ps1 = Get-Content $env:APPDATA\Spotify\cache-spotify.ps1 -Raw $file_cache_spotify_ps1 = Get-Content $env:APPDATA\Spotify\cache-spotify.ps1 -Raw
$new_file_cache_spotify_ps1 = $file_cache_spotify_ps1 -replace 'seven', $ch -replace '-7', - $ch $new_file_cache_spotify_ps1 = $file_cache_spotify_ps1 -replace 'seven', $ch -replace '-7', - $ch
@@ -428,23 +470,16 @@ if ($ch -eq 'y') {
Write-Host "installation completed" -ForegroundColor Green Write-Host "installation completed" -ForegroundColor Green
exit exit
} }
}
else { else {
Write-Host "Unsuccessful again" -ForegroundColor Red Write-Host "Oops, default desktop folder not found, cache clearing installation stopped"
Write-Host 'Please open the cache-spotify.ps1 file in this path "AppData\Roaming\Spotify" and enter your number of days'
Write-Host "Installation completed" -ForegroundColor Green
exit exit
} }
}
} }
else {
Write-Host "Error, i can't find the 'Desktop' folder" -ForegroundColor Red
Write-Host "Installation completed" -ForegroundColor Green
Exit
}
}
if ($ch -eq 'n') { if ($ch -eq 'n') {
@@ -484,9 +519,4 @@ if ($ch -eq 'u') {
} }
} }
else {
Write-Host "Oops, unsuccessful" -ForegroundColor Red
Write-Host "installation completed" -ForegroundColor Green
}
exit exit