BitTitan.Runbooks.AzureRMS.psm1

<#
.SYNOPSIS
    Powershell module for common Azure RMS functions and resources used in BitTitan Runbooks
.NOTES
    Version: 0.1.1
    Last updated: 10 September 2018
 
    Copyright (c) BitTitan, Inc. All rights reserved.
    Licensed under the MIT License.
#>


<#
.SYNOPSIS
    This function connects to Azure RMS using admin account credentials or a MSPComplete Endpoint.
.DESCRIPTION
    This function connects to Azure RMS using admin account credentials or a MSPComplete Endpoint.
    It returns whether the connection and logon was successful.
.PARAMETER username
    The username of the Azure RMS admin account.
.PARAMETER password
    The password of the Azure RMS admin account.
.PARAMETER endpoint
    The MSPComplete Endpoint for the Azure RMS admin credentials.
    This endpoint can be masked or unmasked.
.EXAMPLE
    Connect-AzureRmsAdminAccount -Endpoint $Endpoint
.EXAMPLE
    $Endpoint | Connect-AzureRmsAdminAccount
.EXAMPLE
    Connect-AzureRmsAdminAccount -Username $username -Password $password
#>

function Connect-AzureRmsAdminAccount {
    param (
        # The username of the Azure RMS account.
        [Parameter(Mandatory=$true, ParameterSetName="credential")]
        [String]$username,

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

        # The MSPComplete Endpoint.
        [Parameter(Mandatory=$true, ParameterSetName="endpoint", ValueFromPipeline=$true)]
        $endpoint
    )

    # If given endpoint, retrieve credential directly
    if ($PSCmdlet.ParameterSetName -eq "endpoint") {
        $aadrmCredential = $endpoint | Get-UnmaskedMSPCompleteEndpoint `
            | Get-CredentialFromUnmaskedMSPCompleteEndpoint
        $username = $endpoint.Configuration.AdministrativeUserName
    }
    # Create the Azure RMS credential from the given username and password
    else {
        $aadrmCredential = New-Object System.Management.Automation.PSCredential($username, $password)
    }

    # Logon to Azure RMS
    try {
        Connect-AadrmService -Credential $aadrmCredential -ErrorAction Stop

        # Logon was successful
        Write-Information "Connection and logon to Azure RMS successful with username '$($username)'."
        return $true
    }
    catch {
        # Logon was unsuccessful
        Write-Error "Failed Azure RMS account login with username '$($username)'. $($_.Exception.Message)"
        return $false
    }
}