# CMS_AddCustomInventory

> Adds a persistent CustomInventory row to the database, meaning that the data will not be flushed out the next time that the CustomInventory package is run.

Source: https://docs.capaone.com/capainstaller/vb-scripting-library/cms-functions/cms-addcustominventory/  
Product: CapaInstaller — a separate CapaSystems product; do not apply this page to any other.

### Description

Adds a persistent CustomInventory row to the database, meaning that the data will not be flushed out the next time that the CustomInventory package is run.

:::note[Important]
### Please note

Please note, that this is an expensive call, seen from the Frontend/database perspective. Calling this function repeatedly from a package could result in overall slower performance. This function should be used with care.
:::

### Syntax

**VBScript**

```vb
CMS_AddCustomInventory(category, entry, value, valuetype)
```

**PowerShell**

```powershell
[bool] CMS_AddCustomInventory -category <string> -entry <string> -value <string> -valuetype <string>
```

### Parameters

#### category (String)

Category name

#### entry (String)

variable name

#### value (string)

value (string)

If you are inserting a Time (valuetype: "T") it must be set in epoch-time, meaning seconds since January 1. 1970. The scripting library has a function called GetEpochTime, which will convert a date to seconds from 1-1-1970.

If you want to use 'now' as the value, you can either insert;

- an empty string ("")
- or the value ("0")

#### valuetype (string)

The type of the value

Valid valuetypes are;

- "S" for string
- "I" for integer
- "T" for time

### Return value

**VBScript**

Boolean, TRUE if function completed successfully.

**PowerShell**

Boolean, TRUE if function completed successfully.

### Example

**VBScript**

```vb
If bStatus Then bStatus = CMS_AddCustomInventory("Customer Inventory", "Install time packages", "112", "I")
If bStatus Then bStatus = CMS_AddCustomInventory("Bitlocker", "TPM Chip - Enabled", "True", "S")
If bStatus Then bStatus = CMS_AddCustomInventory("Bitlocker", "TPM Chip - Manufactured", "1694973146", "T")
```

**PowerShell**

```powershell
CMS_AddCustomInventory 'Customer Inventory' 'Install time packages' '112' 'I'

CMS_AddCustomInventory -category 'Bitlocker' -entry 'TPM Chip - Enabled' -value 'True' -valuetype 'S'

if (CMS_AddCustomInventory('Bitlocker' 'TPM Chip - Manufactured' '1694973146' 'T') {
  $cs.Job_WriteLog("Added", "Bitlocker entry added to Custom inventory")
}
```

[Scripting Guidelines](/capainstaller/vb-scripting-library/scripting-guidelines/)

### Handling concurrent execution in PowerPacks

:::caution
When called from a PowerPack, `CMS_AddCustomInventory` can fail if it runs on many devices at the same time. Wrap the call in a retry helper such as the one below to make the call resilient to transient failures.
:::

**PowerShell**

```powershell
function Add-CustomInventoryWithRetry {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string]$Category,
        [Parameter(Mandatory = $true)]
        [string]$Entry,
        [Parameter(Mandatory = $true)]
        [AllowNull()]
        [object]$Value,
        [Parameter(Mandatory = $true)]
        [string]$ValueType,
        [int]$MaxAttempts = 10,
        [int]$SleepSeconds = 3
    )

    if ($MaxAttempts -lt 1) {
        $MaxAttempts = 1
    }
    if ($SleepSeconds -lt 1) {
        $SleepSeconds = 1
    }

    for ($attempt = 1; $attempt -le $MaxAttempts; $attempt++) {
        try {
            CMS_AddCustomInventory -category $Category -entry $Entry -value $Value -valuetype $ValueType
            return
        } catch {
            $errorMessage = $_.Exception.Message

            if ($attempt -ge $MaxAttempts) {
                $cs.Job_WriteLog("CMS_AddCustomInventory failed for entry '$Entry' after $MaxAttempts attempts. Last error: $errorMessage")
                throw
            }

            $cs.Job_WriteLog("CMS_AddCustomInventory failed for entry '$Entry' (attempt $attempt/$MaxAttempts). Retrying in $SleepSeconds seconds. Error: $errorMessage")
            Start-Sleep -Seconds $SleepSeconds
        }
    }
}
```

### Related functions

[CMS_RemoveCustomInventory](/capainstaller/vb-scripting-library/cms-functions/cms-removecustominventory/) [Constants](/capainstaller/vb-scripting-library/addendum/constants/) [Package Property](/capainstaller/vb-scripting-library/addendum/package-property/) [Variables](/capainstaller/vb-scripting-library/addendum/variables/) [Business Unit support](/capainstaller/vb-scripting-library/addendum/business-unit-support/)
