public/New-ZendeskOrganization.ps1

function New-ZendeskOrganization {
    
<#
 
.SYNOPSIS
 
Creates a new msp client's zendesk organization.
 
.DESCRIPTION
 
Creates a new msp client's zendesk organization, with all requisite values.
 
.PARAMETER userName
Specify zendesk [api] user's username in the following format: username@domain.com/token.
 
.PARAMETER apiToken
 
Your zendesk api key.
 
.PARAMETER organizationName
 
The name of the new Zendesk organization.
 
.PARAMETER organizationDomainNames
 
Valid domain names that will be associated with the new organization.
 
.PARAMETER customerSupportEmail
 
Customer's helpdesk support email.
 
.PARAMETER AzureEntitlement
 
.PARAMETER O365Entitlement
 
.PARAMETER MBRdayofMonth
 
Calendar day when MBR should be conducted.
 
.EXAMPLE
 
New-ZendeskOrganization -userName <username> -apiToken <token> -organizationName <orgname> -organizationDomainNames <example.com> -customerSupportEmail <helpdesk@example.com> -AzureEntitlement azure_gold -O365Entitlement o365_none -MBRdayofMonth 1 -Verbose
 
#>


    [cmdletbinding()]
    
    param (
    
        [Parameter(Mandatory=$true)]
        [string]$userName,

        [Parameter(Mandatory=$true)]
        [string]$apiToken,

        [Parameter(Mandatory=$true)]
        [string]$organizationName,

        # BUG: can only accept one at the moment...need to iterate through and do a put after creating first one or something along those lines.
        [Parameter(Mandatory=$true)]
        [string]$organizationDomainNames,

        [Parameter(Mandatory=$true)]
        [string]$customerSupportEmail,

        [Parameter()]
        [ValidateSet("azure_gold","azure_silver","azure_none")]
        [string]$AzureEntitlement,

        [Parameter()]
        [ValidateSet("o365_gold","o365_none")]
        [string]$O365Entitlement,

        [Parameter()]
        [int]$MBRdayofMonth

    )

    Process 
    {
    
        $ZendeskAPI_Username = $userName
        $ZendeskAPI_Token = $apiToken


        # build http-basic auth header
        $headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($ZendeskAPI_Username):$($ZendeskAPI_Token)"));}

        
        # build body out as hash-table (remember...you can't use vars in [json] string litterals).
        $body = @{organization = @{
                    name = $organizationName
                    domain_names = $organizationDomainNames
                    organization_fields = @{
                        customer_support_email = $customerSupportEmail
                        entitlement_azure = $AzureEntitlement
                        entitlement_o365 = $O365Entitlement
                        mbr_day_of_month = 15

                    }
                    
                }
            }
        

        Try {
            $jsonbody = ConvertTo-Json $body -Compress -ErrorAction Stop -Verbose
        }
        Catch {
            $error[0].Exception
            break
        }


        Try {
            $response = Invoke-RestMethod -Uri 'https://attunix.zendesk.com/api/v2/organizations.json' -Body $jsonbody -Method Post -Headers $headers -ContentType 'application/json' -ErrorAction Stop -Verbose
        }
        Catch {
            $error[0].Exception
            break
        }

        return $response

    } # end process block

}