Link to home
Start Free TrialLog in
Avatar of Dan
DanFlag for United States of America

asked on

powershell script error

I'm trying to run a powershell script from my new relic account online, here's the script I am running, directly from new relic, just changed my lic info and acct#.

If anyone knows why this is happening, or how to fix it, I would be so grateful.

Script I'm running:

[Net.ServicePointManager]::SecurityProtocol = 'tls12, tls'; $WebClient = New-Object System.Net.WebClient; $WebClient.DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", "$env:TEMP\install.ps1"); & PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_RELIC_API_KEY='NRAK-WVxxxxxxxxxxxxxxxxx'; $env:NEW_RELIC_ACCOUNT_ID='xxxxxxxx'; & 'C:\Program Files\New Relic\New Relic CLI\newrelic.exe' install


Then I get this error message:

Exception setting "SecurityProtocol": "Cannot convert value "tls12, tls" to type "System.Net.SecurityProtocolType" due

to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration

values are "Ssl3, Tls"."

At line:1 char:28

+ [Net.ServicePointManager]:: <<<< SecurityProtocol = 'tls12, tls'; $WebClient = New-Object System.Net.WebClient; $WebC

lient.DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", "$env:TEMP\install.ps1");

& PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_RELIC_API_KEY='NRAK-xxxxxxxxxxxxxxxxxxx

5M2T70OUW'; $env:NEW_RELIC_ACCOUNT_ID='zzzzzzzz'; & 'C:\Program Files\New Relic\New Relic CLI\newrelic.exe' install

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : PropertyAssignmentException

 

Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: An unexpected error occur

red on a send."

At line:1 char:130

+ [Net.ServicePointManager]::SecurityProtocol = 'tls12, tls'; $WebClient = New-Object System.Net.WebClient; $WebClient.

DownloadFile <<<< ("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", "$env:TEMP\install.ps1");

& PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_RELIC_API_KEY='NRAK-xxxxxxxxxxxxxxxxxxx

5M2T70OUW'; $env:NEW_RELIC_ACCOUNT_ID='zzzzzzzz'; & 'C:\Program Files\New Relic\New Relic CLI\newrelic.exe' install

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

 

The argument 'C:\Users\sadmin\AppData\Local\Temp\install.ps1' to the -File parameter does not exist. Provide the path to

 an existing '.ps1' file as an argument to the -File parameter.

The term 'C:\Program Files\New Relic\New Relic CLI\newrelic.exe' is not recognized as the name of a cmdlet, function, s

cript file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is cor

rect and try again.

At line:1 char:400

+ [Net.ServicePointManager]::SecurityProtocol = 'tls12, tls'; $WebClient = New-Object System.Net.WebClient; $WebClient.

DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", "$env:TEMP\install.ps1"); & Powe

rShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_RELIC_API_KEY='NRAK-xxxxxxxxxxxxxxxxxxx5M2T70

OUW'; $env:NEW_RELIC_ACCOUNT_ID='zzzzzzzz'; & <<<<  'C:\Program Files\New Relic\New Relic CLI\newrelic.exe' install

    + CategoryInfo          : ObjectNotFound: (C:\Program File...LI\newrelic.exe:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Avatar of RobSampson
RobSampson
Flag of Australia image

Try changing the SecurityProtocol command to this
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Avatar of Dan

ASKER

Thanks, I tried that, but apparently I'm still doing something wrong.


This is the command I executed:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $WebClient = New

-Object System.Net.WebClient; $WebClient.DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/instal

l.ps1", "$env:TEMP\install.ps1"); & PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_RELIC

_API_KEY='NRAK-xxxxxxxxxxxxxxxxxxxxxxxx'; $env:NEW_RELIC_ACCOUNT_ID='zzzzzzz'; & 'C:\Program Files\New Relic\New Reli

c CLI\newrelic.exe' install



This is the new error message:

Unexpected token 'WebClient' in expression or statement.

At line:1 char:91

+ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $WebClient <<<<  = New-Object System.

Net.WebClient; $WebClient.DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", "$env:

TEMP\install.ps1"); & PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_RELIC_API_KEY='NRA

K-Wxxxxxxxxxxxxxxxxxx'; $env:NEW_RELIC_ACCOUNT_ID='zzzzzz'; & 'C:\Program Files\New Relic\New Relic CLI\newrel

ic.exe' install

    + CategoryInfo          : ParserError: (WebClient:String) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : UnexpectedToken

Add a semi-colon after Tls12 and before $WebClient

The semi-colon is required to separate the statements....probably a copy paste issue ;-)

Hi!

Here is an updated code with error handlers. I have included notes in the code for your reference. It should work now. Do let me know if there is an error that appears.

try {
    # Set the desired SecurityProtocol
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    # Download the installation script
    $WebClient = New-Object System.Net.WebClient
    $installScriptPath = "$env:TEMP\install.ps1"
    $WebClient.DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", $installScriptPath)

    # Execute the installation script
    & PowerShell.exe -ExecutionPolicy Bypass -File $installScriptPath

    # Set the environment variables
    $env:NEW_RELIC_API_KEY = 'NRAK-WVxxxxxxxxxxxxxxxxx'
    $env:NEW_RELIC_ACCOUNT_ID = 'xxxxxxxx'

    # Check if the New Relic CLI executable exists
    $newRelicCliPath = "C:\Program Files\New Relic\New Relic CLI\newrelic.exe"
    if (Test-Path $newRelicCliPath) {
        # Install New Relic using the CLI
        & $newRelicCliPath install
    } else {
        throw "New Relic CLI executable not found at '$newRelicCliPath'."
    }
}
catch {
    # Handle any exceptions that occurred during the script execution
    switch ($_.Exception.GetType().FullName) {
        'System.Management.Automation.RuntimeException' {
            Write-Host "Error connecting to database: $($_.Exception.Message)"
        }
        'System.Net.WebException' {
            Write-Host "Download error: $($_.Exception.Message)"
        }
        default {
            Write-Host "An unexpected error occurred: $($_.Exception.Message)"
        }
    }
}
finally {
    # Clean up the installation script if it was downloaded
    if (Test-Path $installScriptPath) {
        Remove-Item $installScriptPath -Force
    }
}

Open in new window

Avatar of Dan

ASKER

I tried again with the semicolon, but it's still failing, the command is in black, the error is below.


PS C:\Windows\system32> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ; $WebClient = N

ew-Object System.Net.WebClient; $WebClient.DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/inst

all.ps1", "$env:TEMP\install.ps1"); & PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_REL

IC_API_KEY='NRAK-xxxxxxxxxxxxxxxxxxxxxx'; $env:NEW_RELIC_ACCOUNT_ID='zzzzzzzz'; & 'C:\Program Files\New Relic\New Re

lic CLI\newrelic.exe' install



Exception setting "SecurityProtocol": "Cannot convert null to type "System.Net.SecurityProtocolType" due to invalid enu

meration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Ss

l3, Tls"."

At line:1 char:28

+ [Net.ServicePointManager]:: <<<< SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ; $WebClient = New-Object Syste

m.Net.WebClient; $WebClient.DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", "$en

v:TEMP\install.ps1"); & PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_RELIC_API_KEY='N

RAK-xxxxxxxxxxxxxxxxxxxxxx'; $env:NEW_RELIC_ACCOUNT_ID='zzzzzzzz'; & 'C:\Program Files\New Relic\New Relic CLI\newr

elic.exe' install

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : PropertyAssignmentException


Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: An unexpected error occur

red on a send."

At line:1 char:152

+ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ; $WebClient = New-Object System.Net.

WebClient; $WebClient.DownloadFile <<<< ("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", "$en

v:TEMP\install.ps1"); & PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_RELIC_API_KEY='N

RAK-xxxxxxxxxxxxxxxxxxxxxx'; $env:NEW_RELIC_ACCOUNT_ID='zzzzzzzz'; & 'C:\Program Files\New Relic\New Relic CLI\newr

elic.exe' install

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException


The argument 'C:\Users\sadmin\AppData\Local\Temp\install.ps1' to the -File parameter does not exist. Provide the path to

 an existing '.ps1' file as an argument to the -File parameter.

The term 'C:\Program Files\New Relic\New Relic CLI\newrelic.exe' is not recognized as the name of a cmdlet, function, s

cript file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is cor

rect and try again.

At line:1 char:422

+ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ; $WebClient = New-Object System.Net.

WebClient; $WebClient.DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", "$env:TEMP

\install.ps1"); & PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1;   $env:NEW_RELIC_API_KEY='NRAK-xxxxxxxxxxxxxxxxxxxxxx'; 

$env:NEW_RELIC_ACCOUNT_ID='zzzzzzzz'; & <<<<  'C:\Program Files\New Relic\New Relic CLI\newr

elic.exe' install

    + CategoryInfo          : ObjectNotFound: (C:\Program File...LI\newrelic.exe:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


PS C:\Windows\system32>

Avatar of oBdA
oBdA

Seems like you're using PowerShell 2.0.
The last Windows version that came with PS 2.0 was Windows 7 / Server 2008 R2, so either your OS is really old, or you're starting PowerShell with the "-Version 2" parameter, for whatever reason.
What's your OS, and what's the output of
$PSVersionTable

Open in new window

You can install PowerShell 5 even on Windows 7 (though Windows 7 shouldn't be online anymore, but you probably know that):
Install and Configure WMF 5.1
https://learn.microsoft.com/en-us/powershell/scripting/windows-powershell/wmf/setup/install-configure?view=powershell-5.1

And any specific reason why you're squeezing that into a single line?
Avatar of Dan

ASKER

I am running windows 2008R2.  I just copied and pasted the code from new relic, so not sure why its' on the same line.


So you're saying to upgrade powershell to v5?

Avatar of Dan

ASKER

User generated image


ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dan

ASKER

This is the error message I get now:


PS C:\Windows\system32> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls13

Exception setting "SecurityProtocol": "Cannot convert null to type "System.Net.SecurityProtocolType" due to invalid enu

meration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Ss

l3, Tls"."

At line:1 char:28

+ [Net.ServicePointManager]:: <<<< SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : PropertyAssignmentException


PS C:\Windows\system32> $WebClient = New-Object System.Net.WebClient

PS C:\Windows\system32> $WebClient.DownloadFile("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1"

, "$env:TEMP\install.ps1")

Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: An unexpected error occur

red on a send."

At line:1 char:24

+ $WebClient.DownloadFile <<<< ("https://download.newrelic.com/install/newrelic-cli/scripts/install.ps1", "$env:TEMP\in

stall.ps1")

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException


PS C:\Windows\system32> & PowerShell.exe -ExecutionPolicy Bypass -File $env:TEMP\install.ps1

The argument 'C:\Users\sadmin\AppData\Local\Temp\install.ps1' to the -File parameter does not exist. Provide the path to

 an existing '.ps1' file as an argument to the -File parameter.

PS C:\Windows\system32> $env:NEW_RELIC_API_KEY='NRAK-xxxxxxxxxxxxxxxxx'

PS C:\Windows\system32> $env:NEW_RELIC_ACCOUNT_ID='zzzzzzzz'

PS C:\Windows\system32> & 'C:\Program Files\New Relic\New Relic CLI\newrelic.exe' install

I only posted the full script for reference, not for another attempt; you can't run that script in PS 2.0, full stop.
Download the msi using a browser, install it, run only the last three lines of the script, like I described above.
Avatar of Dan

ASKER

Got it, I did and that would work, but I got this error message, it looks like they don't support windows server 2008 R2, which is a bummer, as I still have 5 or6 or so VMs running this OS.

I have sent an email to support to see if they have a different installer that supports this OS.   Thanks for your help


We encountered an issue during the installation: there was an error discovering system info: Insta

rror: This version of windows is no longer supported.

If this problem persists, visit the documentation and support page for additional help here at htt

/docs/infrastructure/install-infrastructure-agent/get-started/requirements-infrastructure-agent/

Avatar of Dan

ASKER

Noah, I tried the script, but I'm still getting errors with it.

Avatar of Dan

ASKER

I was able to manually install an older version of the client on the server.  Now when I went to activate the API key, I get this error now. This is never ending.

I must be missing something from the code?


PS C:\Users\sadmin>

PS C:\Users\sadmin> $env:NEW_RELIC_API_KEY='NRAK-xxxxxxxxxxxxxxxxxxxxxx'

PS C:\Users\sadmin> $env:NEW_RELIC_ACCOUNT_ID='zzzzzzz'

PS C:\Users\sadmin> & 'C:\Program Files\New Relic\New Relic CLI\newrelic-infra.exe' install

The term 'C:\Program Files\New Relic\New Relic CLI\newrelic-infra.exe' is not recognized as the name of a cmdlet, funct

ion, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path

is correct and try again.

At line:1 char:2

+ & <<<<  'C:\Program Files\New Relic\New Relic CLI\newrelic-infra.exe' install

    + CategoryInfo          : ObjectNotFound: (C:\Program File...relic-infra.exe:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


PS C:\Users\sadmin>

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dan

ASKER


yes, I double checkd, the directory was slightly differrent, I didn't catch that, got it now, thanks so much.