Powershell到Json文件 [英] Powershell to Json file

查看:145
本文介绍了Powershell到Json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取给定平台上所有带有黄色爆炸的设备的列表,将其作为字典写入json文件,最后通过python脚本进行读取.

i'm trying to get a list of all devices with yellow bang on a given platform, write it as a dictionary to json file, and finally read it via python script.

一切似乎都很简单,我可以将字符串保存到json文件中,但是当我尝试打开它时出现错误. 我不知道问题是否出在Powershell脚本上,或者我只是没有正确构造json对象.

All seems pretty easy, i can save the string to json file but when i try to open it i get an error. i can't figure out if the problem is with the powershell script, or maybe i'm just not constructing the json object correctly.

$baddevices = Get-WmiObject Win32_PNPEntity | where {$_.ConfigManagerErrorcode -ne 0}

$jsonRepresentation = '{'

foreach ($device in $baddevices) {
    $jsonRepresentation = $jsonRepresentation + " {0}: {1}," -f $device.name, $device.deviceid 
}


$jsonRepresentation = $jsonRepresentation.Substring(0,$jsonRepresentation.Length-1) + '}'


$jsonRepresentation | ConvertTo-Json | Out-File "C:\file.json"

推荐答案

我不知道您要在中间做些什么,我认为这是没有必要的.

I don't know what your trying to do in the middle there, I think its just not necessary.

尝试:

$baddevices = Get-WmiObject Win32_PNPEntity | where {$_.ConfigManagerErrorcode -ne 0}

$baddevices| ConvertTo-Json | Out-File "C:\file.json"

希望有帮助吗?

这篇关于Powershell到Json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