HornbillHelpers.psm1

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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
##############################
# Hornbill Helpers Powershell Module
#
#.DESCRIPTION
# This module includes helper functions to retrieve useful information from your Hornbill instance, for use within your Azure Runbooks.
#
# Requires Powershell 3.0 or above, and the HornbillAPI Module to be installed on your Azure Automation Account
#
#.NOTES
# See example scripts and function documentation for guidance on usage.
##############################

##############################
#.SYNOPSIS
# Get the Primary Key value of a Service
#
#.DESCRIPTION
# Provide a Service Name, and this CMDLET will search your Hornbill instance for a matching record, returning the Primary Key
#
#.PARAMETER ServiceName
# The Service Name to search for
#
#.EXAMPLE
# Get-HB-ServiceID "Desktop Support" will search your services and return the primary key for the default language record
# of the service "Desktop Support"
#
##############################
function Get-HB-ServiceID {
    Param(
        [Parameter(Mandatory=$True, HelpMessage="Provide the Service Name to retrieve its ID")]
        [ValidateNotNullOrEmpty()]
            [string]$ServiceName
    )

    Add-HB-Param       "application" "com.hornbill.servicemanager"
    Add-HB-Param       "queryName" "getServicesByField"
    Add-HB-Param       "maxResults" "1"
    Open-HB-Element    "queryParams"
    Add-HB-Param       "filter" $ServiceName
    Close-HB-Element   "queryParams"
    Open-HB-Element    "queryOptions"
    Add-HB-Param       "h_servicename" "true"
    Close-HB-Element   "queryOptions"
    Open-HB-Element    "queryOrder"
    Add-HB-Param       "column" "h_pk_serviceid"
    Add-HB-Param       "direction" "ascending"
    Close-HB-Element   "queryOrder"

    # Invoke XMLMC call, output returned as PSObject type
    $xmlmcOutput = Invoke-HB-XMLMC "data" "queryExec"
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        ServiceID = ""
        ExceptionName = ""
        ExceptionSummary = ""
    }
    if($xmlmcOutput.status -eq "ok"){
        if(!$xmlmcOutput.params.rowData) {
            $resultObject.ExceptionName = "NoRecordsFound"
            $resultObject.ExceptionSummary = "No matching Service records found"
        } else {
            if($xmlmcOutput.params.rowData.row.h_servicename -ne $ServiceName) {
                $resultObject.ExceptionName = "Incorrect Record Returned"
                $resultObject.ExceptionSummary = "Service Name queried ["+$ServiceName+"] does not match Service returned ["+$xmlmcOutput.params.rowData.row.h_servicename +"]"
            } else {
                $resultObject.ServiceID = $xmlmcOutput.params.rowData.row.h_pk_serviceid
            }
        }

    }
    return $resultObject
}

##############################
#.SYNOPSIS
# Get the Primary Key value of a Catalog Item
#
#.DESCRIPTION
# Provide a Catalog Item Name, and this CMDLET will search your Hornbill instance for a matching record, returning the Primary Key
#
#.PARAMETER CatalogItem
# The Catalog Item Name to search for
#
#.PARAMETER ServiceName
# The Service that the Catalog Item belogs to
#
#.PARAMETER RequestType
# The Request Type that the Catalog Item is configured for
#
#.EXAMPLE
# Get-HB-CatalogID "Support Me" "Desktop Support" "Incident" will search your services and return the primary key of
# the Catalog Item "Support Me", within the "Desktop Support" Service ans request type "Incident"
#
##############################
function Get-HB-CatalogID {
    Param(
        [Parameter(Mandatory=$True, HelpMessage="Provide the Catalog Item Name to retrieve its ID")]
        [ValidateNotNullOrEmpty()]
            [string]$CatalogItem,
        [Parameter(Mandatory=$True, HelpMessage="Provide the Service Name to which the Catalog Item is associated")]
        [ValidateNotNullOrEmpty()]
            [string]$ServiceName,
        [Parameter(Mandatory=$True, HelpMessage="Provide the Request Type to which the Catalog Item is associated")]
        [ValidateNotNullOrEmpty()]
            [string]$RequestType
    )

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

    Add-HB-Param       "application" "com.hornbill.servicemanager"
    Add-HB-Param       "entity" "Catalogs"
    Open-HB-Element    "searchFilter"
    Add-HB-Param       "h_catalog_title" $CatalogItem
    Add-HB-Param       "h_service_id" $serviceId
    Add-HB-Param       "h_request_type" $RequestType
    Close-HB-Element   "searchFilter"
    Add-HB-Param       "maxResults" "1"

    # Invoke XMLMC call, output returned as PSObject type
    $xmlmcOutput = Invoke-HB-XMLMC "data" "entityBrowseRecords"
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        CatalogID = ""
        ExceptionName = ""
        ExceptionSummary = ""
    }
    if($xmlmcOutput.status -eq "ok"){
        if(!$xmlmcOutput.params.rowData) {
            $resultObject.ExceptionName = "NoRecordsFound"
            $resultObject.ExceptionSummary = "No matching Catalog Item records found"
        } else {
            if($xmlmcOutput.params.rowData.row.h_catalog_title -ne $CatalogItem) {
                $resultObject.ExceptionName = "Incorrect Record Returned"
                $resultObject.ExceptionSummary = "Catalog Item Name queried ["+$CatalogItem+"] does not match Catalog Item returned ["+$xmlmcOutput.params.rowData.row.h_catalog_title +"]"
            } else {
                $resultObject.CatalogID = $xmlmcOutput.params.rowData.row.h_request_catalog_id
            }
        }

    }
    return $resultObject
}

##############################
#.SYNOPSIS
# Get the Primary Key value of an Organisation
#
#.DESCRIPTION
# Provide a Organisation Name, and this CMDLET will search your Hornbill instance for a matching record, returning the Primary Key
#
#.PARAMETER OrgName
# The Organisation Name to search for
#
#.EXAMPLE
# Get-HB-OrganisationID "Support Me" will search your Organisations and return the primary key of the record found
#
##############################
function Get-HB-OrganisationID {
    Param(
        [Parameter(Mandatory=$True, HelpMessage="Provide the Organisation Name to retrieve its ID")]
        [ValidateNotNullOrEmpty()]
            [string]$OrganisationName
    )

    Add-HB-Param       "application" "com.hornbill.core"
    Add-HB-Param       "entity" "Organizations"
    Open-HB-Element    "searchFilter"
    Add-HB-Param       "h_organization_name" $OrganisationName
    Close-HB-Element   "searchFilter"
    Add-HB-Param       "maxResults" "1"

    # Invoke XMLMC call, output returned as PSObject type
    $xmlmcOutput = Invoke-HB-XMLMC "data" "entityBrowseRecords"
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        OrganisationID = ""
        ExceptionName = ""
        ExceptionSummary = ""
    }
    if($xmlmcOutput.status -eq "ok"){
        if(!$xmlmcOutput.params.rowData) {
            $resultObject.ExceptionName = "NoRecordsFound"
            $resultObject.ExceptionSummary = "No matching Organisation records found"
        } else {
            if($xmlmcOutput.params.rowData.row.h_organization_name -ne $OrganisationName) {
                $resultObject.ExceptionName = "Incorrect Record Returned"
                $resultObject.ExceptionSummary = "Organisation Name queried ["+$OrganisationName+"] does not match Organisation returned ["+$xmlmcOutput.params.rowData.row.h_organization_name +"]"
            } else {
                $resultObject.OrganisationID = $xmlmcOutput.params.rowData.row.h_organization_id
            }
        }

    }
    return $resultObject
}

##############################
#.SYNOPSIS
# Get the Primary Key value of a Priority
#
#.DESCRIPTION
# Provide a Priority Name, and this CMDLET will search your Hornbill instance for a matching record, returning the Primary Key
#
#.PARAMETER PriorityName
# The Priority Name to search for
#
#.EXAMPLE
# Get-HB-PriorityID "Low" will search your priorities and return the primary key for "Low" priority
#
##############################
function Get-HB-PriorityID {
    Param(
        [Parameter(Mandatory=$True, HelpMessage="Provide the Priority Name to retrieve its ID")]
        [ValidateNotNullOrEmpty()]
            [string]$PriorityName
    )

    Add-HB-Param       "application" "com.hornbill.servicemanager"
    Add-HB-Param       "entity" "Priority"
    Open-HB-Element    "searchFilter"
    Add-HB-Param       "h_priorityname" $PriorityName
    Close-HB-Element   "searchFilter"
    Add-HB-Param       "maxResults" "1"
    
    # Invoke XMLMC call, output returned as PSObject type
    $xmlmcOutput = Invoke-HB-XMLMC "data" "entityBrowseRecords"
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        PriorityID = ""
        ExceptionName = ""
        ExceptionSummary = ""
    }
    if($xmlmcOutput.status -eq "ok"){
        if(!$xmlmcOutput.params.rowData) {
            $resultObject.ExceptionName = "NoRecordsFound"
            $resultObject.ExceptionSummary = "No matching Priority records found"
        } else {
            if($xmlmcOutput.params.rowData.row.h_priorityname -ne $PriorityName) {
                $resultObject.ExceptionName = "Incorrect Record Returned"
                $resultObject.ExceptionSummary = "Priority Name queried ["+$PriorityName+"] does not match Priority returned ["+$xmlmcOutput.params.rowData.row.h_priorityname +"]"
            } else {
                $resultObject.PriorityID = $xmlmcOutput.params.rowData.row.h_pk_priorityid
            }
        }

    }
    return $resultObject
}

##############################
#.SYNOPSIS
# Get the Primary Key value of a Site
#
#.DESCRIPTION
# Provide a Site Name, and this CMDLET will search your Hornbill instance for a matching record, returning the Primary Key
#
#.PARAMETER SiteName
# The Site Name to search for
#
#.EXAMPLE
# Get-HB-SiteID "Hull" will search your sites and return the primary key for the "Hull" site
#
##############################
function Get-HB-SiteID {
    Param(
        [Parameter(Mandatory=$True, HelpMessage="Provide the Site Name to retrieve its ID")]
        [ValidateNotNullOrEmpty()]
            [string]$SiteName
    )

    Add-HB-Param        "application" "com.hornbill.core"
    Add-HB-Param        "entity" "Site"
    Open-HB-Element     "searchFilter"
    Add-HB-Param        "h_site_name" $SiteName
    Close-HB-Element    "searchFilter"
    Open-HB-Element     "orderBy"
    Add-HB-Param        "column" "h_site_name"
    Add-HB-Param        "direction" "ascending"
    Close-HB-Element    "orderBy"
    Add-HB-Param        "maxResults" "1"
    
    # Invoke XMLMC call, output returned as PSObject type
    $xmlmcOutput = Invoke-HB-XMLMC "data" "entityBrowseRecords"
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        SiteID = ""
        ExceptionName = ""
        ExceptionSummary = ""
    }
    if($xmlmcOutput.status -eq "ok"){
        if(!$xmlmcOutput.params.rowData) {
            $resultObject.ExceptionName = "NoRecordsFound"
            $resultObject.ExceptionSummary = "No matching Site records found"
        } else {
            if($xmlmcOutput.params.rowData.row.h_site_name -ne $SiteName) {
                $resultObject.ExceptionName = "Incorrect Record Returned"
                $resultObject.ExceptionSummary = "Site Name queried ["+$SiteName+"] does not match Site returned ["+$xmlmcOutput.params.rowData.row.h_site_name +"]"
            } else {
                $resultObject.SiteID = $xmlmcOutput.params.rowData.row.h_id
            }
        }

    }
    return $resultObject
}

##############################
#.SYNOPSIS
# Get the Activity Stream ID of a Workspace
#
#.DESCRIPTION
# Provide a Workspace Name, and this CMDLET will search your Hornbill instance for a matching record, returning the Activity Stream ID
#
#.PARAMETER WorkspaceName
# The Workspace Name to search for
#
#.EXAMPLE
# Get-HB-WorkspaceID "Demo Workspace" will search your Workspaces and return the Activity Stream ID for the "Demo Workspace" workspace
#
##############################
function Get-HB-WorkspaceID {
    Param(
        [Parameter(Mandatory=$True, HelpMessage="Provide the Workspace Name to retrieve its Activity Stream ID")]
        [ValidateNotNullOrEmpty()]
            [string]$WorkspaceName
    )

    Add-HB-Param "displayName" $WorkspaceName
    
    # Invoke XMLMC call, output returned as PSObject type
    $xmlmcOutput = Invoke-HB-XMLMC "activity" "workspaceLookup"
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        ActivityStreamID = ""
    }

    if($xmlmcOutput.status -eq "ok"){
        $resultObject.ActivityStreamID = $xmlmcOutput.params.activityStreamID   
    }
    return $resultObject
}


##############################
#.SYNOPSIS
# Retrieves request details
#
#.DESCRIPTION
# Retrieves details about a Request
#
#.PARAMETER RequestRef
# The Request reference
#
#.EXAMPLE
# Get-HB-Request "IN00000123"
#
###############################
function Get-HB-Request {
    Param(
        [Parameter(Mandatory=$True, HelpMessage="Provide the Request Reference to retrieve its details")]
        [ValidateNotNullOrEmpty()]
            [string]$RequestRef
    )

    Add-HB-Param "requestId" $RequestRef
    
    # Invoke XMLMC call, output returned as PSObject type
    $xmlmcOutput = Invoke-HB-XMLMC "apps/com.hornbill.servicemanager/Requests" "smGetDetails"
    $resultObject = New-Object PSObject -Property @{
        Status           = $xmlmcOutput.status
        Error            = $xmlmcOutput.error
        RequestDetails   = ""
        RequestRelated   = ""
        RequestBPM       = ""
        ExceptionName    = ""
        ExceptionSummary = ""
    }
    if($xmlmcOutput.status -eq "ok"){
        if(!$xmlmcOutput.params) {
            $resultObject.ExceptionName = "NoRecordsFound"
            $resultObject.ExceptionSummary = "No matching Request record found"
        } else {
            $resultObject.RequestDetails = $xmlmcOutput.params.requestDetails
            if($xmlmcOutput.params.requestRelatedDetails){
                $resultObject.RequestRelated = $xmlmcOutput.params.requestRelatedDetails
            }
            if($xmlmcOutput.params.requestBPM){
                $resultObject.RequestBPM = $xmlmcOutput.params.requestBPM
            }
        }

    }
    return $resultObject
}

##############################
#.SYNOPSIS
# Get the details of a Hornbill User
#
#.DESCRIPTION
# Provide a User Name, and this CMDLET will search your Hornbill instance for a matching record, returning details
#
#.PARAMETER UserName
# The User Name to search for
#
#.EXAMPLE
# Get-HB-User "some@ad.username.com" will search your Users and return the user object for the "some@ad.username.com" user
#
##############################
function Get-HB-User {
    Param(
        [Parameter(Mandatory=$True, HelpMessage="Provide the User ID to retrieve its details")]
        [ValidateNotNullOrEmpty()]
            [string]$UserName
    )
    # Build XMLMC call
    Add-HB-Param    "userId" $UserName
    # Invoke XMLMC call, output returned as PSObject type
    $xmlmcOutput = Invoke-HB-XMLMC "admin" "userGetInfo"
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
    }
    if($xmlmcOutput.status -eq "ok"){
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "UserType" -Value $xmlmcOutput.params.userType
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "Name" -Value $xmlmcOutput.params.name
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "FirstName" -Value $xmlmcOutput.params.firstName
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "JobTitle" -Value $xmlmcOutput.params.jobTitle
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "HomeOrganization" -Value $xmlmcOutput.params.homeOrganization
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "Site" -Value $xmlmcOutput.params.site
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "Phone" -Value $xmlmcOutput.params.phone
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "Email" -Value $xmlmcOutput.params.email
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "Mobile" -Value $xmlmcOutput.params.mobile
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "AvailabilityStatus" -Value $xmlmcOutput.params.availabilityStatus
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "LastLogonTime" -Value $xmlmcOutput.params.lastLogonTime
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "OnlineStatus" -Value $xmlmcOutput.params.onlineStatus
        Add-Member -InputObject $resultObject -MemberType NoteProperty -Name "AccountStatus" -Value $xmlmcOutput.params.accountStatus
    }
    return $resultObject
}

##############################
#.SYNOPSIS
# Get the ID of a Hornbill asset
#
#.DESCRIPTION
# Provide a string to query, the entity to search against, and the column to search within, and this CMDLET will search your Hornbill instance
# for a matching asset, returning its primary key
#
#.PARAMETER Value
# The Value to search for
#
#.PARAMETER Entity
# The Entity to search within
#
#.PARAMETER Column
# The Column to search within
#
#.EXAMPLE
# Get-HB-AssetID "someserialnumber" "AssetsMobileDevice" "h_serial_number" will search your Hornbill Mobile Assets and
# return the ID for asset with "someserialnumber" in the serial number field
#
##############################
function Get-HB-AssetID {
    Param(
        [Parameter(Mandatory=$True, HelpMessage="Provide the value to search against")]
        [ValidateNotNullOrEmpty()]
            [string]$Value, 

        [Parameter(Mandatory=$True, HelpMessage="Provide the asset entity name to search")]
        [ValidateNotNullOrEmpty()]
            [string]$Entity, 

        [Parameter(Mandatory=$True, HelpMessage="Provide the asset entity column to search")]
        [ValidateNotNullOrEmpty()]
            [string]$Column
    )
    # Build XMLMC call
    Add-HB-Param        "application" "com.hornbill.servicemanager"
    Add-HB-Param        "entity" $Entity
    Open-HB-Element     "searchFilter"
    Add-HB-Param        "column" $Column
    Add-HB-Param        "value" $Value
    Add-HB-Param        "matchType" "exact"
    Close-HB-Element    "searchFilter"
    Add-HB-Param        "maxResults" "1"

    # Invoke XMLMC call, output returned as PSObject type
    $xmlmcOutput = Invoke-HB-XMLMC "data" "entityBrowseRecords2"
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        AssetID = ""
    }
    if($xmlmcOutput.status -eq "ok"){
        $resultObject.AssetID = $xmlmcOutput.params.RowData.Row.h_pk_asset_id
    }
    return $resultObject
}

# Export the functions available to the script importing this module
Export-ModuleMember -Function '*'