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:
How to make a POST request using Powershell if body have a parameter @type
https://hazzy.techanarchy.net/winadmin/windows/windows-powershell-elk-log-wash/
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.