Advertisement
Guest User

IntunePRInventory

a guest
Mar 26th, 2024
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #"StartTime:$(Get-Date)"
  2.  
  3. Function Get-PRCustomInventory {
  4.     # Define and clear variables
  5.         $AllPRInv = $null
  6.         $AllPRInv = @()
  7.         $jsonBlob = $null
  8.         $ComputerName = (Get-CimInstance -Class Win32_ComputerSystem).Name
  9.         $SerialNumber = (Get-CimInstance -Class Win32_ComputerSystemProduct).IdentifyingNumber
  10.         $Manufacturer = (Get-CimInstance -Class Win32_ComputerSystem).Manufacturer
  11.         foreach ($Variable in (Get-Variable -Name 'PRInv_*'))
  12.         {
  13.             $Variable = $null
  14.         }
  15.  
  16.         $PRInv_WMI_Win32_ComputerSystem = @{
  17.             "WMI_Win32_ComputerSystem" = @(
  18.                 $(Get-CimInstance -Class Win32_ComputerSystem -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Name,Manufacturer,Model,Domain,UserName,WORKGROUP)
  19.             )
  20.         }
  21.        
  22.         $PRInv_WMI_Win32_ComputerSystemProduct = @{
  23.             "WMI_Win32_ComputerSystemProduct" = @(
  24.                 $(Get-CimInstance -Class Win32_ComputerSystemProduct -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Version)
  25.             )
  26.         }
  27.        
  28.         $PRInv_WMI_Win32_SystemEnclosure = @{
  29.             "WMI_Win32_SystemEnclosure" = @(
  30.                 $AssetTag = (Get-CimInstance -Class Win32_SystemEnclosure -ErrorAction SilentlyContinue).SMBIOSAssetTag
  31.                 if(-not ([string]::IsNullOrEmpty($AssetTag)) -and -not ($AssetTag -match 'No '))
  32.                 {
  33.                     $(Get-CimInstance -Class Win32_SystemEnclosure -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},SMBIOSAssetTag)
  34.                 }
  35.                 else
  36.                 {
  37.                    
  38.                 }
  39.             )
  40.         }
  41.        
  42.         $PRInv_WMI_Win32_OperatingSystem = @{
  43.             "WMI_Win32_OperatingSystem" = @(
  44.                 $(Get-CimInstance -Class Win32_OperatingSystem -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Version,Caption,OperatingSystemSKU,OSArchitecture,SystemDrive)
  45.             )
  46.         }
  47.        
  48.         $PRInv_Reg_OSUpgradeCompat_Win11 = @{
  49.             "Reg_OSUpgradeCompat_Win11" = @(
  50.                 $(
  51.                     if(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\TargetVersionUpgradeExperienceIndicators\NI22H2" -ErrorAction SilentlyContinue)
  52.                     {
  53.                         $OSUpgradeCompat = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\TargetVersionUpgradeExperienceIndicators\NI22H2"
  54.                         $OSUpgradeCompatReason = $null
  55.                         $OSUpgradeCompat.RedReason | foreach {$OSUpgradeCompatReason += "$_,"}
  56.                         $OSUpgradeCompat | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'Compat'; Expression = {$OSUpgradeCompat.UpgEx}},@{Name = 'Reason'; Expression = {$OSUpgradeCompatReason}}
  57.                     }
  58.                     elseif(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\TargetVersionUpgradeExperienceIndicators\CO21H2" -ErrorAction SilentlyContinue)
  59.                     {
  60.                         $OSUpgradeCompat = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\TargetVersionUpgradeExperienceIndicators\CO21H2"
  61.                         $OSUpgradeCompatReason = $null
  62.                         $OSUpgradeCompat.RedReason | foreach {$OSUpgradeCompatReason += "$_,"}
  63.                         $OSUpgradeCompat | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'Compat'; Expression = {$OSUpgradeCompat.UpgEx}},@{Name = 'Reason'; Expression = {$OSUpgradeCompatReason}}
  64.                     }
  65.                     else
  66.                     {
  67.                        
  68.                     }                      
  69.                 )
  70.             )
  71.         }
  72.        
  73.         $PRInv_Reg_Office_License = @{
  74.             "Reg_Office_License" = @(
  75.                 $(
  76.                     if(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" -Name 'SharedComputerLicensing' -ErrorAction SilentlyContinue)
  77.                     {
  78.                         $Shared_Computer_Activation = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" -Name 'SharedComputerLicensing').SharedComputerLicensing
  79.                         $ProductReleaseIds = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" -Name 'ProductReleaseIds').ProductReleaseIds
  80.                         $DeviceBasedLicensing = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" -Name 'O365ProPlusRetail.DeviceBasedLicensing').'O365ProPlusRetail.DeviceBasedLicensing'
  81.                         if($Shared_Computer_Activation -eq 1)
  82.                         {
  83.                             $true | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'LicenseType'; Expression = {'O365Shared'}}
  84.                         }
  85.                         elseif ($DeviceBasedLicensing -eq 1)
  86.                         {
  87.                             $true | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'LicenseType'; Expression = {'O365Device'}}
  88.                         }
  89.                         elseif ($ProductReleaseIds -match 'O365ProPlusRetail')
  90.                         {
  91.                             $true | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'LicenseType'; Expression = {'O365User'}}
  92.                         }
  93.                         else
  94.                         {
  95.                             $true | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'LicenseType'; Expression = {"KMSorOther-$ProductReleaseIds"}}
  96.                         }
  97.                     }
  98.                     else
  99.                     {
  100.                        
  101.                     }                      
  102.                 )
  103.             )
  104.         }
  105.        
  106.         $PRInv_WMI_Win32_LogicalDisk = @{
  107.             "WMI_Win32_LogicalDisk" = @(
  108.                 $(Get-CimInstance -Class Win32_LogicalDisk -ErrorAction SilentlyContinue | where {$_.DeviceID -eq $PRInv_WMI_Win32_OperatingSystem.WMI_Win32_OperatingSystem.SystemDrive} | select @{Name = 'SN'; Expression = {$SerialNumber}},Size,FreeSpace)
  109.             )
  110.         }
  111.        
  112.         $PRInv_WMI_Win32_Processor = @{
  113.             "WMI_Win32_Processor" = @(
  114.                 $(Get-CimInstance -Class Win32_Processor -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Name,NumberOfCores)
  115.             )
  116.         }
  117.        
  118.         <# $PRInv_WMI_Win32_PhysicalMemory = @{
  119.             "WMI_Win32_PhysicalMemory" = @(
  120.                 $(Get-CimInstance -Class Win32_PhysicalMemory -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Capacity)
  121.             )
  122.         } #>
  123.        
  124.         $PRInv_WMI_Win32_NetworkAdapter = @{
  125.             "WMI_Win32_NetworkAdapter" = @(
  126.                 $(Get-CimInstance -Class Win32_NetworkAdapter -ErrorAction SilentlyContinue | where {($_.PhysicalAdapter -eq $true) -and ($_.Name -notmatch 'anyconnect') -and ($_.Name -notmatch 'bluetooth') -and ($_.Name -notmatch 'vpn')} | select @{Name = 'SN'; Expression = {$SerialNumber}},Index)
  127.             )
  128.         }
  129.        
  130.         $PRInv_WMI_Win32_NetworkAdapterConfiguration = @{
  131.             "WMI_Win32_NetworkAdapterConfiguration" = @(
  132.                 $(Get-CimInstance -Class Win32_NetworkAdapterConfiguration -ErrorAction SilentlyContinue | foreach{foreach($Index in $PRInv_WMI_Win32_NetworkAdapter.WMI_Win32_NetworkAdapter.Index){if($_.Index -eq $Index){$_ | select @{Name = 'SN'; Expression = {$SerialNumber}},Description,IPAddress,MACAddress}}})
  133.             )
  134.         }
  135.        
  136.         if($Manufacturer -match 'lenovo')
  137.         {
  138.             $Warranty = $null
  139.             (Get-CimInstance -Namespace root/Lenovo -ClassName Lenovo_WarrantyInformation -ErrorAction SilentlyContinue | select -ExpandProperty warranty | foreach {
  140.                 $Warranty += "$($_.ID);"}
  141.             )
  142.             $PRInv_WMI_Lenovo_WarrantyInformation = @{
  143.                 "WMI_Lenovo_WarrantyInformation" = @(
  144.                     $(Get-CimInstance -Namespace root/Lenovo -ClassName Lenovo_WarrantyInformation -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'Warranty'; Expression = {$Warranty.trim(';')}},StartDate,EndDate)
  145.                 )
  146.             }
  147.         }
  148.        
  149.         if($Manufacturer -match 'Dell')
  150.         {
  151.             $PRInv_WMI_Dell_WarrantyInformation = @{
  152.                 "WMI_Dell_WarrantyInformation" = @(
  153.                     $(Get-CimInstance -Namespace root/DCIM/SYSMAN -ClassName DCIM_AssetWarrantyInformation -ErrorAction SilentlyContinue | foreach {$_ | select @{Name = 'SN'; Expression = {$SerialNumber}},Name,WarrantyStartDate,WarrantyEndDate})
  154.                 )
  155.             }
  156.         }
  157.        
  158.         $PRInv_WinEvent_Last_Login_User = @{
  159.             "WinEvent_Last_Login_User" = @(
  160.                 $(Get-WinEvent -FilterHashtable @{Logname='Security';ID=4672} | Where-Object {-not(($_.Properties[0].Value -like  "S-1-5-18") -or ($_.Properties[0].Value -like  "S-1-5-19") -or ($_.Properties[0].Value -like  "S-1-5-20"))} | select -first 1 @{Name = 'SN'; Expression = {$SerialNumber}},@{N='User';E={$_.Properties[1].Value}})
  161.             )
  162.         }
  163.        
  164.     # Add all custom inventories to a variable and return json
  165.         foreach ($Variable in (Get-Variable -Name 'PRInv_*'))
  166.         {
  167.             $AllPRInv += $Variable.Value
  168.         }
  169.        
  170.         $AllPRInv | ConvertTo-Json -Compress -Depth 20
  171. }
  172.  
  173.     # Check if script has been run today and just return dumped result to avoid any additional processing on the machine
  174.     if (Test-Path -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory')
  175.     {
  176.         if (Get-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastRunDate' -ErrorAction SilentlyContinue)
  177.         {
  178.             $script:LastRunDate = Get-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastRunDate' | select -ExpandProperty LastRunDate
  179.             if ([datetime]$LastRunDate -ge (Get-Date -Hour 0 -Minute 0 -Second 0))
  180.             {
  181.                 if(Get-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastOutput' -ErrorAction SilentlyContinue)
  182.                 {
  183.                     $script:LastOutput = Get-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastOutput' | select -ExpandProperty LastOutput
  184.                     $script:RunScriptNow = $false
  185.                 }
  186.                 else
  187.                 {
  188.                     $script:RunScriptNow = $true
  189.                 }
  190.             }
  191.             else
  192.             {
  193.                 $script:RunScriptNow = $true
  194.             }
  195.         }
  196.         else
  197.         {
  198.             $script:RunScriptNow = $true
  199.         }
  200.     }
  201.     else
  202.     {
  203.         $script:RunScriptNow = $true
  204.     }
  205.    
  206. if($script:RunScriptNow -eq $true)
  207. {
  208.     # Convert to JSON and return result
  209.     $jsonBlob = Get-PRCustomInventory
  210.    
  211.     # Add Registry LastRunDate for Detection
  212.     if (-not(Test-Path -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory'))
  213.     {
  214.         New-Item -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Force | Out-Null
  215.     }
  216.  
  217.     New-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastRunDate' -PropertyType string -Value $(Get-Date) -Force | Out-Null
  218.     New-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastOutput' -PropertyType string -Value $jsonBlob -Force | Out-Null
  219. }
  220. else
  221. {
  222.     $jsonBlob = $script:LastOutput
  223. }
  224.  
  225. # return result and exit
  226.     $jsonBlob
  227.     #"EndTime:$(Get-Date)"
  228.     exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement