function Get-CMDBFallback {
    param (
        $comp
    )
    Write-Progress -Activity "CMDB Fallback $comp" -Status 'Querying Spark! for Name match' -PercentComplete 30 -ParentId 1

    ## $cmdbData = Get-LANDeskCMDBItem -name $comp
    $cmdbData = Search-ISMBO -bo cis -filter "Name eq '$comp'" -RawFilter
    
    if(!$cmdbData){
        Write-Progress -Activity "CMDB Fallback $comp" -Status 'Attempting to extract asset tag from name' -PercentComplete 40 -ParentId 1
        if($comp.Length -ge 5){
            #$asset = $comp[-5..-1] -join ""
            # Match the first set of 5 digits that don't have any digits after it until end of line
            if ($comp -match "\d{5}(?!\d)*$") { $asset = $Matches[0]}
            ## $cmdbData = Get-LANDeskCMDBItem -AssetTag $asset
            $cmdbData = Search-ISMBO -bo cis -filter "AssetTag eq '$asset'" -RawFilter
            if($cmdbData){
                $comp = $cmdbData.Name
                Write-Progress -Activity "CMDB Fallback $comp" -Status "Asset tag found, checking connection to $comp" -PercentComplete 70 -ParentId 1
                if(Test-Connection $comp){
                    Write-Progress -Activity "CMDB Fallback $comp" -Status "Rerunning get-pc with new name $comp" -PercentComplete 85 -ParentId 1
                    $getpcData = get-pc $comp
                    Write-Progress -Activity "CMDB Fallback $comp" -Completed
                    return $getpcData
                }
            }
        }
    }

    Write-Progress -Activity "CMDB Fallback $comp" -Status 'Parsing Spark! results' -PercentComplete 60 -ParentId 1
    $status = $cmdbData.Status
    if(!$status){
        $status = 'No CMDB/SCCM Record Located'
    } else{
        $status = $cmdbData.Status + ' (CMDB Data)'
    }

    $LocationConstructors = @(
        "SHS_AssetLocality",
        "ivnt_Location",
        "SHS_Floor",
        "SHS_Department",
        "SHS_LocationDetails"
    )

    $LocationData = Foreach($Loc in $LocationConstructors){

        if ($Loc -eq 'SHS_Floor'){
            $(if ($cmdbData.$Loc -match '-'){$cmdbData.$Loc.split('-')[-1] + " Floor"} else{$cmdbData.$Loc}) 
        } elseif (![string]::IsNullOrEmpty($cmdbData.$Loc)){
            $cmdbData.$Loc
        }
    }

    $LocationData = $LocationData -join ' | '

    # $locationData = $cmdbData.SHS_AssetLocality + " | " + $cmdbData.ivnt_Location + " | " + $(if ($cmdbData.SHS_Floor -match '-'){$cmdbData.SHS_Floor.split('-')[-1] + " Floor"}) + " | " + $cmdbData.SHS_Department + " | " + $cmdbData.SHS_LocationDetails
    # if($cmdbData.values._SHSLocation3.Length -gt $cmdbData.values._SHSCalcLocationString.Length){
    #     $locationData = $cmdbData.values._SHSLocation3
    # }
    # else{
    #     $locationData = $cmdbData.values._SHSCalcLocationString
    # }
    if($cmdbData.CIType -eq 'MobileDevice'){
        Write-Progress -Activity "CMDB Fallback $comp" -Status 'Querying Spark! for mobile device details' -PercentComplete 85 -ParentId 1

        $cmdbData = Get-ISMBO -bo ci__mobiledevices -RecID $cmdbData.RecId

        # $props = [Ordered]@{ 
        #     Hostname = $cmdbData.Title
        #     'MAC' = $cmdbData.values._SHSMACAddress
        #     Model = $cmdbData.values._Model + ' (' + $cmdbData.Values._SHSChasisRef + ')'
        #     'OS' = $cmdbData.Values._SHSOperatingSystem
        #     'Asset Tag' = $cmdbData.values._SHSAssetTag
        #     'Service Tag' = $cmdbData.values._SerialNumber
        #     'MDM Platform' = $cmdbData.values._SHSMDMPlatform
        #     'Phone Number' = $cmdbData.values._SHSPhoneNumber
        #     'Cellular Carrier' = $cmdbData.values._SHSCellularCarrier
        #     'Cellular Voice' = $cmdbData.values._SHSCellularVoice
        #     'Cellular Data' = $cmdbData.values._SHSCellularData
        #     'CMDB Location' = $locationData
        #  }

         $props = [Ordered]@{ 
            Hostname = $cmdbData.Name
            'MAC' = $cmdbData.MACAddress
            Model = $cmdbData.Model + ' (' + $cmdbData.ivnt_assetsubtype + ')'
            'OS' = $cmdbData.SHS_AssignedOperatingSystem  + " " + $cmdbData.OSMajorVersion
            'Asset Tag' = $cmdbData.AssetTag
            'Service Tag' = $cmdbData.SerialNumber
            'MDM Platform' = $cmdbData.SHS_MdmPlatform
            'MDM Config' = $cmdbData.SHS_ConfigProfile
            'MDM Last Seen' = $cmdbData.SHS_LastSeen
            'Phone Number' = $cmdbData.PhoneNumber
            'Cellular Carrier' = $cmdbData.SHS_Carrier
            'Cellular Voice' = $cmdbData.SHS_IsCellularVoice
            'Cellular Data' = $cmdbData.SHS_IsCellularData
            'CMDB Location' = $locationData
            'CMDB Status' = $cmdbData.Status
         }
    
         $obj = New-Object -TypeName PSObject -Property $props
    }
    else{
        # $props = [Ordered]@{ 
        #     Hostname = "$comp"
        #     Status = $status
        #     'Current User' = "Not Available"
        #     'Last User(s)' = $null  
        #     'IP | MAC' = $cmdbData.values._IPAddress + " | " + $cmdbData.values._SHSMACAddress
        #     Model = $cmdbData.values._Model + ' (' + $cmdbData.Values._SHSChasisRef + ')'
        #     'OS' = $cmdbData.Values._SHSOperatingSystem
        #     'OS Build' = $null
        #     'BIOS Ver' = $null
        #     Encryption = $null
        #     'Free Space' = $cmdbData.Values._AvailableDiskSpace + ' GB / ' + $cmdbData.Values._HardDiskSize + ' GB'
        #     RAM = $cmdbData.Values._RAM + " GB "
        #     'SSO Client' = "Not Available"
        #     'Kiosk Role' = "Not Available"
        #     'Asset Tag' = $cmdbData.values._SHSAssetTag
        #     'Service Tag' = $cmdbData.values._SerialNumber
        #     'Last Reboot' = $null
        #     Printers = $null
        #     'CMDB Location' = $locationData
        #  }

        if ($cmdbData.CIType -eq "Computer"){
            Write-Progress -Activity "CMDB Fallback $comp" -Status 'Querying Spark! for computer details' -PercentComplete 70 -ParentId 1
            $cmdbData = Get-ISMBO -BO ci__computers -recid $cmdbData.Recid
            Write-Progress -Activity "CMDB Fallback $comp" -Status 'Querying Spark! for memory details' -PercentComplete 80 -ParentId 1
            $memoryData = Search-ISMBO -bo frs_CIComponent__memorys -Filter "ParentLink_RecId eq '$($cmdbData.RecId)'" -RawFilter
            Write-Progress -Activity "CMDB Fallback $comp" -Status 'Querying Spark! for disk usage details' -PercentComplete 90 -ParentId 1
            $diskData = Search-ISMBO -bo frs_cicomponent__logicalstorages -Filter "ParentLink_RecId eq '$($cmdbdata.recid)'" -Rawfilter
            if ($memoryData){
                $MemoryTotal = ($Memorydata.Memorysize | measure-object -sum).sum
            }

        }

        $props = [Ordered]@{ 
            Hostname = $cmdbData.Name
            Status = 'Offline'
            'Current User' = "Not Available"
            'Last User(s)' = $cmdbData.LoginName
            'IP | MAC' = $cmdbData.IPAddress + " | " + $cmdbData.MACAddress
            Model = $cmdbData.Model + ' (' + $cmdbData.ivnt_assetsubtype + ')'
            'OS' = $cmdbData.OperatingSystem  + " " + $cmdbData.OSMajorVersion
            'OS Build' = $cmdbData.OSMajorVersion
            'BIOS Ver' = "$($cmdbData.BIOSVersion) ($($cmdbData.BiosDate))"
            Encryption = $cmdbData.ivnt_EncryptionState
            'Free Space' = "$($diskData.FreeSpace) GB / $($diskData.capacity) GB"
            'RAM' = "$MemoryTotal GB "
            'SSO Client' = "Not Available"
            # 'Kiosk Role' = "Not Available"
            'Asset Tag' = $cmdbData.AssetTag
            'Service Tag' = $cmdbData.SerialNumber
            'Last Reboot' = $null
            Printers = $null
            'CMDB Location' = $locationData
            'CMDB Status' = $cmdbData.Status
        }

         if ($cmdbData.SHS_IsKiosk){
            $props.add("Kiosk Role", $cmdbdata.SHS_KioskRoles)
         }
    
         $obj = New-Object -TypeName PSObject -Property $props
    }

     Write-Host "`n`nPulling cached CMDB data for $comp." -ForegroundColor Yellow
     Write-Host "Last CMDB Update: ",$cmdbData.LastModDateTime -ForegroundColor Yellow
     if($cmdbData.SHS_IsException -eq 'True'){
        ## $delInfo = Get-CMDBDELInfo $cmdbData
        $obj | Add-Member -MemberType NoteProperty -Name 'DEL Owner' -Value $cmdbData.SHS_ExceptionContact
        $obj | Add-Member -MemberType NoteProperty -Name 'DEL Vendor PC' -Value $cmdbData.SHS_IsVendorPC
        $obj | Add-Member -MemberType NoteProperty -Name 'DEL Description' -Value $cmdbData.SHS_ExceptionNotes
    }
    Write-Progress -Activity "CMDB Fallback $comp" -Completed
     return $obj
} 

function Get-CMDBData {
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $comp,

        [string]
        $asset
    )

    if($asset -match "\w \w"){
        $asset = $asset -split ' '
        $asset = $asset[0]
    }

    ## $cmdb = Get-LANDeskCMDBItem -Name $comp
    $cmdb = Search-ISMBO -bo cis -filter "Name eq '$comp'" -RawFilter
    if ($null -eq $cmdb){
        try { 
        ## $cmdb = Get-LANDeskCMDBItem -AssetTag $asset
        $cmdb = Search-ISMBO -bo cis -filter "AssetTag eq '$asset'" -RawFilter

        } catch{ $cmdb = $null }
            
    }


    return $cmdb
    
}