WPBucketSiteProvisioning.ps1

<#PSScriptInfo
 
.VERSION 1.4
 
.GUID 3684EEDE-6CB4-4B7D-BEDC-AE47AA1F1798
 
.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 bucket sitecollection
 
.DESCRIPTION
This scripts creates a SharePoint Site Collection, sets it up as a WorkPoint bucket sitecollection and registers it
 
.PARAMETER wpUrl
    Specify the Workpoint Solution url, e.g. "https://yourtenant.sharepoint.com/sites/workpoint"
     
.PARAMETER wpBusinessModuleId
    Specify the Workpoint Businessmodule Id, e.g. "263104BC-A519-4D35-942F-715194E005EB"
 
.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.
    WPBucketSiteProvisioning -wpUrl "https://yourtenant.sharepoint.com/sites/workpoint" -wpBusinessModuleId "6d34ceb2-0d93-4352-89bc-a705015a921b" -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: WPBucketSiteProvisioning.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-01) 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'")]
    [string]$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,[string]$wpBusinessModuleId, [string]$newSiteTitle, [string]$daemonClientID, [string]$daemonSecret, [string]$newSiteUrl, [string]$siteOwner, [string]$wpAppID, [System.Management.Automation.PSCredential]$udvCred, [Int]$apiMode)
    {
       try
       {
               if($wpBusinessModuleId -eq "." -or $wpBusinessModuleId -eq ""-or $wpBusinessModuleId -eq $null) 
            {
                $wpBusinessModuleId = [guid]::Empty;
            }
            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;
            }
        
            $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 Bucket -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
}