NewRelicPS.Applications.psm1

Using module .\NewRelicPS.NRQLQuery.psm1

<#
.Synopsis
  Gets Application details from New Relic
.Description
  Gets Application details from New Relic
.Example
  Get-NRApplication -APIKey 'fake-api-key'
  Returns all the application in New Relic
.Example
  Get-NRApplication -APIKey 'fake-api-key' -ApplicationId '543219870'
  Returns only the application details specific to the provided application id
.Example
  Get-NRApplication -APIKey 'fake-api-key' -ApplicationId ''
  Returns all the application in New Relic when no Application Id is passed.
.Example
  @('543219870','503698546','657896324') | Get-NRApplication -APIKey 'fake-api-key'
  Returns applications in New Relic when multiple Application Id is passed through pipe
.Parameter APIKey
  Can be either the account level REST API key or an admin user's API Key. See more here: https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys
.Parameter ApplicationId
  Returns a single Application, identified by ID
#>

Function Get-NRApplication {
  [CmdletBinding()]
  param (
    [Parameter (Mandatory = $true)]
    [string] $APIKey,
    [Parameter (ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
    [string] $ApplicationId
  )

  Begin {
    $headers = @{
      'X-Api-Key' = $APIKey
    }
  }
  Process {
    if ($ApplicationId) {
      $url = "https://api.newrelic.com/v2/applications/$ApplicationId.json"
    }
    else {
      $url = "https://api.newrelic.com/v2/applications.json"
    }
    [array] $application = Invoke-RestMethod -uri $url -method 'GET' -ContentType 'application/json' -header $headers -FollowRelLink
    return $application
  }
}

<#
.Synopsis
  Gets host name using the application id in New Relic
.Description
  Gets host name using the application id in New Relic
.Example
  Get-NRHostName -APIKey 'fake-api-key' -AccountId 2789621
  Returns all the host names in New Relic
.Example
  Get-NRHostName -APIKey 'fake-api-key' -ApplicationId '543256987' -AccountId 2789621
  Returns host name for the provided application id
.Example
  Get-NRHostName -APIKey 'fake-api-key' -ApplicationId '' -AccountId 2789621
  Returns all the host names in New Relic when no ApplicationId is provided
.Example
  @('543219870','503698546','657896324') | Get-NRHostName -APIKey 'fake-api-key' -AccountId 123456789
  Returns applications in New Relic when multiple Application Id is passed through pipe
.Parameter APIKey
  Can be either the account level REST API key or an admin user's API Key. See more here: https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys
.Parameter ApplicationId
  Returns a single host name, identified by ID
.Parameter AccountId
  New Relic account id
#>

Function Get-NRHostName {
  Param (
    [Parameter (ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
    [string] $ApplicationID,
    [Parameter (Mandatory = $true)]
    [string] $APIKey,
    [Parameter (Mandatory = $true)]
    [string] $AccountId
  )
  Process {
    If ($ApplicationID) {
      [string] $Query = "SELECT uniques(hostname) AS host_names FROM SystemSample SINCE 1 hour ago limit max WHERE apmApplicationIds = '|$ApplicationID|' "
    }
    Else {
      [string] $Query = 'SELECT uniques(hostname) AS host_names FROM SystemSample SINCE 1 hour ago limit max'
    }

    $application_hosts = Invoke-NRQLQuery -AccountId $AccountId -Query $Query -APIKey $APIKey
    $applicationHostName = $application_hosts.data.actor.account.nrql.results.host_names

    return $applicationHostName
  }
}