WPEntitySiteProvisioning.ps1

<#PSScriptInfo
 
.VERSION 1.3
 
.GUID 30a0d2fe-6d28-46c4-b3c6-852f7e23e46a
 
.AUTHOR support@workpoint.com
 
.COMPANYNAME WorkPoint A/S
 
.COPYRIGHT Copyright WorkPoint A/S. All Rights reserved
 
.TAGS "WorkPoint", "SiteCollection", "API", "Site Registration"
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI https://workpoint.azureedge.net/Images/workpoint-express-icon.png
 
.EXTERNALMODULEDEPENDENCIES SharePointPnPPowerShellOnline
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>
 

#Requires -Module SharePointPnPPowerShellOnline, WorkPointPSCmdlets




<#
.SYNOPSIS
    Create and Register a SharePoint Site Collection as a WorkPoint ready entity sitecollection
 
.DESCRIPTION
This scripts creates a SharePoint Site Collection, sets it up as a WorkPoint entity sitecollection and registers it
 
.PARAMETER wpUrl
    Specify the Workpoint Solution url, e.g. "https://yourtenant.sharepoint.com/sites/workpoint"
 
.PARAMETER wpAppID
    Specify the WorkPoint Application ID, which is specified in the Tennant app catalog, e.g. "35F22BAC-99EF-4B37-B795-8FA0410AB8B1"
 
.PARAMETER newSiteUrl
    Specify the Full Url of the sitecollection to be created, e.g. "https://yourtenant.sharepoint.com/sites/workpointx12"
 
.PARAMETER newSiteTitle
    Specify the Title of the site to be created, e.g. 'My brand new Site collection'. If nothing is defined, the title will be the leaf url of the site.
     
.PARAMETER siteOwner
    Specify the Site Owner of the site to be created, e.g. 'test@yourtenant.sharepoint.com'. If nothing is defined, the owner will be Service Account User
     
.PARAMETER daemonClientID
    Specify the Daemon Client ID, e.g. "12ad387a-1c30-4577-a6b1-bdc3d5d83184"
     
.PARAMETER daemonSecret
    Specify the Daemon daemonSecret, e.g. "dasd12rghfhn'¨-"
     
.PARAMETER username
    Specify the Service Account Username for creating the site collection
     
.PARAMETER password
    Specify the Service Account Password for creating the site collection
 
.EXAMPLE
    # Create a new SharePoint site collection, set it up and Register it as a WorkPoint Site.
    WPEntitySiteProvisioning -wpUrl "https://yourtenant.sharepoint.com/sites/workpoint" -wpAppID "35F22BAC-99EF-4B37-B795-8FA0410AB8B1" -newSiteUrl https://yourtenant.sharepoint.com/sites/workpointx23D -daemonClientID "12ad387a-1c30-4577-a6b1-bdc3d5d83184" -daemonSecret "myhiddendaemonSecret" -username ulh@yourtenant.onmicrosoft.com
 
 
.NOTES
    FileName: WPEntitySiteProvisioning.ps1
    Author: WorkPoint A/S Development Team
    Contact: support@workpoint.com
    Created: 2020-10-01
    Updated: 2020-10-05
     
    Version history:
    1.0.0 - (2020-10-01) Script created.
    1.1.0 - (2020-10-05) Added BusinessModule Id requirement and added guid type to parameters
     
#>
 

[CmdletBinding(SupportsShouldProcess=$true)]
param(
    #WorkPoint Solution Url
    [parameter(Mandatory=$true, HelpMessage="Specify the WorkPoint SolutionUrl, e.g: https://yourtenant.sharepoint.com/sites/workpoint")]
    [ValidateNotNullOrEmpty()]
    [string]$wpUrl,

    #WorkPoint Businessmodule ID
    [parameter(Mandatory=$true, HelpMessage="Specify the ID of the Businessmodule, e.g: '263104BC-A519-4D35-942F-715194E005EB'")]
    [ValidateNotNullOrEmpty()]
    [guid]$wpBusinessModuleId,
    
    #WorkPoint App ID
    [parameter(Mandatory=$true, HelpMessage="Specify the WorkPoint App ID which is found in the tennent app catalog, e.g. 35F22BAC-99EF-4B37-B795-8FA0410AB8B1")]
    [ValidateNotNullOrEmpty()]
    [guid]$wpAppID,
    
    #New Full Sitecollection URL
    [parameter(Mandatory=$true, HelpMessage="Specify the full url of the site to be created, e.g. https://yourtenant.sharepoint.com/sites/workpointx1")]
    [ValidateNotNullOrEmpty()]
    [string]$newSiteUrl,
    
    #New Sitecollection title
    [parameter(Mandatory=$false, HelpMessage="Specify the Title of the site to be created, e.g. 'My brand new Site collection'. If nothing is defined, the title will be the leaf url of the site")]
    [ValidateNotNullOrEmpty()]
    [string]$newSiteTitle,
    
    #New SiteCollection Owner
    [parameter(Mandatory=$false, HelpMessage="Specify the Site Owner the SharePoint Site collection to be created. If no owner is defined, the owner will be the service account user")]
    [ValidateNotNullOrEmpty()]
    [string]$siteOwner,

    #WorkPoint Daemon App Client ID
    [parameter(Mandatory=$true, HelpMessage="Specify the WorkPoint Daemon App ID, e.g. 12ad387a-1c30-4577-a6b1-bdc3d5d83184")]
    [ValidateNotNullOrEmpty()]
    [guid]$daemonClientID,

    #WorkPoint Daemon App daemonSecret
    [parameter(Mandatory=$true, HelpMessage="Specify the daemonSecret of the Daemon app.")]
    [ValidateNotNullOrEmpty()]
    [string]$daemonSecret,
    
    #Service Account Username
    [parameter(Mandatory=$true, HelpMessage="Specify the Username of the account for creating of the SharePoint Site collection")]
    [ValidateNotNullOrEmpty()]
    [string]$username,
    
    #Service Account Password
    [parameter(Mandatory=$true, HelpMessage="Specify the Password of the account for creating of the SharePoint Site collection")]
    [ValidateNotNullOrEmpty()]
    [Security.SecureString]$password=$(Throw "Password required."),

    #Internal Testing Parameter. Do not use.
    [Parameter(Mandatory = $false, DontShow = $true)]
    [Int]$apiMode = $(4)
)
Begin {
    #$ErrorActionPreference = "Stop"
    function CreateAndRegisterSite([string]$wpUrl, [guid]$wpBusinessModuleId, [string]$newSiteTitle, [guid]$daemonClientID, [string]$daemonSecret, [string]$newSiteUrl, [string]$siteOwner, [guid]$wpAppID, [System.Management.Automation.PSCredential]$udvCred, [Int]$apiMode)
    {
        try 
        {
            if($wpUrl -eq "")
            {
                Write-Error -Message "You need to fill out all the WorkPoint solution Url in the script!"
                return;
            }
            if($daemonClientID -eq "" -or $daemonSecret -eq "" -or $tenantId -eq "")
            {
                Write-Error -Message "You need to fill out all the WorkPoint Deamon Information in the script!"
                return;
            }

            if($wpBusinessModuleId -and $wpBusinessModuleId -ne [guid]::Empty)
            {
                Connect-PnPOnline -Url $wpUrl -Credentials $udvCred -ErrorAction Stop 
                $bmList = Get-PnPList -Identity $wpBusinessModuleId -ErrorAction SilentlyContinue
                if($bmList -ne $null)
                {
                    $bmListTitle = $bmList.Title
                    Write-Output "BusinessModule List '$bmListTitle' found"
                }
                else
                {
                    Write-Error -Message "Script halted! Could not find a Business module with the ID of '$wpBusinessModuleId'"
                    return;
                }
                Disconnect-PnPOnline -ErrorAction Stop
            }
        
            $uri =[System.Uri]"$wpUrl"
            $tennantUrl = "https://"+$uri.Host
        
            Connect-PnPOnline -Url $tennantUrl -Credentials $udvCred -ErrorAction Stop 
        
            try {
                $result = Get-PnPApp -Identity $wpAppID -ErrorAction Stop
            }
            catch {
                Write-Error -Message "Cannot retreive the app with the ID $wpAppID!"
                return;
            }
        
            $tenantId = Get-PnPTenantId -TenantUrl $tennantUrl -ErrorAction Stop
        
            $daemonCred = Get-WorkPointDeamonCredential -WorkPointSolutionUrl $wpUrl -ClientID $daemonClientID -ClientSecret $daemonSecret -TenantID $tenantId -Mode $apiMode -ErrorAction Stop
        
            $result = Test-WPConnection -WorkPointCredential $daemonCred -ErrorAction SilentlyContinue
        
            if($result -ne $true)
            {
                Write-Error -Message "Script halted! Could not authenticate agianst the WorkPoint API.!"
                return;
            }

            $siteExists = Get-PnPTenantSite -Url $newSiteUrl -ErrorAction SilentlyContinue
            if($siteExists -ne $null)
            {
                throw "The site $newSiteUrl already exists."
                return;
            }

            Write-Output "Creating new Site collection $newSiteUrl"
            New-PnPTenantSite -Title $newSiteTitle -Url "$newSiteUrl" -Owner "$siteOwner" -TimeZone 3 -Template "STS#3" -Wait -ErrorAction Stop
            Write-Output ""
            Write-Output "Site $newSiteUrl created."
            Add-PnPSiteCollectionAppCatalog -Site "$newSiteUrl" -ErrorAction Stop
            Write-Output "App Catalog for the site created"

            Set-PnPTenantSite -Url $newSiteUrl -NoScriptSite:$false -ErrorAction Stop
            Write-Output "NoScript on site disabled"

            Disconnect-PnPOnline -ErrorAction Stop

            Connect-PnPOnline -Url $newSiteUrl -Credentials $udvCred -ErrorAction Stop
            $site = Get-PnPSite -Includes Id,ServerRelativeUrl -ErrorAction Stop

            Write-Output "Installing App $wpAppID"
            Install-PnPApp -Identity $wpAppID -Wait -ErrorAction Stop
            Write-Output "The app with the ID $wpAppID has been installed on the site $newSiteUrl"


            Register-WorkPointSiteCollection -WorkPointCredential $daemonCred -ServerRelativeUrl $site.ServerRelativeUrl -SiteCollectionType Entity -AssignedToBusinessModuleId $wpBusinessModuleId -ErrorAction Stop
        }
        catch
        {
            Write-Error -Message $_.Exception.Message -Exception $_.Exception
            throw $_.Exception
        }
    }
    if($newSiteTitle -eq "" -or $newSiteTitle -eq $null){
        $newSiteTitle = $newSiteUrl.Substring($newSiteUrl.LastIndexOf("/")+1)
    }
    if($siteOwner -eq "" -or $siteOwner -eq $null){
        $siteOwner = $username
    }
    
    $udvCred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
    CreateAndRegisterSite -wpUrl $wpUrl -wpBusinessModuleId $wpBusinessModuleId -newSiteTitle $newSiteTitle -daemonClientID $daemonClientID -daemonSecret $daemonSecret -newSiteUrl $newSiteUrl -siteOwner $siteOwner -wpAppID $wpAppID -udvCred $udvCred -apiMode $apiMode
}