|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Create a new configuration item |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + Create a new configuration item. You can create a specific class ci or root cmdb_ci. |
| 7 | +
|
| 8 | +.PARAMETER Name |
| 9 | + 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. |
| 31 | + |
| 32 | +.EXAMPLE |
| 33 | + New-ServiceNowConfigurationItem -Name 'MyServer' -Class cmdb_ci_server |
| 34 | + Create a new CI |
| 35 | +
|
| 36 | +.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 |
| 39 | +#> |
| 40 | +function New-ServiceNowConfigurationItem { |
| 41 | + |
| 42 | + [CmdletBinding(DefaultParameterSetName = 'Session', SupportsShouldProcess)] |
| 43 | + |
| 44 | + Param( |
| 45 | + |
| 46 | + [parameter(Mandatory)] |
| 47 | + [string] $Name, |
| 48 | + |
| 49 | + [parameter()] |
| 50 | + [string] $Class, |
| 51 | + |
| 52 | + [parameter()] |
| 53 | + [string] $Description, |
| 54 | + |
| 55 | + [parameter()] |
| 56 | + [string] $OperationalStatus, |
| 57 | + |
| 58 | + # custom fields as hashtable |
| 59 | + [parameter()] |
| 60 | + [hashtable] $CustomField, |
| 61 | + |
| 62 | + [Parameter()] |
| 63 | + [switch] $PassThru, |
| 64 | + |
| 65 | + #Azure Automation Connection object containing username, password, and URL for the ServiceNow instance |
| 66 | + [Parameter(ParameterSetName = 'UseConnectionObject', Mandatory)] |
| 67 | + [ValidateNotNullOrEmpty()] |
| 68 | + [Hashtable] $Connection, |
| 69 | + |
| 70 | + [Parameter(ParameterSetName = 'Session')] |
| 71 | + [ValidateNotNullOrEmpty()] |
| 72 | + [hashtable] $ServiceNowSession = $script:ServiceNowSession |
| 73 | + ) |
| 74 | + |
| 75 | + begin {} |
| 76 | + |
| 77 | + process { |
| 78 | + # Create a hash table of any defined parameters (not CustomFields) that have values |
| 79 | + $definedParams = @{ |
| 80 | + 'Name' = 'name' |
| 81 | + 'Class' = 'sys_class_name' |
| 82 | + 'Description' = 'description' |
| 83 | + 'OperationalStatus' = 'operational_status' |
| 84 | + } |
| 85 | + $tableEntryValues = @{} |
| 86 | + foreach ($key in $PSBoundParameters.Keys) { |
| 87 | + if ($definedParams.$key) { |
| 88 | + $tableEntryValues.Add($definedParams.$key, $PSBoundParameters.$key) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + # Add CustomFields hash pairs to the Table Entry Values hash table |
| 93 | + $dupes = ForEach ($Key in $CustomField.Keys) { |
| 94 | + If ($TableEntryValues.ContainsKey($Key)) { |
| 95 | + # Capture the duplicate key name |
| 96 | + $Key |
| 97 | + } |
| 98 | + Else { |
| 99 | + # Add the unique entry to the table entry values hash table |
| 100 | + $TableEntryValues.Add($Key, $CustomField[$Key]) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + # Throw an error if duplicate fields were provided |
| 105 | + If ($dupes) { |
| 106 | + throw ('You are attempting to redefine a value, ''{0}'', with $CustomFields that is already set' -f ($dupes -join ",")) |
| 107 | + } |
| 108 | + |
| 109 | + # Table Entry Splat |
| 110 | + $params = @{ |
| 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 |
| 121 | + } |
| 122 | + |
| 123 | + If ( $PSCmdlet.ShouldProcess($Name, 'Create new configuration item') ) { |
| 124 | + $response = New-ServiceNowRecord @params |
| 125 | + If ($PassThru.IsPresent) { |
| 126 | + $response.PSObject.TypeNames.Insert(0, "ServiceNow.ConfigurationItem") |
| 127 | + $response |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments