[comment]: # translation:outdated

[comment]: # ({8961fc33-8961fc33})
# 创建

[comment]: # ({/8961fc33-8961fc33})

[comment]: # ({a2df1a0f-d7a51ffa})
### 描述
`object script.create(object/array scripts)`

此方法允许创建新脚本。
::: 请注意
此方法只有_超级管理员_ 用户可以使用。可以在用户角色设置中撤销调用此方法的权限。更多信息见[User roles](manual/web_interface/frontend_sections/administration/user_roles)。
:::

[comment]: # ({/a2df1a0f-d7a51ffa})

[comment]: # ({bcb9a453-bcb9a453})
### 参数

`(对象/数组)` 要创建的脚本。

此方法接受具有[标准脚本属性](/manual/api/reference/script/object#脚本)的脚本。

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

[comment]: # ({15216b8d-15216b8d})
### 返回值

`(对象)` 返回一个`scriptids`属性包含被创建脚本ID的对象。返回的ID顺序与传入脚本的顺序一致。

[comment]: # ({/15216b8d-15216b8d})

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

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

[comment]: # ({655bc2d0-55d911d7})
#### 创建webhook脚本
创建一个webhook脚本向外部服务发送HTTP请求。
[请求](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "script.create",
    "params": {
        "name": "Webhook script",
        "command": "try {\n var request = new HttpRequest(),\n response,\n data;\n\n request.addHeader('Content-Type: application/json');\n\n response = request.post('https://localhost/post', value);\n\n try {\n response = JSON.parse(response);\n }\n catch (error) {\n response = null;\n }\n\n if (request.getStatus() !== 200 || !('data' in response)) {\n throw 'Unexpected response.';\n }\n\n data = JSON.stringify(response.data);\n\n Zabbix.log(3, '[Webhook Script] response data: ' + data);\n\n return data;\n}\ncatch (error) {\n Zabbix.log(3, '[Webhook Script] script execution failed: ' + error);\n throw 'Execution failed: ' + error + '.';\n}",
        "type": 5,
        "timeout": "40s",
        "parameters": [
            {
                "name": "token",
                "value": "{$WEBHOOK.TOKEN}"
            },
            {
                "name": "host",
                "value": "{HOST.HOST}"
            },
            {
                "name": "v",
                "value": "2.2"
            }
        ]
    },
    "id": 1
}
```

响应
```json
{
    "jsonrpc": "2.0",
    "result": {
        "scriptids": [
            "3"
        ]
    },
    "id": 1
}
```

[comment]: # ({/655bc2d0-55d911d7})

[comment]: # ({e3a6c1b8-16a09744})
#### 创建SSH脚本
创建一个SSH脚本，通过公钥完成身份验证从而可以在主机运行，并且带有上下文菜单。
[请求](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "script.create",
    "params": {
        "name": "SSH script",
        "command": "my script command",
        "type": 2,
        "authtype": 1,
        "username": "John",
        "publickey": "pub.key",
        "privatekey": "priv.key",
        "password": "secret",
        "port": "12345",
        "scope": 2,
        "menu_path": "All scripts/SSH",
        "usrgrpid": "7",
        "groupid": "4"
    },
    "id": 1
}
```

响应
```json
{
    "jsonrpc": "2.0",
    "result": {
        "scriptids": [
            "5"
        ]
    },
    "id": 1
}
```

[comment]: # ({/e3a6c1b8-16a09744})

[comment]: # ({189a5044-0e2eb663})
#### 创建定制脚本

创建一个重启服务器的定制脚本。该脚本会要求主机的写权限，并且在前端运行之前会显示配置信息。
[请求](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "script.create",
    "params": {
        "name": "Reboot server",
        "command": "reboot server {MANUALINPUT}",
        "type": 0,
        "scope": 2,
        "confirmation": "Are you sure you would like to reboot the server {MANUALINPUT}?",
        "manualinput": 1,
        "manualinput_prompt": "Which server you want to reboot?",
        "manualinput_validator": "[1-9]",
        "manualinput_validator_type": 0,
        "manualinput_default_value": "1"
    },
    "id": 1
}
```
响应
```json
{
    "jsonrpc": "2.0",
    "result": {
        "scriptids": [
            "4"
        ]
    },
    "id": 1
}
```

[comment]: # ({/189a5044-0e2eb663})

[comment]: # ({aecb5455-c363dc9b})
#### 创建一个URL类型的脚本

为主为作用域创建一个URL类型的脚本，该脚本会要求主机的写权限，并且在前端运行之前会显示配置信息。
[请求](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "script.create",
    "params": {
        "name": "URL script",
        "type": 6,
        "scope": 2,
        "url": "http://zabbix/ui/zabbix.php?action=host.edit&hostid={HOST.ID}",
        "confirmation": "Edit host {HOST.NAME}?",
        "new_window": 0
    },
    "id": 1
}
```

响应
```json
{
    "jsonrpc": "2.0",
    "result": {
        "scriptids": [
            "56"
        ]
    },
    "id": 1
}
```

[comment]: # ({/aecb5455-c363dc9b})

[comment]: # ({9b0cfc65-83bd22b0})
####  创建一个手动输入的URL类型脚本
为在新窗口中打开并具有手动输入的事件作用域创建URL类型脚本。
[请求](/manual/api#performing-requests):

```json
{
    "jsonrpc": "2.0",
    "method": "script.create",
    "params": {
        "name": "URL script with manual input",
        "type": 6,
        "scope": 4,
        "url": "http://zabbix/ui/zabbix.php?action={MANUALINPUT}",
        "new_window": 1,
        "manualinput": 1,
        "manualinput_prompt": "Select a page to open:",
        "manualinput_validator": "dashboard.view,script.list,actionlog.list",
        "manualinput_validator_type": 1
    },
    "id": 1
}
```

响应
```json
{
    "jsonrpc": "2.0",
    "result": {
        "scriptids": [
            "57"
        ]
    },
    "id": 1
}
```

[comment]: # ({/9b0cfc65-83bd22b0})

[comment]: # ({8c09f129-8c09f129})
### 源代码

CScript::create() 在 *ui/include/classes/api/services/CScript.php*。

[comment]: # ({/8c09f129-8c09f129})
