MSEndpointMgr / ConfigMgr

Microsoft Endpoint Configuration Manager scripts and tools
637 stars 281 forks source link

Fallback from Pilot to Production if 0 packages have been found #339

Open stephannn opened 2 years ago

stephannn commented 2 years ago

Hi, I would like to know if it is possible to change the function to a fallback scenario so that if 0 BIOS packages have been found for Pilot, it switches back to Production:

I had to change the Get-BIOSPackages function; Changed the url query and the return value part

function Get-BIOSPackages {
        param (
            [parameter(Mandatory = $true, HelpMessage = "Specify the computer details object from Get-ComputerDetails function.")]
            [ValidateNotNullOrEmpty()]
            [PSCustomObject]$InputObject
        )

        try {
            # Retrieve BIOS packages but filter out matches depending on script operational mode
            switch ($OperationalMode) {
                "Production" {
                    if ($Script:PSCmdlet.ParameterSetName -like "XMLPackage") {
                        Write-CMLogEntry -Value " - Reading XML content logic file BIOS package entries" -Severity 1
                        $Packages = (([xml]$(Get-Content -Path $XMLPackageLogicFile -Raw)).ArrayOfCMPackage).CMPackage | Where-Object {
                            $_.Name -notmatch "Pilot" -and $_.Name -notmatch "Legacy" -and $_.Name -match $Filter -and $_.Description -match $InputObject.SystemSKU
                        }
                    } else {
                        Write-CMLogEntry -Value " - Querying AdminService for BIOS package instances" -Severity 1
                        $Packages = Get-AdminServiceItem -Resource "/SMS_Package?`$filter=contains(Name,'$($Filter)') and contains(Description,'$($InputObject.SystemSKU)')" | Where-Object {
                            $_.Name -notmatch "Pilot" -and $_.Name -notmatch "Retired"
                        }
                    }

                }
                "Pilot" {
                    if ($Script:PSCmdlet.ParameterSetName -like "XMLPackage") {
                        Write-CMLogEntry -Value " - Reading XML content logic file BIOS package entries" -Severity 1
                        $Packages = (([xml]$(Get-Content -Path $XMLPackageLogicFile -Raw)).ArrayOfCMPackage).CMPackage | Where-Object {
                            $_.Name -match "Pilot" -and $_.Name -match $Filter -and $_.Description -match $InputObject.SystemSKU
                        }
                    } else {
                        Write-CMLogEntry -Value " - Querying AdminService for BIOS package instances" -Severity 1
                        $Packages = Get-AdminServiceItem -Resource "/SMS_Package?`$filter=contains(Name,'$($Filter)') and contains(Description,'$($InputObject.SystemSKU)')" | Where-Object {
                            $_.Name -match "Pilot"
                        }
                    }
                }
            }

            # Handle return value
            if ($Packages -ne $null) {
                Write-CMLogEntry -Value " - Retrieved a total of '$(($Packages | Measure-Object).Count)' BIOS packages from $($Script:PackageSource) matching operational mode: $($OperationalMode)" -Severity 1
                return $Packages
            } elseif ($Packages -eq $null -and $OperationalMode -eq "Pilot") {
                Write-CMLogEntry -Value " - Retrieved a total of '0' BIOS packages from $($Script:PackageSource) matching operational mode: $($OperationalMode)" -Severity 1
                return $Packages
            } else {
                Write-CMLogEntry -Value " - Retrieved a total of '0' BIOS packages from $($Script:PackageSource) matching operational mode: $($OperationalMode)" -Severity 3

                # Throw terminating error
                $ErrorRecord = New-TerminatingErrorRecord -Message ([string]::Empty)
                $PSCmdlet.ThrowTerminatingError($ErrorRecord)
            }
        } catch [System.Exception] {
            Write-CMLogEntry -Value " - An error occurred while calling $($Script:PackageSource) for a list of available BIOS packages. Error message: $($_.Exception.Message)" -Severity 3

            # Throw terminating error
            $ErrorRecord = New-TerminatingErrorRecord -Message ([string]::Empty)
            $PSCmdlet.ThrowTerminatingError($ErrorRecord)
        }
    }

and later called it just in the script like that:

Retrieve available BIOS packages from admin service

    $BIOSPackages = Get-BIOSPackages -InputObject $ComputerData

    if(!($BIOSPackages) -and ($OperationalMode -eq 'Pilot')){
        Write-CMLogEntry -Value "No Pilot BIOS package found, switching to Production Mode" -Severity 1
        $OperationalMode = "Production"
        $BIOSPackages = Get-BIOSPackages -InputObject $ComputerData
    }