简体   繁体   English

如何将PowerShell动态变量值传递给JSON

[英]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. 我正在开发一个项目,让基于Windows的计算机运行一个计划的PowerShell脚本,该脚本将POST主机状态作为JSON结构化数据文件。 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 ie hostname, IP address, etc. 目标是让相同的脚本能够在任何Windows机器上运行,而无需为脚本将被放置的每个服务器手动键入变量值,即主机名,IP地址等。

$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): 我最终得到以下输出的变体(注意主机名和ip变量保持不变):

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 ie "param +=" or "param =+". 我尝试了双引号和单引号的组合,并使用@ {},@'{,并附加到参数即“param + =”或“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 ie 我可以从转换为JSON计数器部分的单个命令中获取详细信息

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. 但我很难将两个不同的信息加入到单个json文件中。 In addition, I am having a hard time extracting a specific pieces of information and populating the json file ie 另外,我很难提取特定的信息并填充json文件,即

(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. 提前谢谢你,非常感谢你的帮助。

You need to invert quotation in your JSON string. 您需要在JSON字符串中反转引号。 Anything within single apostrophes is not parsed to include something calculatable. 单撇号内的任何内容都不会被解析为包含可计算的内容。 Or, as an alternative, use a Here-String like this: 或者,作为替代方案,使用像这样的Here-String:

$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 PS:获取IP地址应该(可能)更好地完成Powershell方式: Get-NetIPAddress (params)| Select IPAddress Get-NetIPAddress (params)| Select IPAddress . 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 此外,您可以在此处阅读: MSDN关于报价规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM