Functions/New-IAWidgetObject.ps1

Function New-IAWidgetObject{
    Param(
        [Parameter(ParameterSetName='WidgetConf')]
        [Parameter(ParameterSetName='WidgetConf and Interactive')]
        $WidgetConfiguration,
        [Parameter(Mandatory = $true, ParameterSetName='WidgetConf')]
        $IAWidgetTemplate,
        [Parameter(Mandatory = $true, ParameterSetName='WidgetConf')]
        $IAGroup,
        [Parameter(Mandatory = $true, ParameterSetName='WidgetConf')]
        $IAWidgetType,
        [Parameter(ParameterSetName='WidgetConf')]
        [Guid]$IAConnectorId,
        [Parameter(ParameterSetName='Interactive')]
        [Parameter(ParameterSetName='WidgetConf and Interactive')]
        [Switch]$Interactive,
        [String]$Title,
        [String]$Subtitle,
        [Nullable[int]]$RowIndex,
        [Nullable[int]]$ColumnIndex,
        [Parameter(ParameterSetName='WidgetConf')]
        $IAWidgetConfigurationObject,
        [Nullable[int]]$BlockHeight,
        [Nullable[int]]$BlockWidth
    )

    # Create empty widget object
    $IAWidgetObject = New-Object -TypeName PSObject

    if($Interactive){

        # Get nessessary data for a widget object
        Write-Output "Select a widget template." | Out-Null
        $IAWidgetTemplate = Get-IAWidgetTemplate -All | Out-GridView -PassThru

        Write-Output "Select a group, where the widget shall be created." | Out-Null
        $IAGroup = Get-IAGroup -All | Out-GridView -PassThru

        Write-Output "Select a type for the widget." | Out-Null
        $IAWidgetType = Get-IAWidgetType -All | Out-GridView -PassThru
    }

    # Make sure only one group is taken
    $IAGroup = $IAGroup | Select-Object -First 1

    # Creating a Task Schedule
    Write-Output "Creating a Task Schedule." | Out-Null
    $IATaskScheduleObject = New-IATaskScheduleObject -Interval 60 #Default value: 60
    $IATaskSchedule = New-IATaskSchedule -IATaskSchedule $IATaskScheduleObject -Passthru

    # Getting Connector being used:
    $IAConnectorModuleType = Get-IAConnectorModuleTypes -Id $IAWidgetTemplate.ConnectorModuleTypeId
    $IAConnectorType = Get-IAConnectorTypes -Id $IAConnectorModuleType.ConnectorTypeId

    # Getting Connector
    if($PSBoundParameters.ContainsKey("IAConnectorId")) {
        $IAConnector = Get-IAConnector -Id $IAConnectorId
    }
    else {
        $IAConnector = Get-IAConnector -All | Where-Object {$_.Enabled -eq 'True' -and $_.ConnectorTypeId -eq $IAConnectorType.Id}
    }

    # Fail the script if multible connectors are chosen. Must only be one that is specified.
    if($IAConnector.Count -gt 1) {
        Write-Error "Multiple IA Connectors available. Please choose a specific Connector. $($IAConnector.Name)"
    }

    # Getting a Connector Module Id for the selected Widget Template
    Write-Output "Getting the Connector Module Id" | Out-Null
    $IAConnectorModule = Get-IAConnectorModule -All | 
                            Where-Object {$_.ConnectorModuleTypeId -like "$($IAWidgetTemplate.ConnectorModuleTypeId)" -and $_.ConnectorId -eq $IAConnector.Id} #-and $_.Enabled -eq $true
    
    if($IAConnectorModule) {
        if($IAConnectorModule.Count -gt 1){
            $IAConnectorModule = Get-IAConnectorModule -All | 
                        Where-Object {$_.ConnectorModuleTypeId -like "$($IAWidgetTemplate.ConnectorModuleTypeId)" -and $_.ConnectorId -eq $IAConnector.Id -and $_.Enabled -eq $true}
        }
        if($IAConnectorModule.Count -gt 1){
            $IAConnectorModule = $IAConnectorModule | Select-Object -First 1    
        }

        $assemblyName = $IAConnectorType.AssemblyName
        $NamespaceName = $assemblyName.Split(".")[-1]

        $IAConfigurationId = $WidgetConfiguration.Id
        $IAWidgetConfiguration = $null
        $IAWidgetConfiguration = $WidgetConfiguration
        # Remove unnessessary fields that we dont need.
        if($IAWidgetConfiguration){
            $IAWidgetConfiguration.PSObject.Properties.Remove('@odata.context')
            $IAWidgetConfiguration.PSObject.Properties.Remove('CreatedDate')
            $IAWidgetConfiguration.PSObject.Properties.Remove('Id')
            $IAWidgetConfiguration.PSObject.Properties.Remove('CreatedBy')
            $IAWidgetConfiguration.PSObject.Properties.Remove('LastUpdated')
            $IAWidgetConfiguration.PSObject.Properties.Remove('LastUpdatedBy')
            $IAWidgetConfiguration | Add-Member -MemberType NoteProperty -Name '@odata.type' -Value "#IA_Backend.Entities.DataDefinitions.$($NamespaceName).$($IAConnectorModule.Name)Configuration" -Force
        }

        # Creating a DataDefinition for the Widget
        Write-Output "Creating the DataDefinition" | Out-Null
        if($IAwidgetConfiguration){
            $IADataDefinitionObject = New-IADataDefinitionObject -IAWidgetConfiguration $IAWidgetConfiguration -IATaskScheduleId $IATaskSchedule.Id -IAConnectorModuleId $IAConnectorModule.Id
        }
        else{
            $IADataDefinitionObject = New-IADataDefinitionObject -IATaskScheduleId $IATaskSchedule.Id -IAConnectorModuleId $IAConnectorModule.Id
        }

        $IADataDefinition = New-IADataDefinition -IADataDefinitionObject $IADataDefinitionObject -Passthru

        # Set the correct ConfigurationId
        $IADataDefinition.ConfigurationId = $IAConfigurationId
        Set-IADataDefinition -IADataDefinition $IADataDefinition

        $IAWidgetDataValueFieldMaps = Get-IAWidgetDataValueFieldMaps -WidgetTemplateId $IAWidgetTemplate.Id |
            Select-Object -ExpandProperty WidgetDataValueFieldMaps

        if($IAWidgetConfigurationObject -eq $null){
            # Set the WidgetType object
            switch($IAWidgetType.Name){

                {$_ -in "Pie", 'List', 'Custom Query List'} {

                    $IAWidgetConfigurationDonutObject = New-IAWidgetConfigurationDonutObject
                    $IAWidgetConfigurationObject = New-IAWidgetConfigurationDonut -IAWidgetConfigurationDonutObject $IAWidgetConfigurationDonutObject -Passthru
                }
                {$_ -in "Bar", 'Bar List'}{
                    $IAWidgetConfigurationBasesObject = New-IAWidgetConfigurationBasesObject
                    $IAWidgetConfigurationObject = New-IAWidgetConfigurationBases -IAWidgetConfigurationBasesObject $IAWidgetConfigurationBasesObject -PassThru
                }
                {$_ -eq "Number"}{
                    $IAWidgetConfigurationNumbersObject = New-IAWidgetConfigurationNumbersObject
                    $IAWidgetConfigurationObject = New-IAWidgetConfigurationNumbers -IAWidgetConfigurationNumbersObject $IAWidgetConfigurationNumbersObject -PassThru
                }
                {$_ -eq "Cost"}{
                    $IAWidgetConfigurationCostsObject = New-IAWidgetConfigurationCostsObject
                    $IAWidgetConfigurationObject = New-IAWidgetConfigurationCosts -IAWidgetConfigurationCostsObject $IAWidgetConfigurationCostsObject -PassThru
                }
                {$_ -eq "History"} {

                    $IAWidgetConfigurationHistoriesObject = New-IAWidgetConfigurationHistoriesObject -ChartType "line" `
                        -PeriodType "Entire Period" `
                        -PeriodSlidingType "Rolling" `
                        -PeriodValue 1 `
                        -HistoryUnit "Month(s)" `
                        -PeriodFixedStartDateTime "2009-12-31T22:00:00.000Z" `
                        -PeriodFixedEndDateTime "2098-12-31T22:00:00.000Z"
               
                    $IAWidgetConfigurationObject = New-IAWidgetConfigurationHistories -IAWidgetConfigurationHistories $IAWidgetConfigurationHistoriesObject -Passthru
                }
                {$_ -eq "Donut"}{

                    $IAWidgetCenterLabelDataField = $IAWidgetDataValueFieldMaps | Where-Object -Property Default -eq $true | Select-Object -First 1

                    if($IAWidgetCenterLabelDataField.Count -eq 0){

                        $IAWidgetCenterLabelDataField = $IAWidgetDataValueFieldMaps | Select-Object -First 1
                    }

                    $IAWidgetConfigurationDonutObject = New-IAWidgetConfigurationDonutObject -CenterLabelDataFieldName $IAWidgetCenterLabelDataField.DataValueFieldName -ShowCenterSubTitle $true -ShowCenterPercentage $true
                    $IAWidgetConfigurationObject = New-IAWidgetConfigurationDonut -IAWidgetConfigurationDonutObject $IAWidgetConfigurationDonutObject -Passthru
                }
                {$_ -eq "Text"}{

                    $RawContent = New-IARawContent -IAWidgetTemplate $IAWidgetTemplate
                    $IAWidgetConfigurationTextObject = New-IAWidgetConfigurationTextObject -RawContent $RawContent
                    $IAWidgetConfigurationObject = New-IAWidgetConfigurationText -IAWidgetConfigurationTextObject $IAWidgetConfigurationTextObject -Passthru
                }
                {$_ -eq "Vulnerability"}{

                    $IAWidgetConfigurationVulnerabilityObject = New-IAWidgetConfigurationVulnerabilityObject
                    $IAWidgetConfigurationObject = New-IAWidgetConfigurationVulnerability -IAWidgetConfigurationVulnerabilitiesObject $IAWidgetConfigurationVulnerabilityObject -Passthru
                }
                {$_ -eq "Donut Clock"}{
                    $IAWidgetConfigurationClockObject = New-IAWidgetConfigurationClockObject
                    $IAWidgetConfigurationObject = New-IAWidgetConfigurationClock -IAWidgetConfigurationClockObject $IAWidgetConfigurationClockObject -Passthru
                }
            }
        }

        if([String]::IsNullOrEmpty($Title)) { $Title = $IAWidgetTemplate.Name }

        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'Title' -Value $Title
        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'ConnectorModuleTypeId' -Value $IAWidgetTemplate.ConnectorModuleTypeId
        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'Enabled' -Value $True
        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'WidgetTemplateId' -Value $IAWidgetTemplate.Id
        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'GroupId' -Value $IAGroup.Id
        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'WidgetTypeId' -Value $IAWidgetType.Id
        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'DataDefinitionId' -Value $IADataDefinition.Id
        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'WidgetConfigurationBaseId' -Value $IAWidgetConfigurationObject.Id
        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'SubTitle' -Value $Subtitle
        $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'HyperlinkUri' -Value $null

        
        if($BlockHeight -ge 0) { $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'BlockHeight' -Value $BlockHeight }
        if($BlockWidth -ge 0) { $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'BlockWidth' -Value $BlockWidth }
        if($RowIndex -ge 0) { $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'RowIndex' -Value $RowIndex }
        if($ColumnIndex -ge 0) { $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'ColumnIndex' -Value $ColumnIndex }
        else{

            $ColumnIndex = Get-IAWidget -All | 
                Where-Object -Property GroupId -eq $IAGroup.Id | 
                Select-Object -ExpandProperty ColumnIndex | 
                Sort-Object -Descending | 
                Select-Object -First 1 

            $ColumnIndex += 1
            $IAWidgetObject | Add-Member -MemberType NoteProperty -Name 'ColumnIndex' -Value $ColumnIndex
        }

        return $IAWidgetObject
    }
    else{
        Write-Warning -Message 'No such Connector Module found!'   
    }
}