#CONST
$SITENAME="100"
$SCCMSERVER="shscm01.int.samhealth.net"
$SCCMNAMESPACE="root\sms\site_100"

Function Get-SCCMQuery {
    param (
        [string]$comp
    )

    if(!(Test-Connection -ComputerName $comp -Count 1)){
                
        $compStatus = "Offline"
    }
    else{
        $compStatus = "Online"
    }

    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Checking if disabled in AD' -PercentComplete 5 -ParentId 1
    if(get-module -ListAvailable -Name 'ActiveDirectory'){
        try { $adTest = ((Get-ADComputer $comp).DistinguishedName -match "Disabled Computers") } catch { $adTest = $true }
            if($adTest){
                $compStatus += " (disabled or off the domain)"
            }
    }

    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Last Hardware Scan' -PercentComplete 10 -ParentId 1
    $FindLastHardwareScanSCCM = Get-SCCMLastHardwareScan $comp
    if(!$FindLastHardwareScanSCCM){
        $props = [Ordered]@{ 
            Hostname = "$comp"
            Status = "$compStatus"
            'Current User' = $null 
            'Last User(s)' = $null  
            'IP | MAC' = $null  
            Model = $null 
            'OS' = $null 
            'OS Build' = $null 
            'BIOS Ver' = $null 
            Encryption = $null 
            'Free Space' = $null 
            RAM = $null 
            'SSO Client' = $null 
            'Kiosk Role' = $null 
            'Asset Tag' = $null 
            'Service Tag' = $null 
            'Last Reboot' = $null 
            Printers = $null 
         }
    
         $obj = New-Object -TypeName PSObject -Property $props
    
         return $obj
    }
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Last User Logon' -PercentComplete 15 -ParentId 1
    $LastUserSCCM = Get-SCCMLastUserLogOnQuery $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for IP Address' -PercentComplete 20 -ParentId 1
    $FindIPSCCM = Get-SCCMIPQuery $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Model' -PercentComplete 25 -ParentId 1
    $CompModelSCCM = Get-SCCMFindModelQuery $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Bios Version' -PercentComplete 30 -ParentId 1
    $FindBiosVerSCCM = Get-SCCMBiosVerQuery $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for HDD/SSD space' -PercentComplete 35 -ParentId 1
    $CompFreeSpaceSCCMGB = Get-SCCMFindFreeSpaceQuery $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for RAM' -PercentComplete 40 -ParentId 1
    $FindMemorySCCMGB = Get-SCCMFindMemoryQuery $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Asset Tag' -PercentComplete 45 -ParentId 1
    $FindAssetTagSCCM = Get-SCCMFindAssetTagQuery $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Service Tag' -PercentComplete 50 -ParentId 1
    $FindServiceTagSCCM = Get-SCCMServiceTagQuery $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for OS Name' -PercentComplete 55 -ParentId 1
    $FindOSNameSCCM = Get-SCCMFindOSNameQuery $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for OS Architecture' -PercentComplete 60 -ParentId 1
    $FindOSArchSCCM = Get-SCCMFindOSArch $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for OS Build' -PercentComplete 65 -ParentId 1
    $FindOSBuild = Get-SCCMFindOSBuild $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Encryption' -PercentComplete 70 -ParentId 1
    $FindEncryptionSCCM = Get-SCCMFindEncryption $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Last boot time' -PercentComplete 75 -ParentId 1
    $FindLastBootUpTimeSCCM = Get-SCCMLastBootUpTime $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Printers' -PercentComplete 80 -ParentId 1
    $FindPCPrinterSCCM = Get-SCCMPCPrinter $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for MAC address' -PercentComplete 85 -ParentId 1
    $FindMACAddressSCCM = Get-SCCMFindMACAddress $comp
    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for Chassis Type' -PercentComplete 90 -ParentId 1
    $FindChassisTypeSCCM = Get-SCCMChassisType($comp)
    if($FindAssetTagSCCM -is [array]){
        Write-Warning "Dupe record in SCCM - $comp"
        $FindAssetTagSCCM = $FindAssetTagSCCM[0]
        <#Get-CMDBFallback $comp#>
    }

    Write-Progress -Activity "Getting SCCM data for $comp" -Status 'Querying for CMDB data' -PercentComplete 95 -ParentId 1
    $cmdbData = Search-ISMBO -bo cis -Filter "Name eq $comp" -RawFilter

    $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 ' | '

    if($FindLastHardwareScanSCCM){
        Write-Host "`n`nPulling cached SCCM data for $comp." -ForegroundColor Yellow
        Write-Host "Last Hardware Scan Time: $FindLastHardwareScanSCCM" -ForegroundColor Yellow
    }
    
    Write-Progress -Activity "Getting SCCM data for $comp" -Completed

    $props = [Ordered]@{ 
        Hostname = "$comp"
        Status = "$compStatus"
        'Current User' = "Not Available"
        'Last User(s)' = "$LastUserSCCM"  
        'IP | MAC' = "$FindIPSCCM | $FindMACAddressSCCM" 
        Model = "$CompModelSCCM ($FindChassisTypeSCCM)"
        'OS' = "$FindOSNameSCCM" + " ($FindOSArchSCCM)"
        'OS Build' = "Build #$FindOSBuild"
        'BIOS Ver' = "$FindBiosVerSCCM"
        Encryption = "$FindEncryptionSCCM"
        'Free Space' = "$CompFreeSpaceSCCMGB" + " GB"
        RAM = "$FindMemorySCCMGB" + " GB "
        'SSO Client' = "Not Available"
        'Kiosk Role' = "Not Available"
        'Asset Tag' = "$FindAssetTagSCCM"
        'Service Tag' = "$FindServiceTagSCCM"
        'Last Reboot' = "$FindLastBootUpTimeSCCM"
        Printers = "$FindPCPrinterSCCM" 
        'CMDB Location' = "$LocationData"
        'CMDB Status' = $cmdbData.Status
     }

     $obj = New-Object -TypeName PSObject -Property $props
     if($cmdbData.values.ConfigurationItem._SHSDELAsset -eq 'True'){
        $delInfo = Get-CMDBDELInfo $cmdbData
        $obj | Add-Member -MemberType NoteProperty -Name 'DEL Owner' -Value $delInfo.Owner
        $obj | Add-Member -MemberType NoteProperty -Name 'DEL Vendor PC' -Value $delInfo.Vendor
        $obj | Add-Member -MemberType NoteProperty -Name 'DEL Description' -Value $delInfo.Description
      } 
     return $obj
}

Function Get-SCCMLastUserLogOnQuery($ComputerName){

    $SCCMLastUserLogOnQuery = "select SMS_R_System.LastLogonUserName from SMS_R_System where SMS_R_System.Name = '$ComputerName'"
    $LastUserSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $SCCMLastUserLogOnQuery | 
    Select-Object -ExpandProperty LastLogonUserName

    Return $LastUserSCCM
}

Function Get-SCCMIPQuery($ComputerName){

    $FindIPQuery = "select SMS_R_System.IPAddresses from SMS_R_System where SMS_R_System.Name = '$ComputerName'"
    $FindIPSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindIPQuery |
    Select-Object -ExpandProperty IPAddresses

    if($FindIPSCCM.Count -gt 1){
        
        Return $FindIPSCCM[0]
    }else{
    
        Return $FindIPSCCM    
    }
}

Function Get-SCCMFindMACAddress($ComputerName){

    $FindMacAddressQuery = "select SMS_R_System.MACAddresses from SMS_R_System where SMS_R_System.Name = '$ComputerName'"
    $FindMacAddressSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindMacAddressQuery | Select-Object -ExpandProperty MACAddresses
    
    if($FindMacAddressSCCM.Count -gt 1){
        
        Return $FindMacAddressSCCM[0]
    }else{
    
        Return $FindMacAddressSCCM    
    }
} 

Function Get-SCCMFindModelQuery($ComputerName){

    $FindModelQuery = "select SMS_G_System_COMPUTER_SYSTEM.Model from SMS_R_System inner join SMS_G_System_COMPUTER_SYSTEM on 
                       SMS_G_System_COMPUTER_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
    $CompModelSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindModelQuery | 
    Select-Object -ExpandProperty Model

    Return $CompModelSCCM
}

