BitTitan.Runbooks.PartnerCenter.psm1

<#
.SYNOPSIS
    PowerShell module for common Partner Center functions and resources used in BitTitan Runbooks
.NOTES
    Version: 0.3.5
    Last updated: 7 November 2018
 
    Copyright (c) BitTitan, Inc. All rights reserved.
    Licensed under the MIT License.
#>


# Install/import external modules
Install-Module PartnerCenter -RequiredVersion 1.0.1809.10 -Scope CurrentUser -AllowClobber -Force
Import-Module PartnerCenter -RequiredVersion 1.0.1809.10 -Force -Global

# Install/import BitTitan.Runbooks.Modules to bootstrap the install/import of the other modules
Install-Module BitTitan.Runbooks.Modules -Scope CurrentUser -AllowClobber
Import-Module BitTitan.Runbooks.Modules -Force

# Install/import the other BitTitan.Runbooks modules
Import-BT_Module BitTitan.Runbooks.Common -Quiet
Import-BT_Module BitTitan.Runbooks.MSPComplete -Quiet

<#
.SYNOPSIS
    This function connects to Partner Center using admin account credentials or a MSPComplete Endpoint.
.DESCRIPTION
    This function connects to Partner Center using admin account credentials or a MSPComplete Endpoint.
    It returns whether the connection and logon was successful.
.PARAMETER username
    The username of the Partner Center admin account.
.PARAMETER password
    The password of the Partner Center admin account.
.PARAMETER applicationId
    The Partner Center Native App Application Id.
.PARAMETER endpoint
    The MSPComplete Endpoint for the Partner Center admin credentials.
.EXAMPLE
    Connect-PartnerCenterAdminAccount -Endpoint $Endpoint
.EXAMPLE
    $Endpoint | Connect-PartnerCenterAdminAccount
.EXAMPLE
    Connect-PartnerCenterAdminAccount -Username $username -Password $password -ApplicationId $applicationId
#>

function Connect-PartnerCenterAdminAccount {
    param (
        # The username of the Partner Center admin account.
        [Parameter(Mandatory=$true, ParameterSetName="credential")]
        [String]$username,

        # The password of the Partner Center admin account.
        [Parameter(Mandatory=$true, ParameterSetName="credential")]
        [SecureString]$password,

        # The Partner Center Native App Application Id
        [Parameter(Mandatory=$true, ParameterSetName="credential")]
        [GUID]$applicationId,

        # The MSPComplete Endpoint for the Partner Center admin credentials.
        [Parameter(Mandatory=$true, ParameterSetName="endpoint", ValueFromPipeline=$true)]
        $endpoint
    )

    # If given endpoint, retrieve credential directly
    if ($PSCmdlet.ParameterSetName -eq "endpoint") {
        $partnerCenterCredential = $endpoint | Get-CredentialFromMSPCompleteEndpoint
        $username = $partnerCenterCredential.Username

        # Check if endpoint has application ID extended property
        if (!($endpoint.ExtendedProperties) -or !(Search-HashTable -HashTable $endpoint.ExtendedProperties -Key "ApplicationId")) {
            Write-Error "Endpoint provided does not have an 'ApplicationId' extended property."
            return $false
        }
        $applicationId = $endpoint.ExtendedProperties.ApplicationId
    }

    # Create the Partner Center credential from the given username and password
    else {
        $partnerCenterCredential = New-Object System.Management.Automation.PSCredential($username, $password)
    }

    # Logon to Partner Center
    try {
        Connect-PartnerCenter -ApplicationId $applicationId -Credential $partnerCenterCredential -ErrorAction Stop

        # Logon was successful
        Write-Information "Connection and logon to Partner Center successful with username '$($username)'."
        return $true
    }

    # Error while attempting to logon
    catch {
        Write-Error "Error while attempting to logon to Partner Center account with username '$($username)'. `r`n$($_.Exception.Message)"
        return $false
    }
}