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 Test-LogetoRegistryValue($regkey, $name)
{
    Get-ItemProperty $regkey $name -ErrorAction SilentlyContinue | Out-Null
    $?
}

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
    {
        try
        {
            $doc = [Environment]::GetFolderPath("MyDocuments")
            $modulesPath = Join-Path -Path $doc -ChildPath "\WindowsPowerShell\Modules\Logeto" 
            if (Test-Path $modulesPath)
            {
                Get-ChildItem -Path $modulesPath -Filter PSGetModuleInfo.xml -Recurse -ErrorAction SilentlyContinue -Force | %{$_.Attributes="Hidden"}
            }
        }
        catch {}

        try
        {
            Install-Module -Name PackageManagement -Repository PSGallery -Force
            Install-Module -Name PowerShellGet -Repository PSGallery -Force
        }
        catch {}        

        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-Module -ListAvailable -Name "Logeto"
        if ($local.Count -gt 1)
        {
          Write-LogetoDebug "Remove old version of module." 
          $latest = Get-Module -ListAvailable -Name "Logeto" | Sort-Object -Descending -Property Version | Select-Object -First 1
          Get-InstalledModule -AllVersions -Name "Logeto" -AllowPrerelease | ? {$_.Version -notmatch $Latest.Version} | Uninstall-Module
        }                
              
        $local = Get-Module -ListAvailable -Name "Logeto" | Sort-Object -Descending -Property Version | Select-Object -First 1
        if (Get-LogetoScope)
        {
            $online = Find-Module -Name "Logeto" -AllowPrerelease 
        }
        else 
        {
            $online = Find-Module -Name "Logeto"
        }
        $onlineVersion = $online.Version -Replace "-beta", ""
        $localVersion = $local.Version -Replace "-beta", "" 
        Write-LogetoDebug "Online Powershell version: $($onlineVersion)"
        Write-LogetoDebug "Local Powershell version: $($localVersion)"
        if ([Version]$onlineVersion -gt [Version]$localVersion)        
        {
            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-Module -ListAvailable -Name "Logeto" | Sort-Object -Descending -Property Version | Select-Object -First 1
              Get-InstalledModule -AllVersions -Name "Logeto" -AllowPrerelease | ? {$_.Version -notmatch $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;