|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Retrieves a list of log sources for a LogicMonitor device. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | +The Get-LMDeviceLogSourceList function retrieves all log sources associated with a specific device. The device can be identified by either ID or name, and the results can be filtered. |
| 7 | +
|
| 8 | +.PARAMETER Id |
| 9 | +The ID of the device. Required for Id parameter set. |
| 10 | +
|
| 11 | +.PARAMETER Name |
| 12 | +The name of the device. Required for Name parameter set. |
| 13 | +
|
| 14 | +.PARAMETER Filter |
| 15 | +A filter object to apply when retrieving log sources. This parameter is optional. |
| 16 | +
|
| 17 | +.PARAMETER BatchSize |
| 18 | +The number of results to return per request. Must be between 1 and 1000. Defaults to 1000. |
| 19 | +
|
| 20 | +.EXAMPLE |
| 21 | +#Retrieve log sources by device ID |
| 22 | +Get-LMDeviceLogSourceList -Id 123 |
| 23 | +
|
| 24 | +.EXAMPLE |
| 25 | +#Retrieve log sources by device name with filter |
| 26 | +Get-LMDeviceLogSourceList -Name "MyDevice" -Filter $filterObject |
| 27 | +
|
| 28 | +.NOTES |
| 29 | +You must run Connect-LMAccount before running this command. |
| 30 | +
|
| 31 | +.INPUTS |
| 32 | +None. You cannot pipe objects to this command. |
| 33 | +
|
| 34 | +.OUTPUTS |
| 35 | +Returns log source objects. |
| 36 | +#> |
| 37 | + |
| 38 | +function Get-LMDeviceLogSourceList { |
| 39 | + |
| 40 | + [CmdletBinding(DefaultParameterSetName = 'Id')] |
| 41 | + param ( |
| 42 | + [Parameter(Mandatory, ParameterSetName = 'Id')] |
| 43 | + [Int]$Id, |
| 44 | + |
| 45 | + [Parameter(ParameterSetName = 'Name')] |
| 46 | + [String]$Name, |
| 47 | + |
| 48 | + [Object]$Filter, |
| 49 | + |
| 50 | + [ValidateRange(1, 1000)] |
| 51 | + [Int]$BatchSize = 1000 |
| 52 | + ) |
| 53 | + #Check if we are logged in and have valid api creds |
| 54 | + if ($Script:LMAuth.Valid) { |
| 55 | + |
| 56 | + if ($Name) { |
| 57 | + $LookupResult = (Get-LMDevice -Name $Name).Id |
| 58 | + if (Test-LookupResult -Result $LookupResult -LookupString $Name) { |
| 59 | + return |
| 60 | + } |
| 61 | + $Id = $LookupResult |
| 62 | + } |
| 63 | + |
| 64 | + #Build header and uri |
| 65 | + $ResourcePath = "/device/devices/$Id/devicelogsources" |
| 66 | + |
| 67 | + #Initalize vars |
| 68 | + $QueryParams = "" |
| 69 | + $Count = 0 |
| 70 | + $Done = $false |
| 71 | + $Results = @() |
| 72 | + |
| 73 | + #Loop through requests |
| 74 | + while (!$Done) { |
| 75 | + #Build query params |
| 76 | + $QueryParams = "?size=$BatchSize&offset=$Count&sort=+id" |
| 77 | + |
| 78 | + if ($Filter) { |
| 79 | + $ValidFilter = Format-LMFilter -Filter $Filter -ResourcePath $ResourcePath |
| 80 | + $QueryParams = "?filter=$ValidFilter&size=$BatchSize&offset=$Count&sort=+id" |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + $Headers = New-LMHeader -Auth $Script:LMAuth -Method "GET" -ResourcePath $ResourcePath |
| 85 | + $Uri = "https://$($Script:LMAuth.Portal).$(Get-LMPortalURI)" + $ResourcePath + $QueryParams |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + Resolve-LMDebugInfo -Url $Uri -Headers $Headers[0] -Command $MyInvocation |
| 90 | + |
| 91 | + #Issue request |
| 92 | + $Response = Invoke-LMRestMethod -CallerPSCmdlet $PSCmdlet -Uri $Uri -Method "GET" -Headers $Headers[0] -WebSession $Headers[1] |
| 93 | + |
| 94 | + #Stop looping if single device, no need to continue |
| 95 | + if (![bool]$Response.psobject.Properties["total"]) { |
| 96 | + $Done = $true |
| 97 | + return $Response |
| 98 | + } |
| 99 | + #Check result size and if needed loop again |
| 100 | + else { |
| 101 | + [Int]$Total = $Response.Total |
| 102 | + [Int]$Count += ($Response.Items | Measure-Object).Count |
| 103 | + $Results += $Response.Items |
| 104 | + if ($Count -ge $Total) { |
| 105 | + $Done = $true |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + return $Results |
| 111 | + } |
| 112 | + else { |
| 113 | + Write-Error "Please ensure you are logged in before running any commands, use Connect-LMAccount to login and try again." |
| 114 | + } |
| 115 | +} |
0 commit comments