Function Get-SCCMFindFreeSpaceQuery($ComputerName){
    
    $FindFreeSpaceQuery = "select SMS_G_System_LOGICAL_DISK.Size from  SMS_R_System inner join SMS_G_System_LOGICAL_DISK on 
                           SMS_G_System_LOGICAL_DISK.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
    $CompFreeSpaceSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindFreeSpaceQuery | 
    Select-Object -ExpandProperty Size  
    $CompFreeSpaceSCCMGB = [math]::Round(($CompFreeSpaceSCCM / 1000), 2)

    Return $CompFreeSpaceSCCMGB
}

Function Get-SCCMFindMemoryQuery($ComputerName){
    
    $FindMemoryQuery = "select SMS_G_System_X86_PC_MEMORY.TotalPhysicalMemory from SMS_R_System inner join SMS_G_System_X86_PC_MEMORY on 
                        SMS_G_System_X86_PC_MEMORY.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
    $FindMemorySCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindMemoryQuery | 
    Select-Object -ExpandProperty TotalPhysicalMemory  
    $FindMemorySCCMGB = [math]::Round($FindMemorySCCM / 1MB)

    Return $FindMemorySCCMGB
}

Function Get-SCCMFindOSNameQuery($ComputerName){
    
    $FindOSNameQuery = "select SMS_G_System_OPERATING_SYSTEM.Caption, SMS_G_System_OPERATING_SYSTEM.OSArchitecture, SMS_G_System_OPERATING_SYSTEM.BuildNumber from  
                        SMS_R_System inner join SMS_G_System_OPERATING_SYSTEM on SMS_G_System_OPERATING_SYSTEM.ResourceID = 
                        SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
    $FindOSNameSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindOSNameQuery | 
    Select-Object -ExpandProperty Caption
 
    Return $FindOSNameSCCM  
}

Function Get-SCCMFindOSArch ($ComputerName){

    $FindOSNameQuery = "select SMS_G_System_OPERATING_SYSTEM.Caption, SMS_G_System_OPERATING_SYSTEM.OSArchitecture, SMS_G_System_OPERATING_SYSTEM.BuildNumber from  
                        SMS_R_System inner join SMS_G_System_OPERATING_SYSTEM on SMS_G_System_OPERATING_SYSTEM.ResourceID = 
                        SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
    $FindOSArchSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindOSNameQuery | 
    Select-Object -ExpandProperty OSArchitecture
     
    Return $FindOSArchSCCM
}

Function Get-SCCMFindOSBuild ($ComputerName){
    
    $FindOSNameQuery = "select SMS_G_System_OPERATING_SYSTEM.Caption, SMS_G_System_OPERATING_SYSTEM.OSArchitecture, SMS_G_System_OPERATING_SYSTEM.BuildNumber from  
                        SMS_R_System inner join SMS_G_System_OPERATING_SYSTEM on SMS_G_System_OPERATING_SYSTEM.ResourceID = 
                        SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
    $FindOSBuild = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindOSNameQuery | 
    Select-Object -ExpandProperty BuildNumber

    Return $FindOSBuild
}

Function Get-SCCMBiosVerQuery($ComputerName){
    
    $FindBiosVerQuery = "select SMS_G_System_PC_BIOS.SMBIOSBIOSVersion from  SMS_R_System inner join SMS_G_System_PC_BIOS on 
                         SMS_G_System_PC_BIOS.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
    $FindBiosVerSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindBiosVerQuery | 
    Select-Object -ExpandProperty SMBIOSBIOSVersion

    Return $FindBiosVerSCCM
}
 
Function Get-SCCMServiceTagQuery($ComputerName){
    
    $FindServiceTagQuery = "select SMS_G_System_SYSTEM_ENCLOSURE.SerialNumber from  SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on 
                            SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'" 
    $FindServiceTagSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindServiceTagQuery | 
    Select-Object -ExpandProperty SerialNumber

    Return $FindServiceTagSCCM
}

