HornbillLogServiceRequest.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

<#PSScriptInfo
 
.VERSION 1.1.1
 
.GUID ea49320b-7470-4814-98ea-6522557820ab
 
.AUTHOR steve.goldthorpe@hornbill.com
 
.COMPANYNAME Hornbill
 
.TAGS hornbill powershell azure automation workflow runbook
 
.LICENSEURI https://wiki.hornbill.com/index.php/The_Hornbill_Community_License_(HCL)
 
.PROJECTURI https://github.com/hornbill/powershellHornbillAzureRunbooks
 
.ICONURI https://wiki.hornbill.com/skins/common/images/HBLOGO.png
 
.RELEASENOTES
Corrected metadata
Included parameter descriptions
 
.DESCRIPTION
 Azure Automation Runbook to log a new Service Request within Service Manager on a Hornbill instance.
 
#>


#.PARAMETER instanceName
#MANDATORY: The name of the Instance to connect to.

#.PARAMETER instanceKey
#MANDATORY: An API key with permission on the Instance to carry out the required API calls.

#.PARAMETER assetIds
#A comma-seperated string of asset IDs to attach to the new request, for example: 1,12,36

#.PARAMETER bpmName
#The name of a BPM to override the Service BPM or Default BPM.

#.PARAMETER catalogName
#The title of the catalog to raise thew request against

#.PARAMETER categoryId
#The ID of the request category

#.PARAMETER categoryName
#The fullname of the request category

#.PARAMETER customerId
#The ID of the request customer

#.PARAMETER customerType
#The Type of the request customer (0 for Users, 1 for contacts)

#.PARAMETER description
#The request description

#.PARAMETER ownerId
#The ID of the request owner

#.PARAMETER priorityName
#The name of the request Priority

#.PARAMETER resolutionDetails
#The resolution description

#.PARAMETER serviceName
#The name of the service to raise the request against

#.PARAMETER siteName
#The name of the request site

#.PARAMETER sourceId
#The ID of the request source

#.PARAMETER sourceType
#The Type of request source

#.PARAMETER status
#The status of the new request (defaults to status.open)

#.PARAMETER summary
#The request summary

#.PARAMETER teamId
#The ID of the team that the request should be assigned to

#Requires -Module @{ModuleVersion = '1.1.0'; ModuleName = 'HornbillAPI'}
#Requires -Module @{ModuleVersion = '1.1.1'; ModuleName = 'HornbillHelpers'}

workflow Hornbill_RequestLogServiceRequest_Workflow
{
    # Define output stream type
    [OutputType([object])]

    # Define runbook input params
    Param
    (
        # Instance Connection Params
        [Parameter (Mandatory= $true)]
        [string] $instanceName,
        [Parameter (Mandatory= $true)]
        [string] $instanceKey,

        # API Params
        [string] $assetIds,
        [string] $bpmName,
        [string] $catalogName,
        [string] $categoryId,
        [string] $categoryName,
        [string] $customerId,
        [string] $customerType,
        [string] $description,
        [string] $ownerId,
        [string] $priorityName,
        [string] $resolutionDetails,
        [string] $serviceName,
        [string] $siteName,
        [string] $sourceId,
        [string] $sourceType,
        [string] $status,
        [string] $summary,
        [string] $teamId
    )

    # Define instance details
    Set-HB-Instance -Instance $instanceName -Key $instanceKey

    # Get service ID from Name
    $serviceId = ""
    if($serviceName -and $serviceName -ne ""){
        $serviceObj = Get-HB-ServiceID $serviceName
        if($serviceObj.ServiceID -gt 0) {
            $serviceId = $serviceObj.ServiceID
        }
    }

    # Get Priority ID from Name
    $priorityId = ""
    if($priorityName -and $priorityName -ne ""){
        $priorityObj = Get-HB-PriorityID $priorityName
        if($priorityObj.PriorityID -gt 0) {
            $priorityId = $priorityObj.PriorityID
        }
    }

    # Get Catalog ItemID from Name
    $catItemId = ""
    if($catalogName -and $catalogName -ne ""){
        $catalogObj = Get-HB-CatalogID $catalogName $serviceName "Service Request"
        if($catalogObj.CatalogID -gt 0) {
            $catItemId = $catalogObj.CatalogID
        }
    }

    # Get SiteID from Name
    $siteId = ""
    if($requestObject.siteName -and $requestObject.siteName -ne "") {
        $siteObj = Get-HB-SiteID $requestObject.siteName
        $siteId = $siteObj.SiteID
    }

    # Populate status if null
    if(-not $status -or $status -eq "") {
        $status = "status.open"
    }

    Add-HB-Param "summary" $summary $false
    Add-HB-Param "description" $description $false
    Add-HB-Param "customerId" $customerId $false
    Add-HB-Param "customerType" $customerType $false
    Add-HB-Param "ownerId" $ownerId $false
    Add-HB-Param "teamId" $teamId $false
    Add-HB-Param "status" $status $false
    Add-HB-Param "priorityId" $priorityId $false
    Add-HB-Param "categoryId" $categoryId $false
    Add-HB-Param "categoryName" $categoryName $false
    Add-HB-Param "sourceType" $sourceType $false
    Add-HB-Param "sourceId" $sourceId $false

    # Process Assets
    if($assetIds){
        $arrAssets = $assetIds.replace(' ','').split(',')
        foreach ($asset in $arrAssets) {
            Add-HB-Param "assetId" $asset $false
        }
    }

    Add-HB-Param "serviceId" $serviceId $false
    Add-HB-Param "resolutionDetails" $resolutionDetails $false
    Add-HB-Param "siteId" $siteId $false
    Add-HB-Param "siteName" $siteName $false
    Add-HB-Param "catalogId" $catItemId $false
    Add-HB-Param "catalogName" $catalogName $false
    Add-HB-Param "bpmName" $bpmName $false

    # Invoke XMLMC call, output returned as PSObject
    $xmlmcOutput = Invoke-HB-XMLMC "apps/com.hornbill.servicemanager/ServiceRequests" "logServiceRequest"
    $exceptionName = ""
    $exceptionSummary = ""
    # Read output status
    if($xmlmcOutput.status -eq "ok") {
        if($xmlmcOutput.params.requestId -and $xmlmcOutput.params.requestId -ne ""){
            $requestRef = $xmlmcOutput.params.requestId
            $requestWarnings = $xmlmcOutput.params.warnings
        }
        if($xmlmcOutput.params.exceptionName -and $xmlmcOutput.params.exceptionName -ne ""){
            $exceptionName = $xmlmcOutput.params.exceptionName
            $exceptionSummary = $xmlmcOutput.params.exceptionDescription
        }
    }
    # Build resultObject to write to output
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        RequestRef = $requestRef
        Warnings = $requestWarnings
        ExceptionName = $exceptionName
        ExceptionSummary = $exceptionSummary
    }

    if($resultObject.Status -ne "ok"){
        Write-Error $resultObject
    } else {
        if($exceptionName -ne ""){
            Write-Warning $resultObject
        } else {
            Write-Output $resultObject
        }
    }
}