Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass A PowerShell Dynamic Variable Value to JSON

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.

like image 336
unitelife Avatar asked Jul 26 '18 00:07

unitelife


People also ask

Can PowerShell parse JSON?

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.

What type of variable is $$ in PowerShell?

PowerShell has reserved variables such as $$ , $? , $^ , and $_ that contain alphanumeric and special characters.

What is ConvertTo-JSON?

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.


2 Answers

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.

like image 196
Vesper Avatar answered Oct 21 '22 03:10

Vesper


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

like image 44
CodeNagi Avatar answered Oct 21 '22 02:10

CodeNagi