Function Get-SCCMFindAssetTagQuery($ComputerName){

    $FindAssetTagQuery = "select SMS_G_System_SYSTEM_ENCLOSURE.SMBIOSAssetTag from  SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on 
                          SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'"
    $FindAssetTagSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindAssetTagQuery | 
    Select-Object -ExpandProperty SMBIOSAssetTag

    Return $FindAssetTagSCCM
}


Function Get-SCCMFindEncryption($ComputerName){

    $FindEncryptionQuery = "select SMS_G_System_ENCRYPTABLE_VOLUME.ProtectionStatus from  SMS_R_System inner join SMS_G_System_ENCRYPTABLE_VOLUME on 
    SMS_G_System_ENCRYPTABLE_VOLUME.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
    
    $FindEncryptionSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindEncryptionQuery | 
    Select-Object -ExpandProperty ProtectionStatus

    Switch($FindEncryptionSCCM){
        0{$FindEncryptionSCCM = "BitLocker (Disabled)"}

        1{$FindEncryptionSCCM = "BitLocker (Enabled [Unlocked])"}

        2{$FindEncryptionSCCM = "BitLocker (Enabled [Locked])"}
        
        Default{$FindEncryptionSCCM = "Encryption Not Found"}
    }

    Return $FindEncryptionSCCM
}

Function Get-SCCMLastBootUpTime($ComputerName){

    $FindLastBootUpTimeQuery = "select SMS_G_System_OPERATING_SYSTEM.LastBootUpTime from SMS_R_System inner join SMS_G_System_OPERATING_SYSTEM on 
    SMS_G_System_OPERATING_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"
    
    $FindLastBootUpTimeSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindLastBootUpTimeQuery | 
    Select-Object -ExpandProperty LastBootUpTime
    
    if($null -eq $FindLastBootUpTimeSCCM){
        return $null
    }

    $Year = $FindLastBootUpTimeSCCM.substring(0,4)
    $Month = $FindLastBootUpTimeSCCM.substring(4,2)
    $Day = $FindLastBootUpTimeSCCM.substring(6,2)
    $Hour = $FindLastBootUpTimeSCCM.substring(8,2)
    $Minute = $FindLastBootUpTimeSCCM.substring(10,2)
    $Second = $FindLastBootUpTimeSCCM.substring(12,2)

    $LastBootUpTime = "$Month/$Day/$Year $Hour" + ":" + "$Minute" + ":" + "$Second"

    Return $LastBootUpTime
}

Function Get-SCCMPCPrinter($ComputerName){

    $FindPCPrinterQuery = "select SMS_G_System_PRINTER_DEVICE.Name, SMS_G_System_PRINTER_DEVICE.PortName from SMS_R_System inner join SMS_G_System_PRINTER_DEVICE 
    on SMS_G_System_PRINTER_DEVICE.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"

    $FindPCPrinterSCCM = (Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindPCPrinterQuery | 
    Where-Object -Property PortName -ne nul: | 
    Where-Object -Property PortName -ne PORTPROMPT: |
    Where-Object -Property PortName -ne SHRFAX: | Select-Object -ExpandProperty Name) -join ' || '
    
    Return $FindPCPrinterSCCM
}

Function Get-SCCMLastHardwareScan($ComputerName){
    
    $FindLastHardwareScanQuery = "select SMS_G_System_WORKSTATION_STATUS.LastHardwareScan from  SMS_R_System inner join SMS_G_System_WORKSTATION_STATUS on SMS_G_System_WORKSTATION_STATUS.ResourceID = SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"

    $FindLastHardwareScanSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindLastHardwareScanQuery | Select-Object -ExpandProperty LastHardwareScan
    
    if($null -eq $FindLastHardwareScanSCCM){
        return $null
    }

    $Year = $FindLastHardwareScanSCCM.substring(0,4)
    $Month = $FindLastHardwareScanSCCM.substring(4,2)
    $Day = $FindLastHardwareScanSCCM.substring(6,2)

    $Hour = $FindLastHardwareScanSCCM.substring(8,2)
    $Minute = $FindLastHardwareScanSCCM.substring(10,2)
    $Second = $FindLastHardwareScanSCCM.substring(12,2)

    $LastBootUpTime = "$Month/$Day/$Year $Hour" + ":" + "$Minute" + ":" + "$Second"

    Return $LastBootUpTime
}

