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)"
 }
"@

 

$data = (New-Object PSObject |
    Add-Member -PassThru NoteProperty ServerName "nnn" |
    Add-Member -PassThru NoteProperty Infors "192.168.1.1"
) | ConvertTo-JSON #返回是json字符串,等同于@""@方法

 

 后端API,通过GET方法接收URL参数:

def srvinfors_api(request): #Client access this api to write server infors.
    if request.method == 'GET':
        Infors=request.GET['Infors']
        cj = json.loads(Infors,strict=False,encoding='utf-8') #将参数转换为dict
        cjres = json.dumps(cj,ensure_ascii=False) #返回时使用json字符串
        print cj['ServerName']
        #serverinfors.objects.update_or_create(IP=cj['IP'],defaults=cj)
        return HttpResponse(cjres)

提交参数:http://10.160.25.48/sinfors/srvinfors_api/?a=2&b=3&c=22

 

在PowerShell3.0中使用invoke-restmethod提交参数,(invoke-webrequest返回格式是对象,invoke-restmethod返回格式是字符串)

$a='adf'
$h=@{a=$a;
b=222;
c=""
}

$url = "http://10.160.25.48/sinfors/srvinfors_api"

invoke-restmethod -Uri $url -body $h

 参数中还可以包含json格式数据:

$a='adf'

$data=@{a=$a;
b=222;
c=@"
{"ServerName": "$env:ComputerName","IP": "$Server"}
"@
}

$url = "http://10.160.25.48/sinfors/srvinfors_api"

invoke-restmethod -Uri $url -body $data

Python API中取参数c的字段值:

json.loads(c)['ServerName']

 

示例:将计算机硬件信息提交到API,该API可以将数据写入到数据库中(PS3.0):

$system = Get-WmiObject -Class Win32_ComputerSystem

#获取计算机域名、型号
$domainname = $system.Domain
$model = $system.Model

#获取计算机IP地址,取IP和gw不为空的网卡IP地址
$Server = (gwmi Win32_NetworkAdapterConfiguration |?{ $_.DefaultIPGateway -ne $null}).IPAddress[0]


#获取操作系统版本
$os = Get-WmiObject -Class Win32_OperatingSystem

#获取操作系统版本
$os_caption =  $os.Caption
If ($os_caption.Contains("Server 2008 R2 Enterprise"))
{$os_caption_s = "Win2008"}
ElseIf ($os_caption.Contains("Server 2003 Enterprise"))
{$os_caption_s = "Win2003"}
Else {$os_caption_s = $os.Caption}
$osversion = $os_caption_s + " " + $os.OSArchitecture.Substring(0,2) + "bit"


#获取CPU名称、单颗CPU核心数量*CPU个数
$cpus = Get-WmiObject -Class win32_processor
$cpucount = 0
Foreach ($cpu in $cpus)
  {
  If ($cpu.DeviceID -ne $null)
 {$cpucount += 1}
  }
$cpunamecore = $cpu.name+""+[string]$cpu.NumberOfLogicalProcessors + '*' + [string]$cpucount + "C"

#获取内存大小
$memorys = Get-WmiObject -Class Win32_PhysicalMemory
#$memorylist = $null
$memorysize_sum = $null
Foreach ($memory in $memorys)
  {
   #$memorylist += ($memory.capacity/1024/1024/1024).tostring("F1")+"GB + "
   [int]$memorysize_sum_n +=  $memory.capacity/1024/1024/1024
  }
$memorysize_sum = [string]$memorysize_sum_n + "GB"

#获取磁盘信息
$disks = Get-WmiObject -Class Win32_Diskdrive
$disklist = $null
#$disksize_sum = $null
Foreach ($disk in $disks)
  {
   $disklist += ($disk.deviceid.replace("\\.\PHYSICALDRIVE","Disk") +":" + [int]($disk.size/1024/1024/1024)+"GB ")
   #$disksize_sum+=$disk.size
  }

#获取计算机序列号、制造商
$bios = Get-WmiObject -Class Win32_BIOS
$sn = $bios.SerialNumber
If ($sn.Substring(0,6) -eq "VMware")
   {$sn = "VMware"}
If ($bios.Manufacturer.contains("Dell"))
  {$manufacturer = "Dell"} 
Elseif ($bios.Manufacturer.contains("HP")) 
  {$manufacturer = "HP"} 
Elseif ($bios.Manufacturer.contains("Microsoft")) 
  {
   $manufacturer = "Microsoft"
   $sn = ""
   }
Else {$manufacturer = $bios.Manufacturer}
$type = $manufacturer + " " + $model
if ($type.contains("Microsoft Virtual Machine"))
    {$type = "Hyper-V"}


$serverinfoj = @{
Status="Success";
Infors= @"
{"ServerName": "$env:ComputerName","IP": "$Server","OSVersion": "$osversion","MemorySize": "$memorysize_sum", "CPU": "$cpunamecore","DomainName": "$domainname","DISK": "$disklist","SN": "$sn","Type":"$type"}
"@
}

$url = "http://10.160.25.48/sinfors/srvinfors_api"

invoke-restmethod -Uri $url -body $serverinfoj
View Code

相关文章:

  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案