ScriptBlocks/Grant-Skills-SitePermission.ps1

param(
    [Parameter(Mandatory=$true)] [ValidateSet('enable','read','disable')] [string]$Mode,

    [Parameter(Mandatory=$true)] [string]$ClientId,
    [Parameter(Mandatory=$true)] [string]$TenantId,
    [Parameter(Mandatory=$true)] [string]$Thumbprint,

    [Parameter(Mandatory=$true)] [string]$TenantHost,
    [Parameter(Mandatory=$true)] [string]$RootSitePath,
    [Parameter(Mandatory=$true)] [string]$SitePath,

    [switch]$ForceUpgrade,
    [switch]$CleanDuplicates,
    [switch]$AllowRoot
)

$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$VerbosePreference = 'SilentlyContinue'
$WarningPreference = 'SilentlyContinue'
$InformationPreference = 'SilentlyContinue'

function Get-SRxHash {
    $hash = 'nUk66RUtjK3hk1nBExx2yl%S3G#W5$ck#G#mGsLsJFZy5EgsVLgJA(mgPD2$bzQ#G7ufi7n)ylY^6^3qrZtr!dUqg!ZPcS#wBDR3b^O$kJj'
    return $hash
}

function Get-SRxM365 {
    $username = "svc_spadmin@medline.com"
    if($username.StartsWith('svc_')) { $psswrd = Get-SRxHash }
    $password = ConvertTo-SecureString $psswrd -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential($username,$password)
    return $cred
}

try {

    Import-Module Microsoft.Online.SharePoint.PowerShell -Force -ErrorAction Stop
    $tenancyURL = 'https://medline0-admin.sharepoint.com'
    $cred = Get-SRxM365
    Connect-SPOService -Url  $tenancyURL -Credential $cred
    
    $Changed = $false
    $Deleted = $false
    $Granted = $false

    $skills_config = Get-SPOTenant | Select-Object KnowledgeAgentScope, KnowledgeAgentSelectedSitesList
    $rootSiteUri = 'https://{0}{1}' -f $TenantHost, $RootSitePath 
    if ($skills_config.KnowledgeAgentSelectedSitesList -contains $rootSiteUri) { 
        $Granted = $true 
    }

    switch ($Mode) {
        'read' {
           
        }

        'enable' {
            if( -not $Granted ) { 
                Set-SPOTenant -KnowledgeAgentSelectedSitesListOperation Append -KnowledgeAgentSelectedSitesList @($rootSiteUri) 
                $skills_config = Get-SPOTenant | Select-Object KnowledgeAgentScope, KnowledgeAgentSelectedSitesList
                if ($skills_config.KnowledgeAgentSelectedSitesList -contains $rootSiteUri) { 
                    $Granted = $true 
                    $Changed = $Granted
                }
            }

        }

        'disable' {

            $isRoot = $false
            if( $RootSitePath -eq $SitePath) { $isRoot = $true }
        
            if($isRoot) {
                if( $Granted ) { 
                    Set-SPOTenant -KnowledgeAgentSelectedSitesListOperation Remove -KnowledgeAgentSelectedSitesList @($rootSiteUri) 
                    $skills_config = Get-SPOTenant | Select-Object KnowledgeAgentScope, KnowledgeAgentSelectedSitesList
                    if ($skills_config.KnowledgeAgentSelectedSitesList -contains $rootSiteUri) {
                        
                    }
                    else {
                        $Deleted = $true
                        $Granted = $false
                        $Changed = $Deleted 
                    }
                }
            }
        }
    }

    $result = @{
        Success = $true
        Data    = @{
            Mode            = $Mode
            TenantHost      = $TenantHost
            SitePath        = $SitePath
            RootSitePath    = $RootSitePath

            KnowledgeAgentScope             = $($skills_config.KnowledgeAgentScope)
            KnowledgeAgentScopeCount        = $(@($skills_config.KnowledgeAgentSelectedSitesList).Count)
            KnowledgeAgentSelectedSitesList = $Granted

            Granted         = $Granted

            Changed         = $Changed
            Deleted         = $Deleted

        }
    }
}
catch {
    $result = @{
        Success = $false
        Error   = $_.Exception.ToString()
    }
}

Write-Output ('__JSON__' + ($result | ConvertTo-Json -Compress -Depth 10))