一、概要
ServiceDesk Plus(SDP) 支持通过低代码的配置,满足丰富的个性化场景需求。但是对于一些复杂的的场景,如集成第三方系统。需要处理复杂的逻辑,SDP自带的业务规则和自定义触发器中的规则和字段更新等动作,就无法处理了。此时通过执行脚本触发存储在服务器中的脚本,就能比较好的解决这种复杂逻辑的处理。需要注意的是,因为将这些逻辑都放到脚本中去进行处理了。需要一定的开发能力。
本文主要介绍SDP是如何实现配置执行本地脚本,以及中间参数传递的详细步骤。
1.1 原理
SDP在触发业务规则/自定义触发器时,会生成一个临时的JSON文件,触发此规则的工单的所有信息都会存储在该JSON文件之中,待业务规则/自定义触发器执行完毕后,此文件就会被删除。
执行脚本时带上的这个参数$COMPLETE_V3_JSON_FILE 这个参数的值就是临时JSON文件的路径。
1.2 演示环境
操作系统:Windows 11
SDP版本号:14302 企业版
Python:3.9.13
注:SDP对执行脚本的语言没有限制,SDP只是触发。具体每种语言所需的环境,需要自行配置。
二、配置
2.1 SDP页面配置
以申请虚拟机的场景为例。
某公司H已经使用了SDP系统,之前处理虚拟机申请都是用户直接找到技术员,技术员再创建虚拟机然后给到用户登录信息。
沟通虚拟机配置以及创建虚拟机耗费了技术员大量的时间,想要实现自动化操作。
2.1.1 创建虚拟机申请服务模板
到SDP模板&表单页面,到对应的服务目录下点击添加服务
模板如下
2.1.2 创建自定触发器
管理>自动化>自定义触发器>审批等级中
新建一条触发器规则,在检测到审批已批准后,判断请求的模板是否是“申请虚拟机”模板,如果是执行CreateVMware.txt文件中的脚本。如下图:
2.2 脚本配置
新建CreateVMware.txt文件,将需要SDP执行的命令,放到此文件中。
将该txt文件放在SDP安装目录/integration/custom_scripts/executor_files目录下
将命令中的脚本文件放在SDP安装目录/integration/custom_scripts目录下
作为测试,脚本内容供参考。
脚本执行的动作:
1、复制JSON临时文件至脚本所在目录(文件名test.txt)以供参考
2、将请求中的操作系统和系统配置字段的信息获取并输出
import sys
import json
#获取参数 $COMPLETE_V3_JSON_FILE
param=sys.argv[1]
#打开参数对应的JSON临时文件
file1 = open(param,'r', encoding="utf-8")
#将临时文件复制一份供参考
txt=file1.readlines()
file2 = open("./test.txt",'w', encoding="utf-8")
file2.write(str(txt))
#以JSON格式读取请求详细信息
RequestData=json.load(open(param, 'r', encoding="utf-8"))
#获取字段的信息其中的udf_pick_99和udf_pick_100需要根据您环境中自定义字段对应的API字段修改
VMsystem=RequestData['approval_level']['request']['udf_fields']['udf_pick_99']
VMConfig=RequestData['approval_level']['request']['udf_fields']['udf_pick_100']
#测试输出
print("VMwaretest executed successful,the Operation system"+VMsystem+",configuration"+VMConfig)
注:脚本的执行情况,以及print输出会记录在工单的历史中。汉字会有乱码的情况,建议输出使用英语
三、测试
用户创建工单,选择所需的虚拟机配置
审批人执行审批操作
通过历史记录,可以看到脚本执行成功,并正确获取参数
查看脚本目录,复制JSON文件成功。
JSON解析后如下:
{
"PORTALID":"1",
"approval_level":{
"request":{
"ola_due_by_time":null,
"subject":"虚拟机申请,请协助处理",
"service_sla":null,
"resolution":{
"resolution_attachments":[
],
"content":null
},
"onhold_time":null,
"has_purchase_requests":false,
"is_read":false,
"lifecycle":null,
"reason_for_cancel":null,
"assets":[
],
"is_trashed":false,
"id":"6",
"assigned_time":null,
"group":null,
"requester":{
"email_id":null,
"phone":"8888",
"name":"Guest",
"mobile":"1234567890",
"profile_pic":{
"content-url":"/images/default-profile-pic2.svg"
},
"is_vipuser":false,
"id":"4",
"department":null
},
"cancel_requested_by":null,
"email_to":[
],
"sla_violated_technician":null,
"created_time":{
"display_value":"Jul 12, 2023 03:53 PM",
"value":"1689148410088"
},
"has_resolution_attachments":false,
"approval_status":{
"name":"Pending Approval",
"id":"1"
},
"service_category":{
"name":"Data Management",
"id":"3",
"ciid":"3"
},
"sla":null,
"priority":null,
"created_by":{
"email_id":null,
"phone":"1234455",
"name":"administrator",
"mobile":"1234567890",
"profile_pic":{
"content-url":"/images/default-profile-pic2.svg"
},
"is_vipuser":false,
"id":"5",
"department":null
},
"sla_violated_group":null,
"tags":[
],
"scheduled_end_time":null,
"first_response_due_by_time":null,
"last_updated_time":{
"display_value":"Jul 12, 2023 03:53 PM",
"value":"1689148411326"
},
"has_notes":false,
"is_current_ola_violated":null,
"image_token":"7f9ed836caaebb58202e0f004016168c2d2e971239184bc6b115b3ea0482591180b1370d5322ba1b1738df84967b036be613703a",
"udf_fields":{
"udf_pick_99":"Ubuntu 20.08",
"udf_pick_100":"16core&32GB"
},
"email_cc":[
],
"status":{
"color":"#0066ff",
"name":"Open",
"id":"2"
},
"scheduled_start_time":null,
"template":{
"is_service_template":true,
"service_category":{
"id":"3"
},
"name":"申请虚拟机",
"id":"63"
},
"primary_asset":null,
"cancel_requested_time":null,
"notification_status":null,
"has_purchase_orders":false,
"description":null,
"has_dependency":false,
"has_conversation":false,
"chat_type":"0",
"callback_url":null,
"is_service_request":true,
"is_shared":false,
"cancel_requested":false,
"has_request_initiated_change":false,
"request_template_task_ids":[
],
"department":null,
"is_reopened":false,
"has_draft":false,
"has_attachments":false,
"has_linked_requests":false,
"resources":{
},
"has_request_caused_by_change":false,
"is_overdue":false,
"technician":null,
"due_by_time":null,
"has_project":false,
"is_first_response_overdue":false,
"cancel_requested_is_pending":false,
"recommend_template":null,
"unreplied_count":null,
"maintenance":null
},
"comments":"根据审批规则审批的等级。",
"change_stage":null,
"level":"1",
"associated_entity":"request",
"purchase_request":null,
"release":null,
"action_taken_by":{
"email_id":null,
"phone":null,
"name":"System",
"mobile":null,
"profile_pic":null,
"is_vipuser":false,
"id":"1",
"department":null
},
"change":null,
"rule":{
"type":"template_configurations",
"value":"first_response"
},
"created_by":{
"email_id":null,
"phone":null,
"name":"System",
"mobile":null,
"profile_pic":null,
"is_vipuser":false,
"id":"1",
"department":null
},
"deleted":false,
"created_on":{
"display_value":"Jul 12, 2023 03:53 PM",
"value":"1689148410403"
},
"purchase_order":null,
"approvals":[
{
"approver":{
"email_id":null,
"phone":"1234455",
"name":"administrator",
"mobile":"1234567890",
"profile_pic":null,
"is_vipuser":false,
"id":"5",
"department":null
},
"comments":"123",
"approval_level":{
"request":{
"id":"6"
},
"change_stage":null,
"level":"1",
"purchase_order":null,
"purchase_request":null,
"release":null,
"change":null,
"name":"Level One",
"id":"4",
"status":{
"id":"2"
}
},
"sent_on":{
"display_value":"12/07/2023 03:53 PM",
"value":"1689148410905"
},
"org_role":{
"name":"CEO",
"id":"3",
"display_name":"$CEO$"
},
"action_by":{
"email_id":null,
"phone":"1234455",
"name":"administrator",
"mobile":"1234567890",
"profile_pic":null,
"is_vipuser":false,
"id":"5",
"department":null
},
"deleted":false,
"sent_by":{
"email_id":null,
"phone":null,
"name":"System",
"mobile":null,
"profile_pic":null,
"is_vipuser":false,
"id":"1",
"department":null
},
"id":"4",
"obo_approver":null,
"email":null,
"key":"9f1572a0-6804-4323-a70f-e1706506eb8b",
"status":{
"name":"Approved",
"id":"2"
},
"action_taken_on":{
"display_value":"12/07/2023 03:53 PM",
"value":"1689148429485"
}
}
],
"name":"Level One",
"id":"4",
"is_current":true,
"action_taken_on":{
"display_value":"Jul 12, 2023 03:53 PM",
"value":"1689148429530"
},
"status":{
"name":"Approved",
"id":"2"
}
},
"LOGGEDIN_USER_TYPE":"System",
"diff":{
"new":{
"comments":"根据审批规则审批的等级。",
"approvals":[
{
"approver":{
"email_id":null,
"phone":"1234455",
"name":"administrator",
"mobile":"1234567890",
"profile_pic":null,
"is_vipuser":false,
"id":"5",
"department":null
},
"comments":"123",
"approval_level":{
"request":{
"id":"6"
},
"change_stage":null,
"level":"1",
"purchase_order":null,
"purchase_request":null,
"release":null,
"change":null,
"name":"Level One",
"id":"4",
"status":{
"id":"2"
}
},
"sent_on":{
"display_value":"12/07/2023 03:53 PM",
"value":"1689148410905"
},
"org_role":{
"name":"CEO",
"id":"3",
"display_name":"$CEO$"
},
"action_by":{
"email_id":null,
"phone":"1234455",
"name":"administrator",
"mobile":"1234567890",
"profile_pic":null,
"is_vipuser":false,
"id":"5",
"department":null
},
"deleted":false,
"sent_by":{
"email_id":null,
"phone":null,
"name":"System",
"mobile":null,
"profile_pic":null,
"is_vipuser":false,
"id":"1",
"department":null
},
"id":"4",
"obo_approver":null,
"email":null,
"key":"9f1572a0-6804-4323-a70f-e1706506eb8b",
"status":{
"name":"Approved",
"id":"2"
},
"action_taken_on":{
"display_value":"12/07/2023 03:53 PM",
"value":"1689148429485"
}
}
],
"id":"4",
"status":{
"name":"Approved",
"id":"2"
},
"action_taken_on":{
"value":"1689148429530"
}
},
"old":{
"comments":null,
"approvals":[
{
"approver":{
"email_id":null,
"phone":"1234455",
"name":"administrator",
"mobile":"1234567890",
"profile_pic":null,
"is_vipuser":false,
"id":"5",
"department":null
},
"comments":"123",
"approval_level":{
"request":{
"id":"6"
},
"change_stage":null,
"level":1,
"purchase_order":null,
"purchase_request":null,
"release":null,
"change":null,
"name":"Level One",
"id":"4",
"status":{
"id":"1"
}
},
"sent_on":{
"display_value":"12/07/2023 03:53 PM",
"value":"1689148410905"
},
"org_role":{
"name":"CEO",
"id":"3",
"display_name":"$CEO$"
},
"action_by":{
"email_id":null,
"phone":"1234455",
"name":"administrator",
"mobile":"1234567890",
"profile_pic":null,
"is_vipuser":false,
"id":"5",
"department":null
},
"deleted":false,
"sent_by":{
"email_id":null,
"phone":null,
"name":"System",
"mobile":null,
"profile_pic":null,
"is_vipuser":false,
"id":"1",
"department":null
},
"id":"4",
"obo_approver":null,
"email":null,
"key":"9f1572a0-6804-4323-a70f-e1706506eb8b",
"status":{
"name":"Approved",
"id":"2"
},
"action_taken_on":{
"display_value":"12/07/2023 03:53 PM",
"value":"1689148429485"
}
}
],
"id":"4",
"status":{
"name":"Pending Approval",
"id":"1"
},
"action_taken_on":{
"value":"1689148410905"
}
}
},
"OPERATION_TYPE":"update",
"LOGGEDIN_USER_NAME":"-",
"LOGIN_NAME":"-",
"LOGIN_USER_ID":"-"
}