Function Get-SCCMChassisType($ComputerName){

    $FindProcessorQuery = "select SMS_G_System_PROCESSOR.Name from  SMS_R_System inner join SMS_G_System_PROCESSOR on SMS_G_System_PROCESSOR.ResourceId = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on 
    SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'"

    $FindProcessorSCCM = Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $FindProcessorQuery | Select-Object -ExpandProperty Name

    $Model = Get-SCCMFindModelQuery $ComputerName

    Switch -Wildcard($Model){
        "Optiplex*" {
                        Switch -Wildcard($FindProcessorSCCM){
                            "Intel(R) Core(TM) i5-9500 CPU*" {Return  "SFF"} #5070
                            
                            "Intel(R) Core(TM) i5-6500 CPU*" {Return "SFF"} #7040

                            "Intel(R) Core(TM) i5-6500T*" {Return "Micro"} #7040

                            "Intel(R) Core(TM) i7-6700 CPU*" {Return "SFF"} #7040

                            "Intel(R) Core(TM) i7-6700T*" {Return "Micro"} #7040

                            "Intel(R) Core(TM) i5-9500T*" {Return "Micro"} #5070

                            "Intel(R) Core(TM) i5-8500 CPU*" {Return "SFF"} #5060

                            "Intel(R) Core(TM) i5-8500T*" {Return "Micro"} #5060

                            "Intel(R) Core(TM) i5-7500*" {Return "SFF"} #5050

                            "Intel(R) Core(TM) i5-4670 CPU*" {Return "SFF"} #9020

                            "Intel(R) Core(TM) i5-4590 CPU*" {Return "SFF"} #9020

                            "Intel(R) Core(TM) i5-4590T CPU*" {Return "Micro"} #9020M

                            "Intel(R) Core(TM) i5-4690 CPU*" {Return "SFF"} #9020

                            "Intel(R) Core(TM) i5-3550 CPU*" {Return "SFF"} #9010

                            "Intel(R) Core(TM) i5-2400 CPU*" {Return "SFF"} #990

                            Default {Return "Optiplex - Chassis Type - Unknown"}
                        }

                    }
    
        "Latitude*" { 
                        Return "Laptop"
                    }
        "Precision*"{
                        Return "Laptop"
        }
        "M24*" {
            return "Anesthesia Cart"
        }
        "Medix*"{
            return "Anesthesia Cart"
        }

        Default {Return "Unknown Model/Chassis"}
    }

} 
  
Function Get-SCCM_USB($ComputerName) {
    $usbDevicesQuery = "select SMS_G_System_USB_DEVICE.* from  SMS_R_System inner join 
    SMS_G_System_USB_DEVICE on SMS_G_System_USB_DEVICE.ResourceID = 
    SMS_R_System.ResourceId where SMS_R_System.Name = '$ComputerName'"

    return Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $usbDevicesQuery
}

function Get-SCCM_PCFromMAC($mac) {
    # Try to get address in the right format first like 12:34:56:78:9A:BC
    # Covers deliminating '-', '.', or no delimeter
    $mac = $mac -replace '([^:.-]{2})[-.]?[^:](?!$)', '$1:'
    $pcFromMacQuery = "select SMS_R_System.Name from SMS_R_System where SMS_R_System.MACAddresses = '$mac'"

    return (Get-WmiObject -namespace $SCCMNAMESPACE -DirectRead -computer $SCCMSERVER -query $pcFromMacQuery).Name
}

