Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stephannn opened this issue Dec 9, 2021 · 0 comments
Open

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

stephannn opened this issue Dec 9, 2021 · 0 comments

Comments

@stephannn
Copy link

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
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant