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://powershell.org/forums/topic/how-to-make-a-post-request-using-powershell-if-body-have-a-parameter-type/
https://www.sapien.com/blog/2018/03/22/storing-powershell-variables-in-external-files/comment-page-1/
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.
PowerShell is a great tool to use for manipulating JSON which is used throughout Azure. Have fun scripting! Additional reading: 7.1: ConvertFrom-Json (Microsoft.
PowerShell has reserved variables such as $$ , $? , $^ , and $_ that contain alphanumeric and special characters.
Description. The ConvertTo-Json cmdlet converts any . NET object to a string in JavaScript Object Notation (JSON) format. The properties are converted to field names, the field values are converted to property values, and the methods are removed.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With