6

I am working on a project to have windows-based machines run a scheduled powershell script that will POST host status as a JSON structured data file. The goal is to have the same script be able to run on any Windows Machine without having to manually type in variable values for each server that the script will be placed on i.e. hostname, IP address, etc.

$hn = hostname
$ip = ipconfig
echo $hn
echo $ip
$params = '{
"host": "$(hn)",
"service": "APP_NAME",
"annotation": "Service is looking dope!",
“ip": "$(ip)"
}'

Invoke-WebRequest -Uri http://yoursite.com:5550/ -Method POST -ContentType "application/json" -Body $params
#Invoke-RestMethod -Uri http://yoursite.com:5550/ -Method POST -ContentType "application/json" -Body $params

And I end up getting variations of the following output (notice the hostname and ip variables remained the same):

HOSTNAME1
1.1.1.1
'{
"host": "$(hn)",
"service": "APP_NAME",
"annotation": "Service is looking dope!",
“ip": "$(ip)"
}'

I would like the output to look like the following:

HOSTNAME1
1.1.1.1
'{
"host": "HOSTNAME1",
"service": "APP_NAME",
"annotation": "Service is looking dope!",
“ip": "1.1.1.1"
}'

I tried the suggestions mentioned from the following websites:

I tried a combination of double and single quotation marks and working with @{}, @'{, and appending to the parameter i.e. "param +=" or "param =+". methods with no luck or maybe I am missing something.

I am able to get details from single commands converted to their JSON counter part i.e.

Get-WmiObject -Class Win32_ComputerSystem -Property Name | ConvertTo-Json

But I am having a hard time joining the two different information into a single json file. In addition, I am having a hard time extracting a specific pieces of information and populating the json file i.e.

(Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name | ConvertTo-Json

I apologize if I misused any terminology or did not following any posting guidelines. If I have violated any conditions please let me know and I will make the necessary changes to adhere to them. I apologize if I was not clear or missed any crucial information. Thank you in advance and ny help would greatly be appreciated.

unitelife
  • 151
  • 1
  • 3
  • 13
  • To get string expansion (interpolation) in PowerShell, use `"..."` strings - `'...'` strings, by contrast, are treated as _literals_. To embed `"` inside `"..."`, use `\`"`. – mklement0 Jul 26 '18 at 02:14

2 Answers2

9

You can give this a try.

  $params = '{
    "host": "'+$hn+'",
    "service": "APP_NAME",
    "annotation": "Service is looking dope!",
    “ip": "'+$ip+'"
    }'

This just shows that entire string split to parts and concatenated(+) the variables since you are using single quotes. Also, you can read up on it here: MSDN About Quoting Rules

CodeNagi
  • 419
  • 2
  • 10
7

You need to invert quotation in your JSON string. Anything within single apostrophes is not parsed to include something calculatable. Or, as an alternative, use a Here-String like this:

$params = @"
{
"host": "$hn",
"service": "APP_NAME",
"annotation": "Service is looking dope!",
"ip": "$ip"
}
"@

PS: getting an IP address should (might) be better done the Powershell way: Get-NetIPAddress (params)| Select IPAddress.

Vesper
  • 18,599
  • 6
  • 39
  • 61
  • your script worked! However, how do you get the `Get-NetIPAddress (params)| Select IPAddress` variable to work? I had a similar issue when using the powershell method to grab the host name. Thank you – unitelife Jul 26 '18 at 16:14
  • @unitelife `Get-Help Get-NetIPAddress`, read this and substitute `(params)` with an appropriate collection of parameters. – Vesper Jul 27 '18 at 03:57