Public/New-LMDeviceGroup.ps1
Function New-LMDeviceGroup { [CmdletBinding()] Param ( [Parameter(Mandatory)] [String]$Name, [String]$Description, [Hashtable]$Properties, [Boolean]$DisableAlerting = $false, [Boolean]$EnableNetFlow = $false, [Parameter(Mandatory, ParameterSetName = 'GroupId')] [Int]$ParentGroupId, [Parameter(Mandatory, ParameterSetName = 'GroupName')] [String]$ParentGroupName, [String]$AppliesTo ) #Check if we are logged in and have valid api creds If ($Script:LMAuth.Valid) { #Lookup ParentGroupName If ($ParentGroupName) { If ($ParentGroupName -Match "\*") { Write-Error "Wildcard values not supported for groups names." return } $ParentGroupId = (Get-LMDeviceGroup -Name $ParentGroupName | Select-Object -First 1 ).Id If (!$ParentGroupId) { Write-Error "Unable to find group: $ParentGroupName, please check spelling and try again." return } } #Build custom props hashtable $customProperties = @() If ($Properties) { Foreach ($Key in $Properties.Keys) { $customProperties += @{name = $Key; value = $Properties[$Key] } } } #Build header and uri $ResourcePath = "/device/groups" Try { $Data = @{ name = $Name description = $Description appliesTo = $AppliesTo disableAlerting = $DisableAlerting enableNetflow = $EnableNetFlow customProperties = $customProperties parentId = $ParentGroupId defaultAutoBalancedCollectorGroupId = 0 defaultCollectorGroupId = 0 defaultCollectorId = 0 } $Data = ($Data | ConvertTo-Json) $Headers = New-LMHeader -Auth $Script:LMAuth -Method "POST" -ResourcePath $ResourcePath -Data $Data $Uri = "https://$($Script:LMAuth.Portal).logicmonitor.com/santaba/rest" + $ResourcePath #Issue request $Response = Invoke-RestMethod -Uri $Uri -Method "POST" -Headers $Headers[0] -WebSession $Headers[1] -Body $Data Return (Add-ObjectTypeInfo -InputObject $Response -TypeName "LogicMonitor.DeviceGroup" ) } Catch [Exception] { $Proceed = Resolve-LMException -LMException $PSItem If (!$Proceed) { Return } } } Else { Write-Error "Please ensure you are logged in before running any commands, use Connect-LMAccount to login and try again." } } |