aportal.psm1

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
$ErrorActionPreference = "Stop"
#Requires -Version 5.1
#Requires -Modules PowerShellGet
#Requires -Modules SharePointPnPPowerShellOnline

$moduleName = "SharePointPnPPowerShellOnline"

if (!(Get-command -Module $moduleName).count -gt 0)
{
    Install-Module $moduleName -Force -SkipPublisherCheck -RequiredVersion 3.13.1909.0
}
Write-Output "The modules for $moduleVersion have been installed and can now be used."
function Set-APortalWeb
{
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Medium')]
    Param(
        [Parameter(Mandatory=$true)]
        [string] $Url,
        [bool] $WithStarter,
        [bool] $Global
    )
    if ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
        # Critical code
    }

    $packagesAddress = "https://shuishan-tech.coding.net/p/APortal/d/APortal/git/raw/master/packages/";
    if ($Global) {
        $packagesAddress = "https://raw.githubusercontent.com/shuishan-tech/aportal/master/packages/";
    }
    Connect-PnPOnline $Url -UseWebLogin -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000

    Get-PnPPackage -Name "web" -Address $packagesAddress
    Get-PnPPackage -Name "assets" -Address $packagesAddress

    Install-PnPPackage -Name "web"
    Install-PnPPackage -Name "assets"

}

function Get-PnPPackage
{
    [CmdletBinding(ConfirmImpact='Medium')]
    Param(
        [Parameter(Mandatory=$true)]
        [string] $Name,
        [string] $Address
    )
    Write-Progress -Activity ("Getting APortal PnP package ({0})." -f $Name) -Status "Getting current version."
    # Get current version.
    $versionFilePath = "$($Address)$($Name)/version.json"
    $versionFilePathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name)\version.json"

    New-Item -Path $versionFilePathLocal -ItemType File -Force | Out-Null
    
    Invoke-RestMethod -Uri $versionFilePath -OutFile $versionFilePathLocal
    $versionObj = (Get-Content $versionFilePathLocal) | ConvertFrom-Json


    $pnpFilePath = "$($Address)$($Name)/$($versionObj.currentVersion)/aportal.$($Name).pnp"
    $pnpFilePathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name)\$($versionObj.currentVersion)\aportal.$($Name).pnp"

    $existsLocalFile = Test-Path $pnpFilePathLocal -PathType Leaf

    if (!$existsLocalFile) {
        New-Item -Path $pnpFilePathLocal -ItemType File -Force | Out-Null
        # Download current version file and save it.
        Write-Progress -Activity ("Getting APortal PnP package ({0})." -f $Name) -Status ("Downloading current version ({0})." -f $versionObj.currentVersion)
        Invoke-WebRequest -Uri $pnpFilePath -OutFile $pnpFilePathLocal
    }
}

function Install-PnPPackage
{
    
    [CmdletBinding(ConfirmImpact='Medium')]
    Param(
        [Parameter(Mandatory=$true)]
        [string] $Name
    )
    Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status "Getting current version."
    $versionFilePathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name)\version.json"
    if (Test-Path $versionFilePathLocal) {
        $versionObj = (Get-Content $versionFilePathLocal) | ConvertFrom-Json
        $filePath = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name)\$($versionObj.currentVersion)\aportal.$($Name).pnp"
        Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status "Package name: $($versionObj.currentVersion)\aportal.$($Name).pnp"
        Apply-PnPProvisioningTemplate -Path $filePath
    } else {
        Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status "The current version file is not exist, please run command `Get-APortalPackages` and try again."
    }
}

Export-ModuleMember -Function Set-APortalWeb, Get-APortalPackages