[comment]: # ({02ea8022-02ea8022})
# usermacro.get

[comment]: # ({/02ea8022-02ea8022})

[comment]: # ({78b5ab06-28befc82})
### 描述

```
integer/array usermacro.get(object parameters)
```

该方法允许根据给定的参数检索主机和全局宏。

::: noteclassic
此方法适用于任何类型的用户。可以在用户角色设置中撤销调用该方法的权限。更多信息请参见[User
roles](/manual/web_interface/frontend_sections/users/user_roles)。
:::

[comment]: # ({/78b5ab06-28befc82})

[comment]: # ({665d8bba-141835f9})
### 参数

`(object)` 定义所需输出的参数。

该方法支持以下参数。

|Parameter|[Type](/manual/api/reference_commentary#data-types)|Description|
|--|--|------|
|globalmacro|flag|返回全局宏，而不是主机宏。|
|globalmacroids|ID/array|仅返回具有给定 ID 的全局宏。|
|groupids|ID/array|仅返回属于给定主机组或模板组中的主机或模板的主机宏。|
|hostids|ID/array|仅返回属于给定主机或模板的宏。|
|hostmacroids|ID/array|仅返回具有给定 ID 的主机宏。|
|inherited|boolean|如果设置为 `true`，则仅返回从模板继承的主机原型用户宏。|
|selectHostGroups|query|在 [`hostgroups`](/manual/api/reference/hostgroup/object) 属性中返回该主机宏所属的主机组。<br><br>仅在检索主机宏时使用。|
|selectHosts|query|在 [`hosts`](/manual/api/reference/host/object) 属性中返回该主机宏所属的主机。<br><br>仅在检索主机宏时使用。|
|selectTemplateGroups|query|在 [`templategroups`](/manual/api/reference/templategroup/object) 属性中返回该模板宏所属的模板组。<br><br>仅在检索模板宏时使用。|
|selectTemplates|query|在 [`templates`](/manual/api/reference/template/object) 属性中返回该主机宏所属的模板。<br><br>仅在检索主机宏时使用。|
|sortfield|string/array|按给定属性对结果排序。<br><br>可能的值：`macro`。|
|countOutput|boolean|这些参数在[参考说明](/manual/api/reference_commentary#common-get-method-parameters)中有描述。|
|editable|boolean|^|
|excludeSearch|boolean|^|
|filter|object|^|
|limit|integer|^|
|output|query|^|
|preservekeys|boolean|^|
|search|object|^|
|searchByAny|boolean|^|
|searchWildcardsEnabled|boolean|^|
|sortorder|string/array|^|
|startSearch|boolean|^|
|selectGroups<br>(deprecated)|query|此参数已弃用，请改用 `selectHostGroups` 或 `selectTemplateGroups`。<br>在 `groups` 属性中返回该主机宏所属的主机组和模板组。<br><br>仅在检索主机宏时使用。|

[comment]: # ({/665d8bba-141835f9})

[comment]: # ({7223bab1-7223bab1})
### 返回值

`(integer/array)` 返回以下之一：

-   array 的 objects；
-   如果使用了 `countOutput` 参数，则为检索到的 objects 的数量。

[comment]: # ({/7223bab1-7223bab1})

[comment]: # ({b41637d2-b41637d2})
### 示例

[comment]: # ({/b41637d2-b41637d2})

[comment]: # ({4540e2f0-7089e052})
#### 获取主机的宏变量

检索为 主机宏 "10198" 定义的所有 主机。

[执行请求](/manual/api#执行请求):


```json
{
    "jsonrpc": "2.0",
    "method": "usermacro.get",
    "params": {
        "output": "extend",
        "hostids": "10198"
    },
    "id": 1
}
```
响应：


```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "hostmacroid": "9",
            "hostid": "10198",
            "macro": "{$INTERFACE}",
            "value": "eth0",
            "description": "",
            "type": "0",
            "automatic": "0"
        },
        {
            "hostmacroid": "11",
            "hostid": "10198",
            "macro": "{$SNMP_COMMUNITY}",
            "value": "public",
            "description": "",
            "type": "0",
            "automatic": "0"
        }
    ],
    "id": 1
}
```

[comment]: # ({/4540e2f0-7089e052})

[comment]: # ({93c3e1f5-29b64a7a})
#### 检索全局宏

检索所有全局宏。

[执行请求](/manual/api#执行请求):


```json
{
    "jsonrpc": "2.0",
    "method": "usermacro.get",
    "params": {
        "output": "extend",
        "globalmacro": true
    },
    "id": 1
}
```
响应：


```json
{
    "jsonrpc": "2.0",
    "result": [
        {
            "globalmacroid": "6",
            "macro": "{$SNMP_COMMUNITY}",
            "value": "public",
            "description": "",
            "type": "0"
        }
    ],
    "id": 1
}
```

[comment]: # ({/93c3e1f5-29b64a7a})

[comment]: # ({26db0ec2-26db0ec2})
### 来源

CUserMacro::get() 在 *ui/include/classes/api/services/CUserMacro.php* 中。

[comment]: # ({/26db0ec2-26db0ec2})
