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
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 ;-)
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
}
}
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>
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
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?
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?
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
Download the msi using a browser, install it, run only the last three lines of the script, like I described above.
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/
ASKER
Noah, I tried the script, but I'm still getting errors with it.
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>
ASKER
yes, I double checkd, the directory was slightly differrent, I didn't catch that, got it now, thanks so much.
Try changing the SecurityProtocol command to this
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12