title: How to Pass A PowerShell Dynamic Variable Value to JSON
body: <p>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.</p>
  
  <pre><code>$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
  </code></pre>
  
  <p>And I end up getting variations of the following output (notice the hostname and ip variables remained the same):</p>
  
  <pre><code>HOSTNAME1
  1.1.1.1
  '{
  "host": "$(hn)",
  "service": "APP_NAME",
  "annotation": "Service is looking dope!",
  “ip": "$(ip)"
  }'
  </code></pre>
  
  <p>I would like the output to look like the following:</p>
  
  <pre><code>HOSTNAME1
  1.1.1.1
  '{
  "host": "HOSTNAME1",
  "service": "APP_NAME",
  "annotation": "Service is looking dope!",
  “ip": "1.1.1.1"
  }'
  </code></pre>
  
  <p>I tried the suggestions mentioned from the following websites:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/35722865/how-to-make-a-post-request-using-powershell-if-body-have-a-parameter-type">How to make a POST request using Powershell if body have a parameter @type</a></p></li>
  <li><p><a href="https://powershell.org/forums/topic/how-to-make-a-post-request-using-powershell-if-body-have-a-parameter-type/" rel="noreferrer">https://powershell.org/forums/topic/how-to-make-a-post-request-using-powershell-if-body-have-a-parameter-type/</a></p></li>
  <li><p><a href="https://www.sapien.com/blog/2018/03/22/storing-powershell-variables-in-external-files/comment-page-1/" rel="noreferrer">https://www.sapien.com/blog/2018/03/22/storing-powershell-variables-in-external-files/comment-page-1/</a></p></li>
  <li><p><a href="https://hazzy.techanarchy.net/winadmin/windows/windows-powershell-elk-log-wash/" rel="noreferrer">https://hazzy.techanarchy.net/winadmin/windows/windows-powershell-elk-log-wash/</a></p></li>
  </ul>
  
  <p>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.</p>
  
  <p>I am able to get details from single commands converted to their JSON counter part i.e.</p>
  
  <pre><code>Get-WmiObject -Class Win32_ComputerSystem -Property Name | ConvertTo-Json
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>(Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name | ConvertTo-Json
  </code></pre>
  
  <p>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.  </p>