function Get-SCCM_Apps($ComputerName) {

    $SCCMX64Query = "select SMS_G_System_ADD_REMOVE_PROGRAMS_64.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS_64.Version, SMS_G_System_ADD_REMOVE_PROGRAMS_64.Publisher, SMS_G_System_ADD_REMOVE_PROGRAMS_64.InstallDate from SMS_R_System inner join SMS_G_System_ADD_REMOVE_PROGRAMS_64 on 
    SMS_G_System_ADD_REMOVE_PROGRAMS_64.ResourceId = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on 
    SMS_G_System_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'"
                
    $SCCMX86Query = "select SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName, SMS_G_System_ADD_REMOVE_PROGRAMS.Version, SMS_G_System_ADD_REMOVE_PROGRAMS.Publisher, SMS_G_System_ADD_REMOVE_PROGRAMS.InstallDate from SMS_R_System inner join SMS_G_System_ADD_REMOVE_PROGRAMS on 
    SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID = SMS_R_System.ResourceId inner join SMS_G_System_SYSTEM on 
    SMS_G_System_SYSTEM.ResourceID = SMS_R_System.ResourceId where SMS_G_System_SYSTEM.Name = '$ComputerName'"
                
    $64apps = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMX64Query | 
    Select-Object -Property DisplayName,Version,Publisher,InstallDate | Add-Member -NotePropertyName Computer -NotePropertyValue $ComputerName -PassThru | Add-Member -NotePropertyName x86/x64 -NotePropertyValue 64 -PassThru | Sort-Object -Property DisplayName
                
    $86apps = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMX86Query | 
    Select-Object -Property DisplayName,Version,Publisher,InstallDate | Add-Member -NotePropertyName Computer -NotePropertyValue $ComputerName -PassThru | Add-Member -NotePropertyName x86/x64 -NotePropertyValue 32 -PassThru | Sort-Object -Property DisplayName 

    return $86apps, $64apps
}

Function Get-AssetConversion($ComputerName){
    
    #Set Asset Tag to $comp
    $assetTagSCCM = $ComputerName

    $SCCMQuery = "select SMS_R_System.Name from SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId Where SMS_G_System_SYSTEM_ENCLOSURE.SMBIOSAssetTag = '$assetTagSCCM'"
          
    #Set $comp to find hostname of PC
    $nameHolder = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMQuery | Select-Object -First 1 -ExpandProperty Name
            
    if($null -ne $nameHolder){
        $ComputerName = $nameHolder
        return $ComputerName
    }

    return $null 
}

Function HostnamesByPrinter($ComputerName){

    $FindHostnamesByPrinterQuery = "select SMS_R_System.Name from  SMS_R_System inner join SMS_G_System_PRINTER_DEVICE on 
    SMS_G_System_PRINTER_DEVICE.ResourceId = SMS_R_System.ResourceId where SMS_G_System_PRINTER_DEVICE.Name = '$ComputerName'"
    
    $FindHostnamesByPrinterSCCM = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $FindHostnamesByPrinterQuery | 
    Select-Object -ExpandProperty Name

    Return $FindHostnamesByPrinterSCCM

}

Function Get-ServiceTagConversion($ComputerName){
    
    #Set Service Tag to $comp
    $ServiceTagSCCM = $ComputerName

    $SCCMQuery = "select SMS_R_System.Name from  SMS_R_System inner join SMS_G_System_SYSTEM_ENCLOSURE on 
    SMS_G_System_SYSTEM_ENCLOSURE.ResourceId = SMS_R_System.ResourceId where SMS_G_System_SYSTEM_ENCLOSURE.SerialNumber = '$ServiceTagSCCM'"
          
    #Set $comp to find hostname of PC
    $nameHolder = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMQuery | Select-Object -First 1 -ExpandProperty Name
            
    if($null -ne $nameHolder)
    {
        $ComputerName = $nameHolder
        return $ComputerName      
    }
    
}

Function Get-SCCM_UserLastLoggedOn($user) {
    $SCCMUserQuery = "select distinct SMS_R_System.Name from  SMS_R_System where SMS_R_System.LastLogonUserName = '$user'"
    
    $computerSCCM = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMUserQuery | Select-Object -ExpandProperty Name
    return $computerSCCM
}

Function Get-SCCM_HostnameMatch($name) {
    $SCCMQuery = "select distinct SMS_R_System.Name from SMS_R_System where SMS_R_System.Name is like '%$name%'"
    $res = Get-WmiObject -namespace $SCCMNameSpace -DirectRead -computer $SCCMServer -query $SCCMQuery | Select-Object -ExpandProperty Name
    return $res
}
