Skip to content

Commit cbfee49

Browse files
committed
new ci
1 parent 0f81be8 commit cbfee49

File tree

4 files changed

+69
-39
lines changed

4 files changed

+69
-39
lines changed

ServiceNow/Public/New-ServiceNowConfigurationItem.ps1

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
<#
22
.SYNOPSIS
3-
Generates a new configuration item
3+
Create a new configuration item
44
55
.DESCRIPTION
6-
Generates a new ServiceNow Incident using predefined or custom fields by invoking the ServiceNow API
6+
Create a new configuration item. You can create a specific class ci or root cmdb_ci.
77
88
.PARAMETER Name
99
Name of the ci
10+
11+
.PARAMETER Class
12+
Specify the class of the CI, eg. cmdb_ci_server. If not specified, cmdb_ci will be used.
13+
14+
.PARAMETER Description
15+
Description for the CI
16+
17+
.PARAMETER OperationalStatus
18+
Operational status value of the CI. Note, this is the numerical value, not display value. Eg. Use '1', not 'Operational'.
19+
20+
.PARAMETER CustomField
21+
Key/value pairs for fields not available as a function parameter, eg. @{'ip_address'='1.2.3.4'}
22+
23+
.PARAMETER PassThru
24+
Return the newly created CI
25+
26+
.PARAMETER Connection
27+
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
28+
29+
.PARAMETER ServiceNowSession
30+
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
1031
1132
.EXAMPLE
12-
Generate a basic Incident attributed to the caller "UserName" with descriptions, categories, assignment groups and CMDB items set.
13-
New-ServiceNowIncident -Caller "UserName" -ShortDescription = "New PS Incident" -Description = "This incident was created from Powershell" -AssignmentGroup "ServiceDesk" -Comment "Inline Comment" -Category "Office" -Subcategory "Outlook" -ConfigurationItem UserPC1
33+
New-ServiceNowConfigurationItem -Name 'MyServer' -Class cmdb_ci_server
34+
Create a new CI
1435
1536
.EXAMPLE
37+
New-ServiceNowConfigurationItem -Name 'MyServer' -Class cmdb_ci_server -PassThru
38+
Create a new CI and return the newly created object to the pipeline
1639
#>
1740
function New-ServiceNowConfigurationItem {
1841

@@ -23,7 +46,7 @@ function New-ServiceNowConfigurationItem {
2346
[parameter(Mandatory)]
2447
[string] $Name,
2548

26-
[parameter(Mandatory)]
49+
[parameter()]
2750
[string] $Class,
2851

2952
[parameter()]
@@ -32,18 +55,12 @@ function New-ServiceNowConfigurationItem {
3255
[parameter()]
3356
[string] $OperationalStatus,
3457

35-
[parameter()]
36-
[string] $Environment,
37-
38-
[parameter()]
39-
[string] $FQDN,
40-
41-
[parameter()]
42-
[ipaddress] $IpAddress,
43-
4458
# custom fields as hashtable
4559
[parameter()]
46-
[hashtable] $CustomFields,
60+
[hashtable] $CustomField,
61+
62+
[Parameter()]
63+
[switch] $PassThru,
4764

4865
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
4966
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory)]
@@ -52,10 +69,7 @@ function New-ServiceNowConfigurationItem {
5269

5370
[Parameter(ParameterSetName = 'Session')]
5471
[ValidateNotNullOrEmpty()]
55-
[hashtable] $ServiceNowSession = $script:ServiceNowSession,
56-
57-
[Parameter()]
58-
[switch] $PassThru
72+
[hashtable] $ServiceNowSession = $script:ServiceNowSession
5973
)
6074

6175
begin {}
@@ -67,9 +81,6 @@ function New-ServiceNowConfigurationItem {
6781
'Class' = 'sys_class_name'
6882
'Description' = 'description'
6983
'OperationalStatus' = 'operational_status'
70-
'Environment' = 'environment'
71-
'FQDN' = ''
72-
'IpAddress' = ''
7384
}
7485
$tableEntryValues = @{}
7586
foreach ($key in $PSBoundParameters.Keys) {
@@ -79,14 +90,14 @@ function New-ServiceNowConfigurationItem {
7990
}
8091

8192
# Add CustomFields hash pairs to the Table Entry Values hash table
82-
$dupes = ForEach ($Key in $CustomFields.Keys) {
93+
$dupes = ForEach ($Key in $CustomField.Keys) {
8394
If ($TableEntryValues.ContainsKey($Key)) {
8495
# Capture the duplicate key name
8596
$Key
8697
}
8798
Else {
8899
# Add the unique entry to the table entry values hash table
89-
$TableEntryValues.Add($Key, $CustomFields[$Key])
100+
$TableEntryValues.Add($Key, $CustomField[$Key])
90101
}
91102
}
92103

@@ -97,16 +108,20 @@ function New-ServiceNowConfigurationItem {
97108

98109
# Table Entry Splat
99110
$params = @{
100-
Table = 'cmdb_ci'
101-
Values = $TableEntryValues
102-
Connection = $Connection
103-
ServiceNowSession = $ServiceNowSession
104-
PassThru = $true
111+
Table = 'cmdb_ci'
112+
Values = $TableEntryValues
113+
PassThru = $true
114+
}
115+
116+
if ($ServiceNowSession) {
117+
$params.ServiceNowSession = $ServiceNowSession
118+
}
119+
else {
120+
$params.Connection = $Connection
105121
}
106122

107123
If ( $PSCmdlet.ShouldProcess($Name, 'Create new configuration item') ) {
108124
$response = New-ServiceNowRecord @params
109-
# $response = Invoke-ServiceNowRestMethod @params
110125
If ($PassThru.IsPresent) {
111126
$response.PSObject.TypeNames.Insert(0, "ServiceNow.ConfigurationItem")
112127
$response

ServiceNow/Public/New-ServiceNowIncident.ps1

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Generate an Incident by "Splatting" all fields used in the 1st example plus some
2020
Category "Office"
2121
Subcategory "Outlook"
2222
ConfigurationItem UserPC1
23-
CustomFields = @{u_custom1 = "Custom Field Entry"
23+
CustomField = @{u_custom1 = "Custom Field Entry"
2424
u_another_custom = "Related Test"}
2525
}
2626
New-ServiceNowIncident @Params
@@ -66,7 +66,8 @@ function New-ServiceNowIncident {
6666

6767
# custom fields as hashtable
6868
[parameter()]
69-
[hashtable] $CustomFields,
69+
[Alias('CustomFields')]
70+
[hashtable] $CustomField,
7071

7172
# Credential used to authenticate to ServiceNow
7273
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory)]
@@ -95,7 +96,7 @@ function New-ServiceNowIncident {
9596
begin {}
9697

9798
process {
98-
# Create a hash table of any defined parameters (not CustomFields) that have values
99+
# Create a hash table of any defined parameters (not CustomField) that have values
99100
$DefinedIncidentParameters = @('AssignmentGroup', 'Caller', 'Category', 'Comment', 'ConfigurationItem', 'Description', 'ShortDescription', 'Subcategory')
100101
$TableEntryValues = @{}
101102
ForEach ($Parameter in $DefinedIncidentParameters) {
@@ -115,12 +116,12 @@ function New-ServiceNowIncident {
115116
}
116117
}
117118

118-
# Add CustomFields hash pairs to the Table Entry Values hash table
119-
If ($null -ne $PSBoundParameters.CustomFields) {
120-
$DuplicateTableEntryValues = ForEach ($Key in $CustomFields.Keys) {
119+
# Add CustomField hash pairs to the Table Entry Values hash table
120+
If ($null -ne $PSBoundParameters.CustomField) {
121+
$DuplicateTableEntryValues = ForEach ($Key in $CustomField.Keys) {
121122
If (($TableEntryValues.ContainsKey($Key) -eq $False)) {
122123
# Add the unique entry to the table entry values hash table
123-
$TableEntryValues.Add($Key, $CustomFields[$Key])
124+
$TableEntryValues.Add($Key, $CustomField[$Key])
124125
}
125126
Else {
126127
# Capture the duplicate key name

ServiceNow/ServiceNow.format.ps1xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,15 @@
199199
<TableHeaders>
200200
<TableColumnHeader>
201201
<Label>name</Label>
202-
<Width>60</Width>
202+
<Width>30</Width>
203+
</TableColumnHeader>
204+
<TableColumnHeader>
205+
<Label>sys_id</Label>
206+
<Width>32</Width>
207+
</TableColumnHeader>
208+
<TableColumnHeader>
209+
<Label>sys_class_name</Label>
210+
<Width>30</Width>
203211
</TableColumnHeader>
204212
<TableColumnHeader>
205213
<Label>category</Label>
@@ -216,6 +224,12 @@
216224
<TableColumnItem>
217225
<PropertyName>name</PropertyName>
218226
</TableColumnItem>
227+
<TableColumnItem>
228+
<PropertyName>sys_id</PropertyName>
229+
</TableColumnItem>
230+
<TableColumnItem>
231+
<PropertyName>sys_class_name</PropertyName>
232+
</TableColumnItem>
219233
<TableColumnItem>
220234
<PropertyName>category</PropertyName>
221235
</TableColumnItem>

ServiceNow/ServiceNow.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ FormatsToProcess = @('ServiceNow.format.ps1xml')
5959
NestedModules = @()
6060

6161
# Functions to export from this module
62-
FunctionsToExport = @('Get-ServiceNowRecord','New-ServiceNowSession','Add-ServiceNowAttachment','Get-ServiceNowAttachment','Get-ServiceNowAttachmentDetail','Get-ServiceNowChangeRequest','Get-ServiceNowConfigurationItem','Get-ServiceNowIncident','Get-ServiceNowRequest','Get-ServiceNowRequestedItem','Get-ServiceNowTable','Get-ServiceNowTableEntry','Get-ServiceNowUser','Get-ServiceNowUserGroup','New-ServiceNowChangeRequest','New-ServiceNowIncident','New-ServiceNowQuery','New-ServiceNowRecord','Remove-ServiceNowAttachment','Remove-ServiceNowAuth','Remove-ServiceNowRecord','Set-ServiceNowAuth','Test-ServiceNowAuthIsSet','Update-ServiceNowChangeRequest','Update-ServiceNowIncident','Update-ServiceNowNumber','Update-ServiceNowRequestItem','Update-ServiceNowRecord')
62+
FunctionsToExport = @('New-ServiceNowConfigurationItem','Get-ServiceNowRecord','New-ServiceNowSession','Add-ServiceNowAttachment','Get-ServiceNowAttachment','Get-ServiceNowAttachmentDetail','Get-ServiceNowChangeRequest','Get-ServiceNowConfigurationItem','Get-ServiceNowIncident','Get-ServiceNowRequest','Get-ServiceNowRequestedItem','Get-ServiceNowTable','Get-ServiceNowTableEntry','Get-ServiceNowUser','Get-ServiceNowUserGroup','New-ServiceNowChangeRequest','New-ServiceNowIncident','New-ServiceNowQuery','New-ServiceNowRecord','Remove-ServiceNowAttachment','Remove-ServiceNowAuth','Remove-ServiceNowRecord','Set-ServiceNowAuth','Test-ServiceNowAuthIsSet','Update-ServiceNowChangeRequest','Update-ServiceNowIncident','Update-ServiceNowNumber','Update-ServiceNowRequestItem','Update-ServiceNowRecord')
6363

6464
# Variables to export from this module
6565
VariablesToExport = 'ServiceNowSession', 'ServiceNowOperator', 'ServiceNowTable'

0 commit comments

Comments
 (0)