Trigger for ManageEngine Servicedesk Plus. example
Триггер для ManageEngine Servicedesk Plus. пример
SD trigger string:
powershell -file c:\scripts\cancelrequest.ps1 отклонена $WORKORDERID
code:
#powershell script for manageengine SDP trigger
#by *me
Param ([string] $txt, [string]$WORKORDERID)
$TECHNICIAN_KEY='XXXXXX-XXXXXXX-XXXXXX-XXXXXX'
$SDEMAIL="sd@domain.com"
$SUBJECT = "Ваша заявка ##{0}## была {1}" -f $WORKORDERID, $txt
$LOGFILE="c:\scripts\cancelrequest.log"
$MAILSERVER="mail.domain.com"
$SDHOST="https://localhost"
#errorcodes
$REQUESTERNOTFOUNDERROR=-101
function sendrequest($uri)
{
$response = Invoke-RestMethod -Uri $uri
if($response.operation.result.status -ne "Success"){
$message = "function {0} returns non success result:`n{1}" -f $MyInvocation.MyCommand.Name, $response.operation.result
throw $message}
return $response
}
function getrequest([string]$id)
{
$uri=$SDHOST+'/sdpapi/request/'+$id+'?OPERATION_NAME=GET_REQUEST&TECHNICIAN_KEY='+$TECHNICIAN_KEY+'&format=json'
Invoke-RestMethod -Uri $uri -Method Get
return $response
}
function getresolution([string]$id)
{
$uri=$SDHOST+'/sdpapi/request/'+$id+'?OPERATION_NAME=GET_RESOLUTION&TECHNICIAN_KEY='+$TECHNICIAN_KEY+'&format=json'
$response = sendrequest($uri)
return $response.operation.Details.RESOLUTION
}
function getrequester($r)
{
$uri=$SDHOST+'/sdpapi/requester?OPERATION_NAME=GET_ALL&TECHNICIAN_KEY='+$TECHNICIAN_KEY+'&INPUT_DATA={"operation":{"details":{"name":"'+$r+'"}}}&format=json'
#log("uri="+$uri)
$response = sendrequest($uri)
return $response.operation.details
}
function log([string]$txt)
{
add-content $LOGFILE $txt
}
function main(){
log(Get-Date -format "dd.MM.yyyy H:m")
log("WORKORDERID="+$WORKORDERID)
$request = getrequest($WORKORDERID)
$REQUESTER = $request.REQUESTER
log("REQUESTER="+$REQUESTER)
$requesterdata=getrequester($REQUESTER)
if(![boolean]$requester){
log("Requester not found. Abnormal oO")
return $REQUESTERNOTFOUNDERROR}
$to = @($requesterdata.emailid)[0]
$from = $SDEMAIL
$body = getresolution($WORKORDERID)
log("Sending mail to:"+$to)
log("="*40)
$encoding = [System.Text.Encoding]::UTF8
send-MailMessage -SmtpServer $MAILSERVER -To $to -From $from -Subject $SUBJECT -Body $body -BodyAsHtml -Encoding $encoding
}
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class IDontCarePolicy : ICertificatePolicy {
public IDontCarePolicy() {}
public bool CheckValidationResult(
ServicePoint sPoint, X509Certificate cert,
WebRequest wRequest, int certProb) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy
try{
main}
catch{
$ErrorMessage = $_.Exception.Message
log("Exception detected!`n"+"Error message:"+$ErrorMessage+"`n"+'='*40)
return -1}
Триггер для ManageEngine Servicedesk Plus. пример
SD trigger string:
powershell -file c:\scripts\cancelrequest.ps1 отклонена $WORKORDERID
code:
#powershell script for manageengine SDP trigger
#by *me
Param ([string] $txt, [string]$WORKORDERID)
$TECHNICIAN_KEY='XXXXXX-XXXXXXX-XXXXXX-XXXXXX'
$SDEMAIL="sd@domain.com"
$SUBJECT = "Ваша заявка ##{0}## была {1}" -f $WORKORDERID, $txt
$LOGFILE="c:\scripts\cancelrequest.log"
$MAILSERVER="mail.domain.com"
$SDHOST="https://localhost"
#errorcodes
$REQUESTERNOTFOUNDERROR=-101
function sendrequest($uri)
{
$response = Invoke-RestMethod -Uri $uri
if($response.operation.result.status -ne "Success"){
$message = "function {0} returns non success result:`n{1}" -f $MyInvocation.MyCommand.Name, $response.operation.result
throw $message}
return $response
}
function getrequest([string]$id)
{
$uri=$SDHOST+'/sdpapi/request/'+$id+'?OPERATION_NAME=GET_REQUEST&TECHNICIAN_KEY='+$TECHNICIAN_KEY+'&format=json'
Invoke-RestMethod -Uri $uri -Method Get
return $response
}
function getresolution([string]$id)
{
$uri=$SDHOST+'/sdpapi/request/'+$id+'?OPERATION_NAME=GET_RESOLUTION&TECHNICIAN_KEY='+$TECHNICIAN_KEY+'&format=json'
$response = sendrequest($uri)
return $response.operation.Details.RESOLUTION
}
function getrequester($r)
{
$uri=$SDHOST+'/sdpapi/requester?OPERATION_NAME=GET_ALL&TECHNICIAN_KEY='+$TECHNICIAN_KEY+'&INPUT_DATA={"operation":{"details":{"name":"'+$r+'"}}}&format=json'
#log("uri="+$uri)
$response = sendrequest($uri)
return $response.operation.details
}
function log([string]$txt)
{
add-content $LOGFILE $txt
}
function main(){
log(Get-Date -format "dd.MM.yyyy H:m")
log("WORKORDERID="+$WORKORDERID)
$request = getrequest($WORKORDERID)
$REQUESTER = $request.REQUESTER
log("REQUESTER="+$REQUESTER)
$requesterdata=getrequester($REQUESTER)
if(![boolean]$requester){
log("Requester not found. Abnormal oO")
return $REQUESTERNOTFOUNDERROR}
$to = @($requesterdata.emailid)[0]
$from = $SDEMAIL
$body = getresolution($WORKORDERID)
log("Sending mail to:"+$to)
log("="*40)
$encoding = [System.Text.Encoding]::UTF8
send-MailMessage -SmtpServer $MAILSERVER -To $to -From $from -Subject $SUBJECT -Body $body -BodyAsHtml -Encoding $encoding
}
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class IDontCarePolicy : ICertificatePolicy {
public IDontCarePolicy() {}
public bool CheckValidationResult(
ServicePoint sPoint, X509Certificate cert,
WebRequest wRequest, int certProb) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy
try{
main}
catch{
$ErrorMessage = $_.Exception.Message
log("Exception detected!`n"+"Error message:"+$ErrorMessage+"`n"+'='*40)
return -1}
Комментариев нет:
Отправить комментарий