|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Updates normalized properties in LogicMonitor. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + The Set-LMNormalizedProperties cmdlet updates normalized properties in LogicMonitor. Normalized properties allow you to map multiple host properties to a single alias that can be used across your environment. |
| 7 | +
|
| 8 | +.PARAMETER Alias |
| 9 | + The alias name for the normalized property. |
| 10 | +
|
| 11 | +.PARAMETER Properties |
| 12 | + An array of host property names to map to the alias. |
| 13 | +
|
| 14 | +.EXAMPLE |
| 15 | + PS C:\> Set-LMNormalizedProperties -Add -Alias "location" -Properties @("location", "snmp.sysLocation", "auto.meraki.location") |
| 16 | + Updates a normalized property with alias "location" to include the new properties. |
| 17 | +
|
| 18 | + PS C:\> Set-LMNormalizedProperties -Remove -Alias "location" -Properties @("auto.meraki.location") |
| 19 | + Removes the "auto.meraki.location" property from the "location" alias. |
| 20 | +
|
| 21 | +.NOTES |
| 22 | + Requires valid LogicMonitor API credentials set via Connect-LMAccount. |
| 23 | + This cmdlet uses LogicMonitor API v4. |
| 24 | +#> |
| 25 | + |
| 26 | +Function Set-LMNormalizedProperties { |
| 27 | + |
| 28 | + [CmdletBinding()] |
| 29 | + Param ( |
| 30 | + [Parameter(Mandatory = $true)] |
| 31 | + [String]$Alias, |
| 32 | + [Parameter(Mandatory = $true, ParameterSetName = "Add")] |
| 33 | + [Switch]$Add, |
| 34 | + [Parameter(Mandatory = $true, ParameterSetName = "Remove")] |
| 35 | + [Switch]$Remove, |
| 36 | + [Parameter(Mandatory = $true)] |
| 37 | + [Array]$Properties |
| 38 | + ) |
| 39 | + |
| 40 | + #Check if we are logged in and have valid api creds |
| 41 | + Begin {} |
| 42 | + Process { |
| 43 | + If ($Script:LMAuth.Valid) { |
| 44 | + #Get existing normalized properties as all updates have to be done via bulk |
| 45 | + $ExistingProperties = Get-LMNormalizedProperties |
| 46 | + |
| 47 | + #Build header and uri |
| 48 | + $ResourcePath = "/normalizedProperties/bulk" |
| 49 | + |
| 50 | + #Initialize the body with existing properties that don't match our alias |
| 51 | + $Body = [PSCustomObject]@{ |
| 52 | + data = [PSCustomObject]@{ |
| 53 | + items = @() |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + # Track the highest priority for the current alias |
| 58 | + $maxPriority = 0 |
| 59 | + |
| 60 | + # If we're removing properties, we need to preserve all other aliases |
| 61 | + if ($Remove) { |
| 62 | + # Add all existing properties |
| 63 | + foreach ($prop in $ExistingProperties) { |
| 64 | + if ($prop.isEditable) { |
| 65 | + if ($prop.alias -eq $Alias) { |
| 66 | + # Only keep properties that aren't in our removal list |
| 67 | + if ($prop.hostProperty -notin $Properties) { |
| 68 | + $Body.data.items += [PSCustomObject]@{ |
| 69 | + id = $prop.id |
| 70 | + model = "normalizedProperties" |
| 71 | + alias = $prop.alias |
| 72 | + hostProperty = $prop.hostProperty |
| 73 | + hostPropertyPriority = $prop.hostPropertyPriority |
| 74 | + description = $prop.description |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + else { |
| 79 | + # For non-editable properties, only include id and model |
| 80 | + $Body.data.items += [PSCustomObject]@{ |
| 81 | + id = $prop.id |
| 82 | + model = "normalizedProperties" |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + # If we're adding properties, we need to preserve existing properties for other aliases |
| 89 | + # and add our new properties |
| 90 | + else { |
| 91 | + # First add all existing properties |
| 92 | + foreach ($prop in $ExistingProperties) { |
| 93 | + if ($prop.alias -eq $Alias) { |
| 94 | + # Track the highest priority for this alias |
| 95 | + if ($prop.hostPropertyPriority -gt $maxPriority) { |
| 96 | + $maxPriority = $prop.hostPropertyPriority |
| 97 | + } |
| 98 | + # Only include full property details if it's editable |
| 99 | + if ($prop.isEditable) { |
| 100 | + $Body.data.items += [PSCustomObject]@{ |
| 101 | + id = $prop.id |
| 102 | + model = "normalizedProperties" |
| 103 | + alias = $prop.alias |
| 104 | + hostProperty = $prop.hostProperty |
| 105 | + hostPropertyPriority = $prop.hostPropertyPriority |
| 106 | + description = $prop.description |
| 107 | + } |
| 108 | + } |
| 109 | + else { |
| 110 | + # For non-editable properties, only include id and model |
| 111 | + $Body.data.items += [PSCustomObject]@{ |
| 112 | + id = $prop.id |
| 113 | + model = "normalizedProperties" |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + else { |
| 118 | + # For other aliases, only include id and model |
| 119 | + $Body.data.items += [PSCustomObject]@{ |
| 120 | + id = $prop.id |
| 121 | + model = "normalizedProperties" |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + # Then add our new properties starting from the next available priority |
| 127 | + $Index = $maxPriority + 1 |
| 128 | + foreach ($Property in $Properties) { |
| 129 | + # Check if this property already exists for this alias |
| 130 | + $exists = $false |
| 131 | + foreach ($prop in $ExistingProperties) { |
| 132 | + if ($prop.alias -eq $Alias -and $prop.hostProperty -eq $Property) { |
| 133 | + $exists = $true |
| 134 | + break |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + # Only add if it doesn't already exist |
| 139 | + if (-not $exists) { |
| 140 | + $Body.data.items += [PSCustomObject]@{ |
| 141 | + model = "normalizedProperties" |
| 142 | + alias = $Alias |
| 143 | + hostProperty = $Property |
| 144 | + hostPropertyPriority = $Index |
| 145 | + } |
| 146 | + $Index++ |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + $Body = $Body | ConvertTo-Json -Depth 10 |
| 152 | + |
| 153 | + Try { |
| 154 | + $Headers = New-LMHeader -Auth $Script:LMAuth -Method "PATCH" -ResourcePath $ResourcePath -Version 4 |
| 155 | + $Uri = "https://$($Script:LMAuth.Portal).logicmonitor.com/santaba/rest" + $ResourcePath |
| 156 | + |
| 157 | + Resolve-LMDebugInfo -Url $Uri -Headers $Headers[0] -Command $MyInvocation -Payload $Body |
| 158 | + |
| 159 | + #Issue request |
| 160 | + $Response = Invoke-RestMethod -Uri $Uri -Method "PATCH" -Headers $Headers[0] -WebSession $Headers[1] -Body $Body |
| 161 | + |
| 162 | + # Check for errors in the response |
| 163 | + if ($Response.errors.Count -gt 0) { |
| 164 | + foreach ($error in $Response.errors) { |
| 165 | + Write-Error "Error updating normalized properties: $($error.message)" |
| 166 | + } |
| 167 | + Return |
| 168 | + } |
| 169 | + Write-Output "Normalized properties updated successfully" |
| 170 | + return |
| 171 | + } |
| 172 | + Catch [Exception] { |
| 173 | + $Proceed = Resolve-LMException -LMException $PSItem |
| 174 | + If (!$Proceed) { |
| 175 | + Return |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + Else { |
| 180 | + Write-Error "Please ensure you are logged in before running any commands, use Connect-LMAccount to login and try again." |
| 181 | + } |
| 182 | + } |
| 183 | + End {} |
| 184 | +} |
0 commit comments