Public/New-iPilotDepartment.ps1

Function New-iPilotDepartment {
    <#
        .Synopsis
        Creates a new department in iPilot

        .Parameter DepartmentName
        Department's Name.

        .Parameter ApiVersion
        Version of the iPilot to use. Defaults to v1.

        .Parameter ApiKey
        iPilot API Key supplied by NuWave.

        .Parameter iPilotDomain
        iPilot Domain Name.

        .Parameter Credential
        Credentials for iPilot API

        .Example
        New-iPilotDepartment -DepartmentName Sales -iPilotDomain contoso.com
    #>

    Param (
        [Parameter(
            Mandatory = $true,
            HelpMessage = 'Allowed characters: Alphanumeric, spaces, and hyphens')]
            [ValidatePattern('^[a-zA-Z0-9 ]+$')]
            [System.String] $DepartmentName,
        [Switch]
            $Wait,
        [System.String]
            $ApiUrl = $global:IP_ApiUrl,
        [System.String]
            $ApiVersion = "v1",
        [System.String]
            $ApiKey,
        [System.String] 
            $iPilotDomain,
        [System.Management.Automation.PSCredential] 
            $Credential
     )

    Begin {

        # Set iPilot Domain
        if (!$global:IP_iPilotDomain -and !$iPilotDomain) {
            throw "Run Get-iPilotTeamsDomain or provide domain using -iPilotDomain"
        } elseif (!$iPilotDomain) {
            $iPilotDomain = $global:IP_iPilotDomain
            Write-Verbose "iPilot Domain: $iPilotDomain"
        } # else use passed in iPilotDomain
    }

    Process {
        # Verbose Switch
        if($PSBoundParameters.containskey("Verbose")) {
            $PreviousVerbosePreference = $VerbosePreference
            $VerbosePreference = "continue"
        }

        # Debug Switch
        if($PSBoundParameters.containskey("Debug")) {
            $PreviousDebugPreference = $DebugPreference
            $DebugPreference = "continue"
        }

        # Get/re-use OAuth Token
        $InitializeiPilotSessionSplat = @{
            ApiUrl = $ApiUrl
            ApiVersion = $ApiVersion
            ApiKey = $ApiKey
            Credential = $Credential
        }
        if ($global:IP_Instance) {
            $InitializeiPilotSessionSplat += @{
                Instance = $global:IP_Instance
            }
        }
        if ($VerbosePreference -eq "Continue") {
            $InitializeiPilotSessionSplat += @{
                Verbose = $true
            }
        }
        if ($DebugPreference -eq "Continue") {
            $InitializeiPilotSessionSplat += @{
                Debug = $true
            }
        }
        Initialize-iPilotSession @InitializeiPilotSessionSplat

        # Build iPilot API Request
        $NewiPilotDepartmentRequestUri = "$ApiUrl/$ApiVersion/msteams/$iPilotDomain/department" 
        if ($global:IP_Instance) {
            $GetiPilotDomainRequestUri += "?instance=$global:IP_Instance"
        }
        Write-Verbose "NewiPilotDepartmentRequestUri: $NewiPilotDepartmentRequestUri"

        #region Build Request Body

            Write-Verbose "Request Method: Post"

            # Add UPN and TelephoneNumber to Body
            $NewiPilotDepartmentRequestBody = @{
                departmentName = $DepartmentName
            }
        
        #endregion Build Request Body

        # Splat Invoke-RestMethod Parameters
        $NewiPilotDepartmentInvokeParams = @{
            Uri = $NewiPilotDepartmentRequestUri
            Method = "Post"
            ContentType = "application/json"
            Headers = @{
                "X-Access-Token" = $global:IP_iPilotOAuthToken.access_token
                "x-api-key"      = $global:IP_iPilotApiKey
            }
            Body = $NewiPilotDepartmentRequestUserBody | ConvertTo-Json
        }

        # Execute the REST API
        Try {
            $NewiPilotDepartmentResponse = Invoke-RestMethod @NewiPilotDepartmentInvokeParams -ErrorAction Stop
        } Catch {
            Write-Error "Failed to provision $DepartmentName`n`nURL: $($NewiPilotDepartmentRequestUri)`nBody: $($NewiPilotDepartmentRequestBody | Format-Table | Out-String)`n Error: $($_.Exception.Message)"
            break
        }

        # Return User object
        if ($NewiPilotDepartmentResponse.statuscode -eq 200) {
            Write-Output "Successfully provisioned iPilot Department.`nDepartmentName: $DepartmentName"
        } else {
            Write-Error "Failed to provision iPilot Department.`n
                DepartmentName: $DepartmentName.`n
                Status:$($NewiPilotDepartmentResponse.status)`n
                Error:$($NewiPilotDepartmentResponse.data | Format-Table | Out-String)"

        }

        # Verbose Switch
        if($PSBoundParameters.containskey("Verbose")) {
            $VerbosePreference = $PreviousVerbosePreference
        }

        # Debug Switch
        if($PSBoundParameters.containskey("Debug")) {
            $DebugPreference = $PreviousDebugPreference
        }
    }
}