标签:
1.直接输出为json格式:
Get-Process -Id $pid | ConvertTo-Json | clip.exe
2.自定义结果为json格式:
$serverinfoj = @" { "Status": "Success", "Infors": { "ServerName": "$env:ComputerName", "IP": "$Server", "OSVersion": "$osversion", "MemorySize": "$memorysize_sum", "CPU": "$cpunamecore", "DomainName": "$domainname", "DISK": "$disklist", "SN": "$sn", "Xinghao":"$xinghao" } } "@ #格式必须要这样,顶格写,开头和结尾的@不能与大括号写到一行 #其中的变量内容也必须要引起来 $serverinfo = ConvertFrom-Json -InputObject $serverinfoj #转换为json格式 $serverinfo.Status $serverinfo.Infors $serverinfo.Infors.OSVersion
#输出结果: ServerName : PC-L IP : 10.16.30.51 OSVersion : Microsoft Windows Server 2012 R2 Datacenter 64bit MemorySize : 4GB CPU : Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz 2*1C DomainName : uxin.youxinpai.com DISK : Disk0:300GB SN : Hyper-V Xinghao : Microsoft Virtual Machine Microsoft Windows Server 2012 R2 Datacenter 64bit
还可以对$serverinfo内容进行修改,如:
$serverinfo.Status = "test"
value也可以直接通过命令获得,使用$符号
$json = @" { "ServerName": "$env:ComputerName", "BIOS": { "sn" : "$((Get-WmiObject -Class Win32_BIOS).sn)", "Version" : "$((Get-WmiObject -Class Win32_BIOS).Version)" }, "OS" : "$([Environment]::OSVersion.VersionString)" } "@
标签:
原文地址:http://www.cnblogs.com/dreamer-fish/p/5551148.html