#---------------------------------------------------------------------------------------------- # _ _ _ # | | | | | | # | | ___ _ _ ___| |__ | |_ ___ _ __ # | | / _ | | | |/ __| '_ \| __/ _ | '__| # | |___| __| |_| | (__| | | | || __| | # |______\___|\__,_|\___|_| |_|\__\___|_| # _____ _______ _____ _ _ _ _____ # |_ _|__ __| / ____| | | | | (_) /\ / ____| # | | | | | (___ ___ | |_ _| |_ _ ___ _ __ ___ / \ | | __ # | | | | \___ \ / _ \| | | | | __| |/ _ \| '_ \/ __| / /\ \| | |_ | # _| |_ | | ____) | (_) | | |_| | |_| | (_) | | | \__ \ / ____ | |__| | # |_____| |_| |_____/ \___/|_|\__,_|\__|_|\___/|_| |_|___/ /_/ \_\_____| # # joel.ammann@leuchterag.ch | Winkelriedstrasse 45 | CH-6003 Luzern # #---------------------------------------------------------------------------------------------- $ErrorActionPreference = 'Stop' $IsWindowsPowerShell = $PSVersionTable.PSVersion.Major -le 5 $AppCatEnvironment = if ($env:AppCatDev -eq $true) { 'dev' } else { 'prod' } $ModuleSource = "https://lcservicecdc05workplace.blob.core.windows.net/appkatalog-appcat-$AppCatEnvironment/AppCat.zip" $MyDocumentsPath = try { [Environment]::GetFolderPath('MyDocuments') } catch { "$HOME\Documents" } $CurrentUserInstallPathWindowsPS = "$($MyDocumentsPath)\WindowsPowerShell\Modules" $CurrentUserInstallPathPwsh = "$($MyDocumentsPath)\PowerShell\Modules" $CurrentUserInstallPath = if($IsWindowsPowerShell){ $CurrentUserInstallPathWindowsPS } else { $CurrentUserInstallPathPwsh } $AllUsersInstallPath = "$($env:ProgramFiles)\WindowsPowerShell\Modules" $InstallPath = $AllUsersInstallPath $isAdmin = (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) #======================================================== # Ask user if he wants to install AppCat for the current user only #======================================================== function Get-UserConfirmation { param( $Message = 'Do you want to continue? [Y/n]' ) do { $answer = Read-Host $Message } while ('y', 'n' -notcontains ($answer.ToLower())) return $answer -eq 'y' } if (!$isAdmin) { Write-Warning 'You are not running this script as Administrator.' if (Get-UserConfirmation -Message 'Do you want to install AppCat for the current user only? [Y/n]') { $InstallPath = $CurrentUserInstallPath } else { Write-Host 'Installation aborted.' return } } #======================================================== # Check if install path is contained in $env:PSModulePath #======================================================== if (-NOT (($env:PSModulePath -split ';') -contains $InstallPath)) { Write-Warning "`$env:PSModulePath does not contain the module install path ($InstallPath)." Write-Warning '- PowerShell will not be able to find the AppCat module.' Write-Warning "- You must import the module manually or add the module install path to `$env:PSModulePath." } #======================================================== # Create PowerShell Module Directories if they don't exist #======================================================== if (!(Test-Path $InstallPath -ErrorAction SilentlyContinue)) { if (!(Test-Path (Split-Path $InstallPath) -ErrorAction SilentlyContinue)) { $null = New-Item -Path (Split-Path $InstallPath) -ItemType Directory -ErrorAction Stop } $null = New-Item -Path $InstallPath -ItemType Directory -ErrorAction Stop } #======================================================== #Download and install latest AppCat Module #======================================================== $tempFile = New-TemporaryFile $tempZip = Rename-Item -NewName "$($tempFile.name).zip" -Path $tempFile -PassThru $ProgressPreference = 'SilentlyContinue' Invoke-WebRequest -Uri $ModuleSource -OutFile $tempZip $ProgressPreference = 'Continue' Expand-Archive -Path $tempZip -DestinationPath $InstallPath -Force #======================================================== # Remove AppCat 1.0 if installed #======================================================== if ($isAdmin) { if (Test-Path "$($env:ProgramFiles)\AppKatalog") { Remove-Item -Path "$($env:ProgramFiles)\AppKatalog" -Force -Recurse -ErrorAction SilentlyContinue } } #======================================================== # Set-ExecutionPolicy to RemoteSigned if running as Admin #======================================================== if($isAdmin -and (Get-ExecutionPolicy) -eq 'Restricted'){ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force } #======================================================== # Import latest locally installed AppCat Module #======================================================== Get-Module AppCat -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1 | Import-Module -Force #======================================================== # Modify Prompt (Windows PowerShell only) #======================================================== if($IsWindowsPowerShell){ function Prompt { $(if (Test-Path variable:/PSDebugContext) { '[DBG]: ' } else { '[AppCat]: ' } ) + 'PS ' + $(Get-Location) + $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> ' } } #======================================================== # Send Telemetry to the Leuchter Cloud API #======================================================== $h = [System.Security.Cryptography.HashAlgorithm]::Create('sha256') $b64hash = [convert]::ToBase64String($h.ComputeHash([System.Text.Encoding]::UTF8.GetBytes(((Get-WmiObject -Class Win32_Bios).SerialNumber).ToLower()))) $url = 'https://api.lab.leuchterag.ch/api/v1/Telemetry?code=NbvcOxzcrWYcawVjzwUrjs7bX5bDSksLV_nQLqjmHid_AzFuSIgYEA==' $json = @{ 'serialnumber' = $b64hash 'product' = '0d4dab04-a472-42ca-aa8e-efd58818223f' 'message' = 'appcat.leuchter-cloud.ch was executed successfully' } | ConvertTo-Json try { Invoke-RestMethod -Uri $url -Body $json -ContentType 'application/json' -Method Post | Out-Null } catch { <#Do this if a terminating exception happens#> }