PowerShell中ConvertToJson的问题

时间:2018-08-10 11:54:03

标签: powershell

我已经在Powershell中编写了一个脚本来显示计算机的性能。

如果我想通过js导入文件,则会出现类似以下错误:Uncaught SyntaxError:Unexpected token

我尝试了很多事情,更改了输出参数3次,并创建了psobjects无效...

我的代码:

    <#
Project: Statistik-Dashboard
Class: Get-Value

created by Nicolai
#>

#Disks
$ComuterSystem = Get-CimInstance -ClassName Win32_ComputerSystem  
$storage = foreach ($Computer in $ComuterSystem) {
    $LogicalDisk = Get-CimInstance -ClassName win32_logicaldisk -ComputerName $ComuterSystem.Name
    $diskHash = @{}
    foreach ($disk in ($LogicalDisk | ? {$null -ne $_.DeviceId})) {
        $diskHash.Add($disk.DeviceID, $disk.Size)
    }

    [PSCustomObject]@{
        Name = $Computer.Name
        Model = $Computer.Model
        Manufacturer = $Computer.Manufacturer
        LogicalDisk = $LogicalDisk
        Disks = $diskHash
        Selection = $LogicalDisk | Select-Object DeviceID, VolumeName, Size, FreeSpace
    }
}

$disksum = $diskHash.count
$platte = (Get-PhysicalDisk)
$ssds = (Get-PhysicalDisk | ? model -Match 'ssd')
$ssdsum = 0
foreach($platte in $ssds) {
        $ssdsum++
}

#RAM
$ram_freephysical=0
$ram_totalvisible=0
$ram_freephysical=Get-WmiObject Win32_OperatingSystem | fl *freePhysical* | Out-String
$ram_totalvisible=Get-WmiObject Win32_OperatingSystem | fl *totalvisiblememory* | Out-String
$ram_freephysical = $ram_freephysical -replace '\D+(\d+)','$1'
$ram_totalvisible = $ram_totalvisible  -replace '\D+(\d+)','$1'
$ram = $ram_totalvisible - $ram_freephysical
$ram = [math]::Round($ram/$ram_totalvisible*10000)/100

#CPU
$cpu = (Get-WmiObject win32_processor | Measure-Object -Property LoadPercentage -Average | Select Average).Average

#Ojects
$Diskinfo = [PSCustomObject]@{
    CPUusage = $cpu
    RAMusage = $ram
}

$Utilization = [PSCustomObject]@{
    SSDsum = $ssdsum
    HDDsum = $hddsum
    Disksum = $disksum
}

$disks = [ordered]@{}
for ($i=1; $i -le $storage.Selection.Count; $i++) {
    $disks["Disk${i}"] = $storage.Selection[$i-1]
}

#collector
$stats = [ordered]@{
    Utilization = $Utilization
    Diskinfo = $Diskinfo
    $Computer.Name = $disks
}

#convertor
$file = "C:\Temp\$($ComuterSystem.Name).json"
ConvertTo-Json @($stats) | Add-Content -Path $file

转换的输出:

{
    "Utilization":  {
                        "SSDsum":  2,
                        "HDDsum":  1,
                        "Disksum":  6
                    },
    "Diskinfo":  {
                     "CPUusage":  8,
                     "RAMusage":  81.84
                 },
    "W10-NICO":  {
                     "Disk1":  {
                                   "DeviceID":  "C:",
                                   "VolumeName":  "System",
                                   "Size":  63020462080,
                                   "FreeSpace":  31977562112
                               },
                     "Disk2":  {
                                   "DeviceID":  "D:",
                                   "VolumeName":  null,
                                   "Size":  null,
                                   "FreeSpace":  null
                               },
                     "Disk3":  {
                                   "DeviceID":  "F:",
                                   "VolumeName":  "Daten",
                                   "Size":  499529019392,
                                   "FreeSpace":  480264626176
                               },
                     "Disk4":  {
                                   "DeviceID":  "G:",
                                   "VolumeName":  "SSD",
                                   "Size":  63020462080,
                                   "FreeSpace":  62914928640
                               },
                     "Disk5":  {
                                   "DeviceID":  "H:",
                                   "VolumeName":  "System",
                                   "Size":  106779635712,
                                   "FreeSpace":  70762377216
                               },
                     "Disk6":  {
                                   "DeviceID":  "U:",
                                   "VolumeName":  "System",
                                   "Size":  106779635712,
                                   "FreeSpace":  70762377216
                               }
                 }
}

我希望有人可以帮助我:)

1 个答案:

答案 0 :(得分:0)

首先,您提供的JSON(在编辑之前)无效。问题在于

"Utilization":  [
                    "SSDsum":  2,
                    "HDDsum":  1,
                    "Disksum":  3
                ],

应该有大括号{}而不是方括号[]。这是useful site to validate your JSON


另一个重要的事情是,不可能从ConvertFrom-JSON获得这样的输出。输出将是:

{
    "Utilization":  {
                        "SSDsum":  2,
                        "HDDsum":  1,
                        "Disksum":  3
                    },
    "Diskinfo":  {
                     "CPUusage":  12,
                     "RAMusage":  60.45
                 },
    "W10-NICO":  {
                     "Disk1":  {
                                   "DeviceID":  "C:",
                                   "VolumeName":  "System",
                                   "Size":  63020462080,
                                   "FreeSpace":  35543621632
                               },
                     "Disk2":  {
                                   "DeviceID":  "F:",
                                   "VolumeName":  "Daten",
                                   "Size":  499529019392,
                                   "FreeSpace":  481141452800
                               },
                     "Disk3":  {
                                   "DeviceID":  "G:",
                                   "VolumeName":  "SSD",
                                   "Size":  63020462080,
                                   "FreeSpace":  62914928640
                               }
                 }
}

正如@Ansgar Wiechers所指出的那样,您可能还需要考虑在脚本中指定-Depth参数,以便不会意外修剪结果(根据我的观察,默认值为2,在特定情况下就足够了,因此我将其留在此处作为提醒/最佳实践)。


如果计算机只有一个磁盘,则您的$disks将为空。这是因为:

$i -le $storage.Selection.Count

将永远不会求值为$true(因为单个对象没有.Count参数)。您可以使用

对其进行修复
$i -le @($storage.Selection).count

贷记this answer