Functions/New-IAWidget.ps1

Function New-IAWidget {
    <#
        .SYNOPSIS
            This is used to create a new Widget.
        .DESCRIPTION
            This command will take an inputted IAWidgetObject and fit it in a webrequest after which it will be sent to Insight Analytics to be created.
        .EXAMPLE
            $IACategoryObject = New-IACategoryObject -Name 'Important Folder 1'
            $IACategory = New-IACategory -IACategory $IACategoryObject -PassThru
    #>

    Param(
        [Parameter(Mandatory = $true)]
        [PSObject] $IAWidgetObject,
        [Switch]$PassThru,
        [Switch]$CreateDataValueFieldMaps
    )

    if($IAWidgetObject.Count -gt 1) { Write-Error "The IAWidgetObject can not contain more than one member. Current count: $($IAWidgetObject.Count)" }

    $Uri = "Widgets"
    $Body = $IAWidgetObject | ConvertTo-Json

    Write-Verbose "Body: $Body"

    $response = Invoke-IAQuery -QueryUrl $Uri -Method Post -Body $Body

    # Lets have this -CreateDataValueFieldMaps flag to clean up code and make it easier to manage creating FieldMaps.
    if($CreateDataValueFieldMaps -and $null -ne $response){
        $IAWidgetDataValueFieldMapsObject = New-IAWidgetDataValueFieldMapsObject -WidgetId $response.Id -WidgetTemplateId $response.WidgetTemplateId

        # To avoid problems with DataValueFieldMaps being created too quickly.
        Sleep 1

        foreach($item in $IAWidgetDataValueFieldMapsObject){
            New-IAWidgetDataValueFieldMaps -IAWidgetDataValueFieldMaps $item
        }
    }

    # Create a connector notification for the widget.
    $dataDef = Get-IADataDefinition -Filter "Id eq $($response.DataDefinitionId)"
    $conModule = $null
    $conModule = Get-IAConnectorModule -Id $dataDef.ConnectorModuleId

    if($conModule.ConnectorId){
        $connectorNotificationObject = New-IAConnectorNotificationObject -ConnectorId $conModule.ConnectorId
        $connectorNotification = New-IAConnectorNotification -IAConnectorNotification $connectorNotificationObject -PassThru
    }

    if($PassThru){

        if ($null -eq $response) {
            return $null
        }

        return $response
    }
}