aportal.psm1
$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 Get-APortalPackages -Global $Global Install-PnPPackage -Name "web" Install-PnPPackage -Name "assets" Set-APortalUserGroup Write-Host 'APortal installed successfully!' } 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." } } function Set-APortalUserGroup { [CmdletBinding(ConfirmImpact='Medium')] Param( [Parameter(Mandatory=$false)] [string] $Url ) Write-Progress -Activity ("Setting APortal user group.") if ($Url) { Connect-PnPOnline $Url -UseWebLogin -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000 } $SSUser = Get-PnPRoleDefinition -Identity 'APortalUserRole' -ErrorAction SilentlyContinue if ($SSUser) { Write-Host 'The APortalUserRole RoleDefinition already exists.' } else { Add-PnPRoleDefinition -RoleName 'APortalUserRole' -Description 'For APortal users.' -Include AddListItems, EditListItems, ViewListItems, DeleteListItems, OpenItems, ViewVersions, BrowseDirectories, ViewPages, UseRemoteAPIs, Open New-PnPGroup -Title 'APortalUserGroup' -Description 'For APortal users.' -ErrorAction SilentlyContinue | Out-Null $site = Get-PnPSite -Includes RootWeb Set-PnPGroupPermissions -Identity 'APortalUserGroup' -AddRole 'APortalUserRole' -Web $site.RootWeb } } function Get-APortalPackages { Param( [bool] $Global ) $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/"; } Get-PnPPackage -Name "web" -Address $packagesAddress Get-PnPPackage -Name "assets" -Address $packagesAddress } Export-ModuleMember -Function Set-APortalWeb, Get-APortalPackages, Set-AportalUserGroup |