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.22.2006.2 } 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, [string] $ClientId, [string] $ClientSecret, [switch] $Global ) if ($PSCmdlet.ShouldProcess("ShouldProcess?")) { # Critical code } if ($ClientId) { Connect-PnPOnline -Url $Url -ClientId $ClientId -ClientSecret $ClientSecret -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000 } else { Connect-PnPOnline -Url $Url -UseWebLogin -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000 } # Write-Progress -Activity ("Setting the property 'DenyAddAndCustomizePages=false' for the current site.") # Set-PnPSite -NoScriptSite $true -Url (Get-PnPSite | Select-Object Url -ExpandProperty Url) Install-APortalPnPPackage -Name Web -Global:$Global Install-APortalPnPPackage -Name Assets -Global:$Global Set-APortalUserGroup Write-Host 'APortal installed successfully!' } function Get-APortalPnPPackageVersions { Param( [Parameter(Mandatory = $true)] [ValidateSet("Web", "Assets")] [string] $Name, [switch] $Global ) $Address = "https://shuishan-tech.coding.net/p/APortal/d/APortal/git/raw/master/packages/"; if ($Global) { $Address = "https://raw.githubusercontent.com/shuishan-tech/aportal/master/packages/"; } $versionFilePath = "$($Address)$($Name.ToLower())/version.json" Invoke-RestMethod -Uri $versionFilePath | ConvertTo-Json } function Install-APortalPnPPackage { [CmdletBinding(ConfirmImpact = 'Medium')] Param( [Parameter(Mandatory = $true)] [ValidateSet("Web", "Assets")] [string] $Name, [string] $Url, [string] $ClientId, [string] $ClientSecret, [string] $Version, [switch] $Global, [switch] $Force ) Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) if ($Url) { if ($ClientId) { Connect-PnPOnline -Url $Url -ClientId $ClientId -ClientSecret $ClientSecret -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000 } else { Connect-PnPOnline -Url $Url -UseWebLogin -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000 } } $Address = "https://shuishan-tech.coding.net/p/APortal/d/APortal/git/raw/master/packages/"; if ($Global) { $Address = "https://raw.githubusercontent.com/shuishan-tech/aportal/master/packages/"; } # Get current version. $versionFilePath = "$($Address)$($Name.ToLower())/version.json" $versionFileFolderPathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name.ToLower())" $versionFilePathLocal = "$($versionFileFolderPathLocal)\version.json" if (!(Test-Path $versionFileFolderPathLocal)) { New-Item -Path $versionFileFolderPathLocal -ItemType Directory | Out-Null } Invoke-RestMethod -Uri $versionFilePath -OutFile $versionFilePathLocal -ErrorAction Stop $version = $Version if (!$version) { $versionObj = (Get-Content $versionFilePathLocal) | ConvertFrom-Json $version = $versionObj.currentVersion } $pnpFilePath = "$($Address)$($Name.ToLower())/$($Version)/aportal.$($Name.ToLower()).pnp" $pnpFileFolderPathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name.ToLower())\$($version)" $pnpFilePathLocal = "$($pnpFileFolderPathLocal)\aportal.$($Name.ToLower()).pnp" $existsLocalFile = Test-Path $pnpFilePathLocal -PathType Leaf if ($Force -or !$existsLocalFile) { if (!(Test-Path $pnpFileFolderPathLocal)) { New-Item -Path $pnpFileFolderPathLocal -ItemType Directory | Out-Null } # Download current version file and save it. Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status ("Downloading the version ({0})." -f $version) Invoke-WebRequest -Uri $pnpFilePath -OutFile $pnpFilePathLocal -ErrorAction Stop } Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) if (Test-Path $pnpFilePathLocal) { Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status "Package name: $($version)\aportal.$($Name.ToLower()).pnp" Apply-PnPProvisioningTemplate -Path $pnpFilePathLocal } 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 } } Export-ModuleMember -Function Set-APortalWeb, Set-AportalUserGroup, Install-APortalPnPPackage, Get-APortalPnPPackageVersions |