param( [switch]$UpdateOnly ) try{ Unblock-File $MyInvocation.MyCommand.Path }catch{} New-Item -ItemType Directory -Force -Path "$($PSScriptRoot)\Logs" | Out-Null Start-Transcript -Path "$($PSScriptRoot)\Logs\PowerShell-Transcript.txt" #Config $DownloadUrl = "http://fastdownload.cloud4you.biz/cloud4you%20Reporting/Downloads.json" $ServiceName = "cloud4you Reporting" $Description = "Erfassung von Verbrauchsdaten zu Mietlizenzen" #EndConfig #region ConfigThings $InstalledMessage = "Service already existing. You can press: - Yes -> To update the binaries - No -> To uninstall the service - Cancel -> Do nothing and exit" $RestartMessage = "The service was unregistered. You can now delete the install folder safely. To finish the uninstall process, we recommend you to restart the machine. You can do this either now or later. Do you want to restart now?" $PlacementMessage = "Before you continue, please make sure that you've placed the installer in the designated path, where the service should be installed. Is this the case?" #endregion if ($UpdateOnly -eq $false){ Add-Type -AssemblyName System.Windows.Forms } If ($UpdateOnly -eq $true -or (Get-Service $ServiceName -ErrorAction SilentlyContinue)) { if ($UpdateOnly -eq $false){ $InstalledReturn = [System.Windows.Forms.MessageBox]::Show($InstalledMessage,"Warning",[System.Windows.Forms.MessageBoxButtons]::YesNoCancel,[System.Windows.Forms.MessageBoxIcon]::Exclamation) } if ($UpdateOnly -eq $true -or $InstalledReturn -eq "Yes"){ #Update Write-Host "Downloading current binaries" -ForegroundColor Green $TempFolderName = "TEMP" New-Item -ItemType Directory -Force -Path "$($PSScriptRoot)\$($TempFolderName)" | Out-Null $oWebclient = New-Object system.net.webclient $Downloads=$oWebclient.DownloadString($DownloadUrl) | ConvertFrom-Json $UpdateRequired = $false; foreach ($Download in $Downloads.downloads){ Invoke-WebRequest -Uri $Download.url -OutFile "$($PSScriptRoot)\$($TempFolderName)\$($Download.filename)" if ($Download.comparable -and (Test-Path -Path "$($PSScriptRoot)\$($Download.filename)")){ $NewVersion = [version](Get-Item "$($PSScriptRoot)\$($TempFolderName)\$($Download.filename)").VersionInfo.FileVersion $OldVersion = [version](Get-Item "$($PSScriptRoot)\$($Download.filename)").VersionInfo.FileVersion if ($NewVersion -gt $OldVersion){ $UpdateRequired = $true; } } elseif ((Test-Path -Path "$($PSScriptRoot)\$($Download.filename)") -ne $true){ $UpdateRequired = $true; } } if ($UpdateRequired) { Write-Host "New Version found" -ForegroundColor Green if ((Get-Service $ServiceName).Status -eq 'Running') { Write-Host "Stopping Service" -NoNewline Stop-Service $ServiceName while((Get-Service $ServiceName).Status -ne 'Stopped'){ Write-Host "." -NoNewline } Write-Host " -> Done." -ForegroundColor Green } foreach ($Download in $Downloads.downloads){ Write-Host "Replacing $($Download.filename)" -ForegroundColor Green Move-Item -Path "$($PSScriptRoot)\$($TempFolderName)\$($Download.filename)" -Destination "$($PSScriptRoot)\$($Download.filename)" -Force } Write-Host "Starting Service" -ForegroundColor Green Start-Service -Name $ServiceName } else { Write-Host "Current version is up-to-date" -ForegroundColor Green } if ($UpdateRequired -ne $true){ foreach ($Download in $Downloads.downloads){ if ($Download.comparable){ Remove-Item -Force -Path "$($PSScriptRoot)\$($TempFolderName)\$($Download.filename)" } } } if ($UpdateRequired -or (Get-ChildItem -Path "$($PSScriptRoot)\$($TempFolderName)").Length -lt 1){ Write-Host "Deleting temporary folder" -ForegroundColor Green Remove-Item -Recurse -Force -Path "$($PSScriptRoot)\$($TempFolderName)" } else { Write-Host "Uncomparable files remain in '$($PSScriptRoot)\$($TempFolderName)' for manual update." -ForegroundColor Green } } elseif ($InstalledReturn -eq "No"){ #Uninstall sc.exe delete "$($ServiceName)" $RestartReturn = [System.Windows.Forms.MessageBox]::Show($RestartMessage,"Information und recommended restart",[System.Windows.Forms.MessageBoxButtons]::YesNo,[System.Windows.Forms.MessageBoxIcon]::Exclamation) if ($RestartReturn -eq "Yes"){ Write-Host "System will restart in 30 seconds. Keep in mind to delete the install folder." -ForegroundColor Yellow shutdown -r -c "Restart after 'cloud4you Reporting'-Uninstallation" -t 30 Write-Host "To abort the restart, type 'shutdown -a'." -ForegroundColor Yellow } else { Write-Host "Script reached its end, just keep in mind to delete the install folder and restart the machine soon." -ForegroundColor Yellow } } else { Write-Host "Script was stopped by the user." -ForegroundColor Yellow } } elseif ($UpdateOnly -eq $false) { # Service was not found and has to be installed. $PlacementReturn = [System.Windows.Forms.MessageBox]::Show($PlacementMessage,"Warning",[System.Windows.Forms.MessageBoxButtons]::YesNo,[System.Windows.Forms.MessageBoxIcon]::Exclamation) if ($PlacementReturn -eq "Yes"){ Write-Host "Changing work directory to install path" -ForegroundColor Green cd $PSScriptRoot Write-Host "Downloading service binaries..." -ForegroundColor Green -NoNewline $oWebclient = New-Object system.net.webclient $Downloads=$oWebclient.DownloadString($DownloadUrl) | ConvertFrom-Json $UpdateRequired = $false; foreach ($Download in $Downloads.downloads){ Invoke-WebRequest -Uri $Download.url -OutFile "$($PSScriptRoot)\$($Download.filename)" } Write-Host " Done." -ForegroundColor Green Write-Host "Installing Service" -ForegroundColor Green New-Service -BinaryPathName "$($PSScriptRoot)\$($ServiceName).exe" -Name $ServiceName -Description $Description -StartupType Automatic Write-Host "Starting Service" -ForegroundColor Green Start-Service -Name $ServiceName } else{ Write-Host "DE: Bitte verschieben Sie zunächst dieses Installerscript in das Installationsverzeichnis des Hosts." -ForegroundColor Yellow Write-Host "EN: Please move the install script to the designated install path first." -ForegroundColor Yellow } } Stop-Transcript exit