NewRelic.Agents.DotNet.psm1

Using module '.\NewRelic.Agents.psm1'
Using module '.\NewRelic.Download.psm1'

<#
.Synopsis
    Installs the New Relic .Net Agent
.Description
    Installs the New Relic .Net Agent
.Example
    Install-NRDotNetAgent -LicenseKey <LicenseKey>
    Installs, licenses and starts the New Relic .Net agent
.Example
    Install-NRDotNetAgent -LicenseKey <LicenseKey> -Version '10.27.0'
    Installs, licenses and starts the New Relic .Net agent version 10.27.0
.Parameter LicenseKey
    New Relic License Key that links the agent to the account
.Parameter Version
    If provided, attempts to install a specific version of the agent otherwise falls back to the default managed by New Relic
.Parameter InstrumentAll
    Installs for non IIS apps to be instrumented like windows services
#>

Function Install-NRDotNetAgent {
    [CMDLetBinding()]
    Param (
        [Parameter (Mandatory = $true)]
        [string] $LicenseKey,
        [Parameter (Mandatory = $false)]
        [string] $Version = '',
        [Parameter (Mandatory = $false)]
        [switch] $InstrumentAll
    )
    # Must run as an admin to install and start the service...
    Confirm-RunAsAdmin | Out-Null

    if ($Version -ne '') {
        Write-Output "Using Version: $Version"
        $AgentRootURL = "https://download.newrelic.com/dot_net_agent/previous_releases/$Version/"
        $AgentName = "NewRelicDotNetAgent_$Version`_x64.msi"
    }
    else {
        # Use Default version updated by New Relic
        $AgentRootURL = 'https://download.newrelic.com/dot_net_agent/latest_release/'
        $AgentName = 'NewRelicDotNetAgent_x64.msi'
    }

    $localPath = Get-TempDownload -DownloadURL "$AgentRootURL$AgentName" -FileName $AgentName

    # Install and Wait
    Write-Output "Installing Agent: $AgentName"
    if($InstrumentAll){
        $arguments = "/qn /i $localPath NR_LICENSE_KEY=$LicenseKey INSTALLLEVEL=50"
    }else{
        $arguments = "/qn /i $localPath NR_LICENSE_KEY=$LicenseKey INSTALLLEVEL=1"
    }
    Start-Process 'C:\Windows\System32\msiexec.exe' -ArgumentList $arguments -wait

    # Remove Temp Agent File
    Write-Output "Cleaning up Agent Files: $localPath"
    Remove-Item $localPath -Force

    # Write output when finished
    Write-Output 'Finished Install'
}

<#
.Synopsis
    Gets details on the New Relic .Net agent running on the machine.
.Description
    Gets details on the New Relic .Net agent running on the machine.
.Example
    Get-NRDotNetAgent
    Returns details on the .Net agent
#>

Function Get-NRDotNetAgent {
    [CMDLetBinding()]
    Param (
    )
    $agentDetails = Get-InstalledSoftwareFromRegistry -Name 'New Relic .NET Agent*'

    # Validate Agent is present
    if($null -eq $agentDetails){
        Throw "DotNet Agent installation not found"
    }

    return [PSCustomObject]@{
        Name = $agentDetails.DisplayName
        Version = $agentDetails.DisplayVersion
    }
}

<#
.Synopsis
    Updates the New Relic .Net Agent
.Description
    Updates the New Relic .Net Agent to a specific version
.Example
    Update-NRDotNetAgent -Version '10.27.0'
    Checks the existing agent version and if it doesn't match updates to 10.27.0
.Parameter Version
    Installs a specific version of the agent. Please review the release notes before updating.
#>

Function Update-NRDotNetAgent {
    [CMDLetBinding(SupportsShouldProcess = $true)]
    Param (
        [Parameter (Mandatory = $true)]
        [string] $Version
    )
    # Must run as an admin to install and start the service...
    Confirm-RunAsAdmin | Out-Null

    if ($Version -ne '') {
        Write-Output "Using Version: $Version"
        $AgentRootURL = "https://download.newrelic.com/dot_net_agent/previous_releases/$Version/"
        $AgentName = "NewRelicDotNetAgent_$Version`_x64.msi"
    }
    else {
        # Use Default version updated by New Relic
        $AgentRootURL = 'https://download.newrelic.com/dot_net_agent/latest_release/'
        $AgentName = 'NewRelicDotNetAgent_x64.msi'
    }

    $agentDetails = Get-NRDotNetAgent

    if($agentDetails.Version -like "$Version*"){
        Write-Output "Version $Version already installed..."
    }else{
        Write-Output "Currently running version: $($agentDetails.Version)"
        if ($PSCmdlet.ShouldProcess('New Relic .Net Agent',"Update to $Version")) {
            # Download Agent
            $localPath = Get-TempDownload -DownloadURL "$AgentRootURL$AgentName" -FileName $AgentName

            # Install and Wait
            Write-Output "Installing Agent: $AgentName"
            Start-Process 'C:\Windows\System32\msiexec.exe' -ArgumentList "/qn /i $localPath" -wait

            # Remove Temp Agent File
            Write-Output "Cleaning up Agent Files: $localPath"
            Remove-Item $localPath -Force

            # Write output when finished
            Write-Output 'Finished Updating'
        }
    }
}