Functions/New-IASoftwareUpdateWidgets.ps1

Function New-IASoftwareUpdateWidgets{
    Param(
        [Parameter(Mandatory=$true, ParameterSetName='PSObject')]
        [PSObject]$IAView,
        [Parameter(Mandatory=$true, ParameterSetName='PSObject')]
        [PSObject]$IAGroup,
        [Parameter(Mandatory=$true, ParameterSetName='Interactive')]
        [Switch]$Interactive
    )

    # Get the group and view where we are creating the widgets.
    if($Interactive) { 
        $IAView = Get-IAView -All | Out-GridView -PassThru
        $IAGroup = Get-IAGroup -All | Where-Object -Property ViewId -EQ $IAView.Id | Out-GridView -PassThru
    }

    # Get the ColumnIndexStarter of the last widget in the IAGroup to make sure our new widgets are placed next to eachother in the right order.
    Try{

        $ColumnIndexStarter = Get-IAWidget -All | 
            Where-Object -Property GroupId -eq $IAGroup.Id |
            Select-Object -ExpandProperty ColumnIndex |
            Sort-Object -Descending |
            Select-Object -First 1
    }
    Catch{}
       
    # Here we select Software Update as the Widget Template that we want to use for our widgets.
    $IAWidgetTemplate = Get-IAWidgetTemplate -Name 'Software Update'

    # Getting the Software Update Widget Data Model. This is the configuration (data model in other words) for the widget and will let us set it up properly.
    $IASoftwareUpdateConfiguration = Get-IAConfigurationDataModel -Name CTSoftwareUpdateConfiguration

    # Getting current date
    $CurrentDate = Get-Date
    
    # The date can be changed to accomodate your needs.
    #$CurrentDate = ($CurrentDate).AddMonths(-7)
    #$CurrentDate = ($CurrentDate).AddYears(-1)

    # Getting all Software Update Groups that we have in SCCM
    $SoftwareUpdateGroups = Get-IAFilter -Name 'SoftwareUpdateGroups'

    #region DonutWidget

    # Lets make the donut widget here.
    # The Donut widget will be the widget that will show us the compliance of the Software Update Group that was created two months back.
    # We can use this to get a good overview of the Software Update progress. A donut type of widget will be a great widget type for this idea.
    
    # Copying over the data model template for our first widget. We will use this to configure the widget.
    $DonutWidget = $IASoftwareUpdateConfiguration

    # Calculate the date of two months back in time.
    $DonutWidgetDate = $CurrentDate.AddMonths(-2)

    # Here we are trying to find the correct Software Update group that fits our criteria (That was added 2 months back).
    $SoftwareUpdateGroup = $SoftwareUpdateGroups | 
        Where-Object -Property Name -like "*$($DonutWidgetDate.Year)*$($DonutWidgetDate.Month)*" | Select-Object -First 1

    # If such a Software Update Group exists, we will continue on and create the widget.
    if($SoftwareUpdateGroup){
        
        # Here we choose the widget type to be donut.
        $DonutWidgetType = Get-IAWidgetType -Name Donut

        # Here we configure the widget as we would in the GUI. We input our selections into the data model.
        $DonutWidget.DataModel.SoftwareUpdateGroupId = $SoftwareUpdateGroup.Id
        $DonutWidget.DataModel.DeploymentSelectionType = 'ALL'
        $DonutWidget.DataModel.Type = 'UpdateGroup'

        # Now we create the configuration with the Data Model that we just populated with our selections
        # Please use -CleanUpEmptyProperties, if you dont plan on cleaning them up yourself. Otherwise creating the configuration might fail.
        $DonutWidgetConfiguration = New-IAConfiguration -Name $IASoftwareUpdateConfiguration.Name -IAConfigurationDataModel $DonutWidget.DataModel -Passthru -CleanUpEmptyProperties

        $ColumnIndexStarter += 1
        
        # Here we create use the New-IAWidgetObject command to create all the prerequisites for a widget and get back a Widget Object that is ready to be created.
        $DonutWidgetObject = New-IAWidgetObject -ColumnIndex $ColumnIndexStarter -Subtitle "$($DonutWidgetDate.Year) $($DonutWidgetDate.Month)" -IAGroup $IAGroup -IAWidgetTemplate $IAWidgetTemplate -IAWidgetType $DonutWidgetType -WidgetConfiguration $DonutWidgetConfiguration
        
        # Here we create the widget by sending in the $DonutWidgetObject. Using -PassThru returns us the newly created widget back from the command.
        # Always use -CreateDataValueFieldMaps, when using this command, incase you dont plan on making the DataValueFieldMaps manually by yourself.
        $DonutWidgetResult = New-IAWidget -IAWidgetObject $DonutWidgetObject -PassThru -CreateDataValueFieldMaps
    }
    else{
        # We couldnt find the correct Software Update Group
        Write-Warning -Message "The '$($DonutWidgetDate.Year)' '$($DonutWidgetDate.Month)' software update group was not found!"
    }

    #endregion DonutWidget

    # We do the same as above for the other widgets, we just change the date.
    #region HistoryMonthWidget
    ## Create history months widget
    $HistoryMonthsWidget = $IASoftwareUpdateConfiguration

    $HistoryMonthDate = $CurrentDate.AddMonths(-1)

    # Try to get last months update group
    $SoftwareUpdateGroup = $SoftwareUpdateGroups | 
        Where-Object -Property Name -like "*$($HistoryMonthDate.Year)*$($HistoryMonthDate.Month)*" | 
        Select-Object -First 1

    if($SoftwareUpdateGroup){
        
        $HistoryMonthsWidgetType = Get-IAWidgetType -Name History

        $HistoryMonthsWidget.DataModel.SoftwareUpdateGroupId = $SoftwareUpdateGroup.Id
        $HistoryMonthsWidget.DataModel.DeploymentSelectionType = 'ALL'
        $HistoryMonthsWidget.DataModel.Type = 'UpdateGroup'

        $HistoryMonthsWidgetConfiguration = New-IAConfiguration -Name $IASoftwareUpdateConfiguration.Name -IAConfigurationDataModel $HistoryMonthsWidget.DataModel -Passthru -CleanUpEmptyProperties

        $ColumnIndexStarter += 1
        $HistoryMonthsWidgetTemplate = New-IAWidgetObject -ColumnIndex $ColumnIndexStarter -Subtitle "$($HistoryMonthDate.Year) $($HistoryMonthDate.Month)" -IAGroup $IAGroup -IAWidgetTemplate $IAWidgetTemplate -IAWidgetType $HistoryMonthsWidgetType -WidgetConfiguration $HistoryMonthsWidgetConfiguration
        $HistoryMonthsWidgetResult = New-IAWidget -IAWidgetObject $HistoryMonthsWidgetTemplate -PassThru -CreateDataValueFieldMaps
    }
    else{
        # We couldnt find the correct Software Update Group
        Write-Warning -Message "The '$($HistoryMonthDate.Year)' '$($HistoryMonthDate.Month)' software update group was not found!"
    }

    #endregion HistoryMonthWidget

    #region CurrentWidget
    ## Create current widget
    $ThisMonthsWidget = $IASoftwareUpdateConfiguration

    # Try to get this months update group
    $SoftwareUpdateGroup = $SoftwareUpdateGroups | 
        Where-Object -Property Name -like "*$($CurrentDate.Year)*$($CurrentDate.Month)*" | 
        Select-Object -First 1

    if($SoftwareUpdateGroup){
        
        $ThisMonthsWidgetType = Get-IAWidgetType -Name Bar

        $ThisMonthsWidget.DataModel.SoftwareUpdateGroupId = $SoftwareUpdateGroup.Id
        $ThisMonthsWidget.DataModel.DeploymentSelectionType = 'ALL'
        $ThisMonthsWidget.DataModel.Type = 'UpdateGroup'

        $ThisMonthsWidgetConfiguration = New-IAConfiguration -Name $IASoftwareUpdateConfiguration.Name -IAConfigurationDataModel $ThisMonthsWidget.DataModel -Passthru -CleanUpEmptyProperties

        $ColumnIndexStarter += 1
        $ThisMonthsWidgetTemplate = New-IAWidgetObject -ColumnIndex $ColumnIndexStarter -Subtitle "$($CurrentDate.Year) $($CurrentDate.Month)" -IAGroup $IAGroup -IAWidgetTemplate $IAWidgetTemplate -IAWidgetType $ThisMonthsWidgetType -WidgetConfiguration $ThisMonthsWidgetConfiguration
        $ThisMonthsWidgetResult = New-IAWidget -IAWidgetObject $ThisMonthsWidgetTemplate -PassThru -CreateDataValueFieldMaps
    }
    else{
        # We couldnt find the correct Software Update Group
        Write-Warning -Message "The '$($CurrentDate.Year)' '$($CurrentDate.Month)' software update group was not found!"
    }

    #endregion CurrentWidget
}