Skip to content

Commit 57d93b7

Browse files
committed
Issue Updates
- New cmdlet for removing device group properties - Fix linq regex for config filtering - Add properties method to device datasource instance cmdlet
1 parent d9ac229 commit 57d93b7

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<#
2+
.SYNOPSIS
3+
Removes a property from a LogicMonitor device group.
4+
5+
.DESCRIPTION
6+
The Remove-LMDeviceGroupProperty function removes a specified property from a LogicMonitor device group. It can remove the property either by providing the device group ID or the device group name.
7+
8+
.PARAMETER Id
9+
The ID of the device group from which the property should be removed. This parameter is mandatory when using the 'Id' parameter set.
10+
11+
.PARAMETER Name
12+
The name of the device group from which the property should be removed. This parameter is mandatory when using the 'Name' parameter set.
13+
14+
.PARAMETER PropertyName
15+
The name of the property to be removed. This parameter is mandatory.
16+
17+
.EXAMPLE
18+
Remove-LMDeviceGroupProperty -Id 1234 -PropertyName "Property1"
19+
Removes the property named "Property1" from the device with ID 1234.
20+
21+
.EXAMPLE
22+
Remove-LMDeviceGroupProperty -Name "Device1" -PropertyName "Property2"
23+
Removes the property named "Property2" from the device with the name "Device1".
24+
25+
.INPUTS
26+
None.
27+
28+
.OUTPUTS
29+
System.Management.Automation.PSCustomObject. The output object contains the following properties:
30+
- Id: The ID of the device group from which the property was removed.
31+
- Message: A message indicating the success of the operation.
32+
33+
.NOTES
34+
- This function requires a valid LogicMonitor API authentication. Make sure you are logged in before running any commands.
35+
- Use the Connect-LMAccount function to log in before using this function.
36+
#>
37+
Function Remove-LMDeviceGroupProperty {
38+
39+
[CmdletBinding(DefaultParameterSetName = 'Id', SupportsShouldProcess, ConfirmImpact = 'High')]
40+
Param (
41+
[Parameter(Mandatory, ParameterSetName = 'Id')]
42+
[Int]$Id,
43+
44+
[Parameter(Mandatory, ParameterSetName = 'Name')]
45+
[String]$Name,
46+
47+
[Parameter(Mandatory)]
48+
[String]$PropertyName
49+
50+
)
51+
Begin {}
52+
Process {
53+
#Check if we are logged in and have valid api creds
54+
If ($Script:LMAuth.Valid) {
55+
56+
#Lookup Id if supplying username
57+
If ($Name) {
58+
$LookupResult = (Get-LMDeviceGroup -Name $Name).Id
59+
If (Test-LookupResult -Result $LookupResult -LookupString $Name) {
60+
return
61+
}
62+
$Id = $LookupResult
63+
}
64+
65+
#Build header and uri
66+
$ResourcePath = "/device/groups/$Id/properties/$PropertyName"
67+
68+
If ($Name) {
69+
$Message = "Id: $Id | Name: $Name | Property: $PropertyName"
70+
}
71+
Else {
72+
$Message = "Id: $Id | Property: $PropertyName"
73+
}
74+
75+
Try {
76+
If ($PSCmdlet.ShouldProcess($Message, "Remove Device Group Property")) {
77+
$Headers = New-LMHeader -Auth $Script:LMAuth -Method "DELETE" -ResourcePath $ResourcePath
78+
$Uri = "https://$($Script:LMAuth.Portal).logicmonitor.com/santaba/rest" + $ResourcePath
79+
80+
Resolve-LMDebugInfo -Url $Uri -Headers $Headers[0] -Command $MyInvocation
81+
82+
#Issue request
83+
$Response = Invoke-RestMethod -Uri $Uri -Method "DELETE" -Headers $Headers[0] -WebSession $Headers[1]
84+
85+
$Result = [PSCustomObject]@{
86+
Id = $Id
87+
Message = "Successfully removed ($Message)"
88+
}
89+
90+
Return $Result
91+
}
92+
}
93+
Catch [Exception] {
94+
$Proceed = Resolve-LMException -LMException $PSItem
95+
If (!$Proceed) {
96+
Return
97+
}
98+
}
99+
}
100+
Else {
101+
Write-Error "Please ensure you are logged in before running any commands, use Connect-LMAccount to login and try again."
102+
}
103+
}
104+
End {}
105+
}

Public/Set-LMCollectorConfig.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Function Set-LMCollectorConfig {
104104
$Value = $Value.toString().toLower()
105105

106106
$ConfigArray = $Config.Split([Environment]::NewLine)
107-
[int[]]$Index = [Linq.Enumerable]::Range(0, $ConfigArray.Count).Where({ Param($i) $ConfigArray[$i] -match $ConfLine })
107+
[int[]]$Index = [Linq.Enumerable]::Range(0, $ConfigArray.Count).Where({ Param($i) $ConfigArray[$i] -match "^$ConfLine" })
108108
If (($Index | Measure-Object).Count -eq 1) {
109109
Write-LMHost "[INFO]: Updating config parameter $ConfLine to value $Value."
110110
$ConfigArray[$Index[0]] = "$ConfLine=$Value"

Public/Set-LMDeviceDatasourceInstance.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Specifies the description for the instance.
2020
.PARAMETER Properties
2121
Specifies a hashtable of custom properties for the instance.
2222
23+
.PARAMETER PropertiesMethod
24+
Specifies the method to use when updating the properties. Valid values are "Add", "Replace", or "Refresh".
25+
2326
.PARAMETER StopMonitoring
2427
Specifies whether to stop monitoring the instance. This parameter accepts $true or $false.
2528
@@ -70,6 +73,9 @@ Function Set-LMDeviceDatasourceInstance {
7073

7174
[Hashtable]$Properties,
7275

76+
[ValidateSet("Add", "Replace", "Refresh")] # Add will append to existing prop, Replace will update existing props if specified and add new props, refresh will replace existing props with new
77+
[String]$PropertiesMethod = "Replace",
78+
7379
[Nullable[boolean]]$StopMonitoring,
7480

7581
[Nullable[boolean]]$DisableAlerting,
@@ -162,7 +168,7 @@ Function Set-LMDeviceDatasourceInstance {
162168

163169
If ($PSCmdlet.ShouldProcess($Message, "Set Device Datasource Instance")) {
164170
$Headers = New-LMHeader -Auth $Script:LMAuth -Method "PATCH" -ResourcePath $ResourcePath -Data $Data
165-
$Uri = "https://$($Script:LMAuth.Portal).logicmonitor.com/santaba/rest" + $ResourcePath
171+
$Uri = "https://$($Script:LMAuth.Portal).logicmonitor.com/santaba/rest" + $ResourcePath + "?opType=$($PropertiesMethod.ToLower())"
166172

167173
Resolve-LMDebugInfo -Url $Uri -Headers $Headers[0] -Command $MyInvocation -Payload $Data
168174

0 commit comments

Comments
 (0)