Public/Acronis/Start-AcronisAutomatedSetup.ps1

function Start-AcronisAutomatedSetup {
    [CmdletBinding()]
    param (
        [Parameter()]
        $CloudFactoryCustomer,
        [Parameter()]
        [string]$TenantId
    )

    begin {
        # Determine if $CloudFactoryCustomer or $TenantId is provided
        if (-not $CloudFactoryCustomer -and -not $TenantId) {
            Write-Error "You must provide either CloudFactoryCustomer or TenantId."
            return
        }
        # if tenanid is provided, find the correct Customer in CloudFactory
        if ($TenantId) {
            Write-ModuleLog -Message "Finding CloudFactory Customer for TenantId: $TenantId - This could take a while...." -Level Info -Component "AcronisAutomatedSetup"
            $AllCloudFactoryCustomers = Get-CFCustomers -All
            $CloudFactoryCustomer = $AllCloudFactoryCustomers | Where-Object { $_.externalServices.MICROSOFT -eq $TenantId }

            if (-not $CloudFactoryCustomer) {
                Write-ModuleLog -Message "No CloudFactory Customer found for TenantId: $TenantId" -Level Error -Component "AcronisAutomatedSetup"
                throw "No CloudFactory Customer found for TenantId: $TenantId"
            }
            Write-ModuleLog -Message "Found CloudFactory Customer: $($CloudFactoryCustomer.name)" -Level Info -Component "AcronisAutomatedSetup"
        }

        elseif ($CloudFactoryCustomer) {
            if (-not ($CloudFactoryCustomer.getType().Name -eq 'PSCustomObject')) {
                Write-ModuleLog -Message "CloudFactoryCustomer must be a PSCustomObject" -Level Error -Component "AcronisAutomatedSetup"
                throw "CloudFactoryCustomer must be a PSCustomObject"
            }

            if (-not $CloudFactoryCustomer.name) {
                Write-ModuleLog -Message "CloudFactoryCustomer must have a name property" -Level Error -Component "AcronisAutomatedSetup"
                throw "CloudFactoryCustomer must have a name property"
            }

            Write-ModuleLog -Message "Using provided CloudFactory Customer: $($CloudFactoryCustomer.name)" -Level Info -Component "AcronisAutomatedSetup"
            $CFCheck = Get-CFCustomers -Filter $CloudFactoryCustomer.name
            if (-not $CFCheck) {
                Write-ModuleLog -Message "No CloudFactory Customer found for name: $($CloudFactoryCustomer.name)" -Level Error -Component "AcronisAutomatedSetup"
                throw "No CloudFactory Customer found for name: $($CloudFactoryCustomer.name)"
            }

            Write-ModuleLog -Message "Verified CloudFactory Customer: $($CFCheck.name)" -Level Info -Component "AcronisAutomatedSetup"
        }

    }


    process {
        if ($CloudFactoryCustomer.externalServices.ACRONIS) {
            Write-ModuleLog -Message "Acronis is already enabled for CloudFactory Customer: $($CloudFactoryCustomer.name) -Verifying existing Acronis Customer" -Level Info -Component "AcronisAutomatedSetup"
            try {
                $AcronisCustomer = Get-AcronisCustomerInfo -CustomerId $CloudFactoryCustomer.externalServices.ACRONIS
                Write-ModuleLog -Message "Acronis Customer found: $($AcronisCustomer.name)" -Level Info -Component "AcronisAutomatedSetup"
            } catch {
                Write-ModuleLog -Message "Failed to retrieve Acronis Customer information for ID: $($CloudFactoryCustomer.externalServices.ACRONIS)" -Level Error -Component "AcronisAutomatedSetup"
                throw "Failed to retrieve Acronis Customer information for ID: $($CloudFactoryCustomer.externalServices.ACRONIS)"
            }
        }

        else {
            Write-ModuleLog -Message "Acronis is not enabled for CloudFactory Customer: $($CloudFactoryCustomer.name) - Enabling Acronis" -Level Info -Component "AcronisAutomatedSetup"
            try {
                $AcronisCustomer = New-AcronisCustomer -CustomerName $CloudFactoryCustomer.name -CustomerReference $CloudFactoryCustomer.customerReference
                Write-ModuleLog -Message "Acronis Customer created: $($AcronisCustomer.name)" -Level Info -Component "AcronisAutomatedSetup"

                $AddCfExternalService = Add-CFExternalService -CustomerObject $CloudFactoryCustomer -ServiceName "ACRONIS" -Uuid $AcronisCustomer.id
                if (-not $AddCfExternalService) {
                    Write-ModuleLog -Message "Failed to add Acronis external service to CloudFactory Customer: $($CloudFactoryCustomer.name)" -Level Error -Component "AcronisAutomatedSetup"
                    throw "Failed to add Acronis external service to CloudFactory Customer: $($CloudFactoryCustomer.name)"
                }
                Write-ModuleLog -Message "Acronis Customer ID: $($AcronisCustomer.id) added to CloudFactory Customer: $($CloudFactoryCustomer.name)" -Level Info -Component "AcronisAutomatedSetup"
            } catch {
                Write-ModuleLog -Message "Failed to create Acronis Customer for CloudFactory Customer: $($CloudFactoryCustomer.name)" -Level Error -Component "AcronisAutomatedSetup"
                throw "Failed to create Acronis Customer for CloudFactory Customer: $($CloudFactoryCustomer.name)"
            }
        }

        try {
            # Enable all offerings for the customer
            Write-ModuleLog -Message "Enabling all Acronis offerings for customer: $($CloudFactoryCustomer.name)" -Level Info -Component "AcronisAutomatedSetup"
            $enabledOfferings = Enable-AcronisAllOfferings -CustomerId $AcronisCustomer.id
            if ($enabledOfferings) {
                Write-ModuleLog -Message "Successfully enabled all Acronis offerings for customer: $($AcronisCustomer.name)" -Level Info -Component "AcronisAutomatedSetup"
            } else {
                Write-ModuleLog -Message "No offerings were enabled for customer: $($AcronisCustomer.name)" -Level Warning -Component "AcronisAutomatedSetup"
            }
        }
        catch {
            Write-ModuleLog -Message "Failed to enable all Acronis offerings for customer: $($AcronisCustomer.name)" -Level Error -Component "AcronisAutomatedSetup" -ErrorRecord $_
            throw "Failed to enable all Acronis offerings for customer: $($AcronisCustomer.name)"
        }

    }

    end {

    }
}