Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #"StartTime:$(Get-Date)"
- Function Get-PRCustomInventory {
- # Define and clear variables
- $AllPRInv = $null
- $AllPRInv = @()
- $jsonBlob = $null
- $ComputerName = (Get-CimInstance -Class Win32_ComputerSystem).Name
- $SerialNumber = (Get-CimInstance -Class Win32_ComputerSystemProduct).IdentifyingNumber
- $Manufacturer = (Get-CimInstance -Class Win32_ComputerSystem).Manufacturer
- foreach ($Variable in (Get-Variable -Name 'PRInv_*'))
- {
- $Variable = $null
- }
- $PRInv_WMI_Win32_ComputerSystem = @{
- "WMI_Win32_ComputerSystem" = @(
- $(Get-CimInstance -Class Win32_ComputerSystem -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Name,Manufacturer,Model,Domain,UserName,WORKGROUP)
- )
- }
- $PRInv_WMI_Win32_ComputerSystemProduct = @{
- "WMI_Win32_ComputerSystemProduct" = @(
- $(Get-CimInstance -Class Win32_ComputerSystemProduct -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Version)
- )
- }
- $PRInv_WMI_Win32_SystemEnclosure = @{
- "WMI_Win32_SystemEnclosure" = @(
- $AssetTag = (Get-CimInstance -Class Win32_SystemEnclosure -ErrorAction SilentlyContinue).SMBIOSAssetTag
- if(-not ([string]::IsNullOrEmpty($AssetTag)) -and -not ($AssetTag -match 'No '))
- {
- $(Get-CimInstance -Class Win32_SystemEnclosure -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},SMBIOSAssetTag)
- }
- else
- {
- }
- )
- }
- $PRInv_WMI_Win32_OperatingSystem = @{
- "WMI_Win32_OperatingSystem" = @(
- $(Get-CimInstance -Class Win32_OperatingSystem -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Version,Caption,OperatingSystemSKU,OSArchitecture,SystemDrive)
- )
- }
- $PRInv_Reg_OSUpgradeCompat_Win11 = @{
- "Reg_OSUpgradeCompat_Win11" = @(
- $(
- if(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\TargetVersionUpgradeExperienceIndicators\NI22H2" -ErrorAction SilentlyContinue)
- {
- $OSUpgradeCompat = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\TargetVersionUpgradeExperienceIndicators\NI22H2"
- $OSUpgradeCompatReason = $null
- $OSUpgradeCompat.RedReason | foreach {$OSUpgradeCompatReason += "$_,"}
- $OSUpgradeCompat | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'Compat'; Expression = {$OSUpgradeCompat.UpgEx}},@{Name = 'Reason'; Expression = {$OSUpgradeCompatReason}}
- }
- elseif(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\TargetVersionUpgradeExperienceIndicators\CO21H2" -ErrorAction SilentlyContinue)
- {
- $OSUpgradeCompat = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\TargetVersionUpgradeExperienceIndicators\CO21H2"
- $OSUpgradeCompatReason = $null
- $OSUpgradeCompat.RedReason | foreach {$OSUpgradeCompatReason += "$_,"}
- $OSUpgradeCompat | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'Compat'; Expression = {$OSUpgradeCompat.UpgEx}},@{Name = 'Reason'; Expression = {$OSUpgradeCompatReason}}
- }
- else
- {
- }
- )
- )
- }
- $PRInv_Reg_Office_License = @{
- "Reg_Office_License" = @(
- $(
- if(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" -Name 'SharedComputerLicensing' -ErrorAction SilentlyContinue)
- {
- $Shared_Computer_Activation = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" -Name 'SharedComputerLicensing').SharedComputerLicensing
- $ProductReleaseIds = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" -Name 'ProductReleaseIds').ProductReleaseIds
- $DeviceBasedLicensing = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" -Name 'O365ProPlusRetail.DeviceBasedLicensing').'O365ProPlusRetail.DeviceBasedLicensing'
- if($Shared_Computer_Activation -eq 1)
- {
- $true | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'LicenseType'; Expression = {'O365Shared'}}
- }
- elseif ($DeviceBasedLicensing -eq 1)
- {
- $true | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'LicenseType'; Expression = {'O365Device'}}
- }
- elseif ($ProductReleaseIds -match 'O365ProPlusRetail')
- {
- $true | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'LicenseType'; Expression = {'O365User'}}
- }
- else
- {
- $true | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'LicenseType'; Expression = {"KMSorOther-$ProductReleaseIds"}}
- }
- }
- else
- {
- }
- )
- )
- }
- $PRInv_WMI_Win32_LogicalDisk = @{
- "WMI_Win32_LogicalDisk" = @(
- $(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)
- )
- }
- $PRInv_WMI_Win32_Processor = @{
- "WMI_Win32_Processor" = @(
- $(Get-CimInstance -Class Win32_Processor -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Name,NumberOfCores)
- )
- }
- <# $PRInv_WMI_Win32_PhysicalMemory = @{
- "WMI_Win32_PhysicalMemory" = @(
- $(Get-CimInstance -Class Win32_PhysicalMemory -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},Capacity)
- )
- } #>
- $PRInv_WMI_Win32_NetworkAdapter = @{
- "WMI_Win32_NetworkAdapter" = @(
- $(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)
- )
- }
- $PRInv_WMI_Win32_NetworkAdapterConfiguration = @{
- "WMI_Win32_NetworkAdapterConfiguration" = @(
- $(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}}})
- )
- }
- if($Manufacturer -match 'lenovo')
- {
- $Warranty = $null
- (Get-CimInstance -Namespace root/Lenovo -ClassName Lenovo_WarrantyInformation -ErrorAction SilentlyContinue | select -ExpandProperty warranty | foreach {
- $Warranty += "$($_.ID);"}
- )
- $PRInv_WMI_Lenovo_WarrantyInformation = @{
- "WMI_Lenovo_WarrantyInformation" = @(
- $(Get-CimInstance -Namespace root/Lenovo -ClassName Lenovo_WarrantyInformation -ErrorAction SilentlyContinue | select @{Name = 'SN'; Expression = {$SerialNumber}},@{Name = 'Warranty'; Expression = {$Warranty.trim(';')}},StartDate,EndDate)
- )
- }
- }
- if($Manufacturer -match 'Dell')
- {
- $PRInv_WMI_Dell_WarrantyInformation = @{
- "WMI_Dell_WarrantyInformation" = @(
- $(Get-CimInstance -Namespace root/DCIM/SYSMAN -ClassName DCIM_AssetWarrantyInformation -ErrorAction SilentlyContinue | foreach {$_ | select @{Name = 'SN'; Expression = {$SerialNumber}},Name,WarrantyStartDate,WarrantyEndDate})
- )
- }
- }
- $PRInv_WinEvent_Last_Login_User = @{
- "WinEvent_Last_Login_User" = @(
- $(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}})
- )
- }
- # Add all custom inventories to a variable and return json
- foreach ($Variable in (Get-Variable -Name 'PRInv_*'))
- {
- $AllPRInv += $Variable.Value
- }
- $AllPRInv | ConvertTo-Json -Compress -Depth 20
- }
- # Check if script has been run today and just return dumped result to avoid any additional processing on the machine
- if (Test-Path -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory')
- {
- if (Get-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastRunDate' -ErrorAction SilentlyContinue)
- {
- $script:LastRunDate = Get-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastRunDate' | select -ExpandProperty LastRunDate
- if ([datetime]$LastRunDate -ge (Get-Date -Hour 0 -Minute 0 -Second 0))
- {
- if(Get-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastOutput' -ErrorAction SilentlyContinue)
- {
- $script:LastOutput = Get-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastOutput' | select -ExpandProperty LastOutput
- $script:RunScriptNow = $false
- }
- else
- {
- $script:RunScriptNow = $true
- }
- }
- else
- {
- $script:RunScriptNow = $true
- }
- }
- else
- {
- $script:RunScriptNow = $true
- }
- }
- else
- {
- $script:RunScriptNow = $true
- }
- if($script:RunScriptNow -eq $true)
- {
- # Convert to JSON and return result
- $jsonBlob = Get-PRCustomInventory
- # Add Registry LastRunDate for Detection
- if (-not(Test-Path -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory'))
- {
- New-Item -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Force | Out-Null
- }
- New-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastRunDate' -PropertyType string -Value $(Get-Date) -Force | Out-Null
- New-ItemProperty -Path 'HKLM:\SOFTWARE\PRInv\PRInv - Intune Custom Inventory' -Name 'LastOutput' -PropertyType string -Value $jsonBlob -Force | Out-Null
- }
- else
- {
- $jsonBlob = $script:LastOutput
- }
- # return result and exit
- $jsonBlob
- #"EndTime:$(Get-Date)"
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement