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#>
1740function 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
0 commit comments