Public/Install-OriAzBopPrerequisityForce.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<#
.SYNOPSIS
    Install modules required from PSGallery when force is needed
.DESCRIPTION
    Install modules required from PSGallery when force is needed
 
.PARAMETER Name
    Specifies the exact names of modules to install from the online gallery. The module name must match the module name in the repository.
     
.PARAMETER RequiredVersion
    Specifies the exact version of a single module to install.
.EXAMPLE
    Install-OriAzBopPrerequisityForce
#>

function Install-OriAzBopPrerequisityForce {
    [CmdLetBinding()]
    param ()
    $ErrorActionPreference = 'Stop'
    Write-Debug "-- Install-OriAzBopPrerequisityForce --"

    # Fix credential provider
    Invoke-WebRequest -Uri "https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1" -UseBasicParsing | Invoke-Expression |Out-Null


    $InstalledPackageManagement = Get-Module -Name PackageManagement -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
    $InstalledPowershellGet = Get-Module -Name PowershellGet -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
    
    if([string]::IsNullOrEmpty($InstalledPackageManagement) -or [string]::IsNullOrEmpty($InstalledPowershellGet)) {
       Write-Error "Some version of Powershell module PackageManagement and PowershellGet is required. In this case please copy these modules manually."
    }
    
    if($InstalledPackageManagement.Version -lt [Version]::new(1,4,6) -or $InstalledPowershellGet.Version -lt [Version]::new(2,2,3)) {
      Write-Verbose "Upgrade of PackageManagement and PowershellGet is required"  
      
      # New-TemporaryDirectory
      $parent = [System.IO.Path]::GetTempPath()
      [string] $name = [System.Guid]::NewGuid()
      $tempDir = (Join-Path $parent $name)
      Write-Verbose "Temp path $tempDir "
      New-Item -ItemType Directory -Path $tempDir |Out-null
      
      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
      
      # Some kind of PowershellGet should be installed for import.
      Import-Module -Name PowerShellGet -RequiredVersion $InstalledPowershellGet.Version 
      
      # The trick in case of issues is in the download modules as packages in new directory
      Save-Package -Name PackageManagement -RequiredVersion 1.4.6 -Path $tempDir -ProviderName PowerShellGet
      Save-Package -Name PowershellGet -RequiredVersion 2.2.3 -Path $tempDir -ProviderName PowerShellGet
      
      # Extract path to current PackageManagemet
      if(![string]::IsNullOrEmpty($InstalledPackageManagement) -and ![string]::IsNullOrEmpty($InstalledPackageManagement.Version) -and ![string]::IsNullOrEmpty($InstalledPackageManagement.Path)) {
        $VersionDir = Split-Path -Path $InstalledPackageManagement.Path -Parent
        $ModuleDir = Split-Path -Path $VersionDir -Parent
        Move-Item -Path "$tempDir\PackageManagement\*" -Destination $ModuleDir
      }
      
      if(![string]::IsNullOrEmpty($InstalledPowershellGet) -and ![string]::IsNullOrEmpty($InstalledPowershellGet.Version) -and ![string]::IsNullOrEmpty($InstalledPowershellGet.Path)) {
        $VersionDir = Split-Path -Path $InstalledPowershellGet.Path -Parent
        $ModuleDir = Split-Path -Path $VersionDir -Parent
        Move-Item -Path "$tempDir\PowershellGet\*" -Destination $ModuleDir
      }
      
    }  

    Write-Debug "-- End of Install-OriAzBopPrerequisityForce --"
}