# Inventory Items in Scripts

## What

- Each Inventory Items has an Internal Name, including Custom Fields which provide extended inventory
- The Internal Name can be used to reference an Inventory Item in Scripts
- These Internal Names should be added to either the Launch Arguments or Environment Variables of the Script
- This applies to all script types, be that other Custom Fields, Policy Blocker Scripts or Fileset Scripts

## When

Internal Name of an Inventory Item may be located from the Inventory Query Editor.<span class="Apple-converted-space"> </span>Example shows the Internal Name: ‘device\_product\_name’

![Pasted Graphic 29.png](https://kb.filewave.com/uploads/images/gallery/2024-07/QyDepMQgYvuZglej-embedded-image-xszkmtt6.png)

This may then be added into a Script, by way of either a Launch Argument or Environment Variable

![Pasted Graphic 30.png](https://kb.filewave.com/uploads/images/gallery/2024-07/TRdIe9TMcFoJSmKB-embedded-image-apalysf6.png)

But, which should be used?

## How

In some respects it does not matter which is used, however, for easy reference consider the following:

- Launch Arguments are referenced by their numerical position
- Environment Variables are referenced by a chosen name
- Custom Fields have an abbreviated name and a full name.<span class="Apple-converted-space"> </span>Custom Field names could overlap with a built-in Inventory Item.

### Built-In Inventory

In general, recommendation here is that of Environment Variables.<span class="Apple-converted-space"> </span>This makes reading the script easier without having to redefine new names within the script for Launch Argument positions.

For example:

![Pasted Graphic 31.png](https://kb.filewave.com/uploads/images/gallery/2024-07/NiOh6QTMHLrDFc0Y-embedded-image-kz46raaw.png)

Could be referenced in a script as:

<table border="1" id="bkmrk-macos-shell-echo-%241-" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr><td>macOS shell</td><td>echo $1</td></tr><tr><td>Windows Powershell</td><td>echo $args\[0\]</td></tr></tbody></table>

But to make the parameters more easily recognisable for anyone reading the script, it could be desirable to name them:

<table border="1" id="bkmrk-macos-shell-product_" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 50%;"></col><col style="width: 50%;"></col></colgroup><tbody><tr><td>macOS shell</td><td>```
product_name=“$1”
echo $product_name
```

  
</td></tr><tr><td>Windows Powershell</td><td>```
$product_name=“$args[0]”
echo $product_name
```

  
</td></tr></tbody></table>

References to the provided inventory parameters in the script now makes more sense, but as mentioned, Environment Variables take this a step further:

![Pasted Graphic 32.png](https://kb.filewave.com/uploads/images/gallery/2024-07/KoGxqMx9maNr3KIW-embedded-image-0wpbc2ez.png)

A variable name is already defined and this can be referenced in the script directly

```
echo $product_name
```

### Improvements

#### First Improvement

To improve the readability of the script further, consider setting the variable name to match the value, e.g:

![Pasted Graphic 33.png](https://kb.filewave.com/uploads/images/gallery/2024-07/imU79dXCQajD97tQ-embedded-image-skyqleci.png)

```
echo $device_product_name
```

#### Second Improvement

When referencing a Custom Field in a script, it could be referenced in one of two ways. <span class="Apple-converted-space"> </span>

Example Custom Field: State

![Pasted Graphic 39.png](https://kb.filewave.com/uploads/images/gallery/2024-07/AXJgxl2b8I5M37Ry-embedded-image-npp8tct8.png)

<p class="callout success">Note, the description has been used to indicate this is a Custom Field. Inventory Query editor shows Description.</p>

This could be referenced with:

![Pasted Graphic 41.png](https://kb.filewave.com/uploads/images/gallery/2024-07/JLp6aCbxY4KiXCgt-embedded-image-kw0zkmfs.png)

and

```
echo $state
```

However, there is a built-in Inventory Item called State.<span class="Apple-converted-space"> </span>So there are now two Internal Names of ‘state’

![Pasted Graphic 38.png](https://kb.filewave.com/uploads/images/gallery/2024-07/Nyvh16aHs1cBIANW-embedded-image-a1jju9vm.png)

![Pasted Graphic 37.png](https://kb.filewave.com/uploads/images/gallery/2024-07/O6tCx50PJQq0JT3D-embedded-image-rdtja1oo.png)

The above scripted example for 'state' would actually report the built-in value, not the Custom Field. There is, though, a hidden prefix that can be used. <span class="Apple-converted-space"> </span>

This Custom Field could be referenced as either:

- %state%
- %CustomFields.state%

The latter prevents unexpected collusion with the matching Internal Name.<span class="Apple-converted-space"> </span>Hence, to make the parameters more obvious when reading…

![Pasted Graphic 40.png](https://kb.filewave.com/uploads/images/gallery/2024-07/kbDuPKrYEEkoJFSx-embedded-image-hu3lgfns.png)

```
echo $custom_fields_state
echo $internal_device_product_name
```

<p class="callout info">Notice, despite no prefix existing for built-in Inventory Items, by including a prefix for both variables in the Environment Variables definitions, reading the script will be much clearer.</p>

<p class="callout success">Anyone reading the script is now aware that state is a Custom Field, without having to cross reference anything.<span class="Apple-converted-space"> </span>Likewise, the reader also is aware that the device\_product\_name also comes from Inventory, again, without any cross reference necessary.</p>

### Unknown Inventory

Not all Inventory Items are available as parameters.

<p class="callout warning">The FileWave Client builds out the report of items to inventory and return to server.<span class="Apple-converted-space"> </span>Additionally, all Custom Fields, including those server-side (Administrator Custom Fields), are available to the client.<span class="Apple-converted-space"> </span>However, inventory returned by MDM is not available, since the client is unaware of these values, they are pure server-side.</p>

%CustomFields.location%