InstallLogetoModule.ps1

$RegistryFolder = "HKCU:\Software\Systemart s.r.o."
$RegistryFolderFallback = "HKLM:\Software\Systemart s.r.o."
$RegistryScopeName = 'UpdateScope'

Function Write-LogetoProgress([String] $text, [String] $code)
{
    if (Get-Command -CommandType Function -Name "Write-InstallerProgress" -ErrorAction SilentlyContinue)
    {
       Write-InstallerProgress $text $code
    } 
    else
    {
       Write-Host $text
    }
}

Function Write-LogetoDebug([String] $text)
{
    if (Get-Command -CommandType Function -Name "Write-InstallerDebug" -ErrorAction SilentlyContinue)
    {
       Write-InstallerDebug $text
    } 
    else
    {
        Write-Host $text
    }
}

Function Get-LogetoScope
{
    try
    {
        if (Test-LogetoRegistryValue $RegistryFolder $RegistryScopeName)
        {
            $scope = (Get-ItemProperty -Path $RegistryFolder -Name $RegistryScopeName)."$RegistryScopeName"
        }
        elseif (Test-LogetoRegistryValue $RegistryFolderFallback $RegistryScopeName)
        {
            $scope = (Get-ItemProperty -Path $RegistryFolderFallback -Name $RegistryScopeName)."$RegistryScopeName"
        }

        if ($scope)
        {
            $scope = $scope.ToLower();
        }

        return $scope
    }
    catch
    {
        return $null;
    }
}

Function InstallLogetoModule()
{
    try
    {
        if (!(Get-PSRepository -Name "PSGallery"))
        {
            Write-LogetoDebug "PSGallery is being registered."
            Register-PSRepository -Default -Verbose
        }

        Write-LogetoDebug "Getting NuGet package provider."
        if (!(Find-PackageProvider -Name "NuGet" -ForceBootstrap -Force))
        {
            Get-PackageProvider -Name "NuGet" -ForceBootstrap -Force -Verbose
        }

        if (Get-Module -ListAvailable -Name Logeto) 
        {
            Write-LogetoDebug "Updating Logeto Powershell Module."
      try
      {    
        $local = Get-InstalledModule -AllVersions -Name "Logeto"
        if ($local.Count -gt 1)
        {
          Write-LogetoDebug "Remove old version of module." 
          $Latest = Get-InstalledModule "Logeto" 
          Get-InstalledModule "Logeto" -AllVersions | ? {$_.Version -ne $Latest.Version} | Uninstall-Module
        }                
              
        $local = Get-InstalledModule -AllVersions -Name "Logeto"
        $online = Find-Module -Name "Logeto"
        if ($online.version -gt $local.version)
        {
            Write-LogetoDebug "Newer version was found."    
                  
            if (Get-LogetoScope)
            {
                Update-Module -Name "Logeto" -Force -AllowPrerelease -Verbose
            }
            else
            {
                Update-Module -Name "Logeto" -Force -Verbose
            }

          Write-LogetoDebug "Remove old version of module." 
          $Latest = Get-InstalledModule "Logeto" 
          Get-InstalledModule "Logeto" -AllVersions | ? {$_.Version -ne $Latest.Version} | Uninstall-Module
              }
        else
        {
          Write-LogetoDebug "No newer update for module was found."    
        }        
      }
      catch
      {
        Write-LogetoDebug "Updating module failed with reason ($_)"
        Update-Module -Name "Logeto" -Force -Verbose
      }
        } 
        else 
        {
            Write-LogetoDebug "Installing Logeto Powershell Module."

            if (Get-LogetoScope)
            {
                Install-Module -Name "Logeto" -Scope CurrentUser -Force -SkipPublisherCheck -AllowPrerelease -Verbose            
            }
            else
            {
                Install-Module -Name "Logeto" -Scope CurrentUser -Force -SkipPublisherCheck -Verbose
            }
    }
  }
    catch
    {
        Write-LogetoDebug "Logeto Powershell module could not been installed/updated ($_)"
        throw $_
    }
}

Write-LogetoProgress "Installing Logeto module" "ScriptInstallLogetoModule"
InstallLogetoModule;