-2

I've written a script in Powershell showing the performance of the computer.

If I want to import the file via js, comes an error like: Uncaught SyntaxError: Unexpected token

I've tried much things, changed the output parameta 3 times and created psobjects nothing worked...

My Code:

    <#
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

The output of the convert:

{
    "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
                               }
                 }
}

I hope somebody can help me :)

Nicolai
  • 1
  • 2
  • My Computername also has a '-' in it. when i try to import this json and read $variable.mycomputer-name - i get errors. Maybe js does not like property names with - signs? :) – Tomek Aug 10 '18 at 12:10
  • The code you posted would not create the output you posted (which is invalid JSON anyway). Please update your question with the actual output of your PowerShell code, as well as the JavaScript code that supposedly imports the file (and errors out) and the the complete error thrown by that code. – Ansgar Wiechers Aug 10 '18 at 12:12
  • It's a bit strange, I tested your script and have `{}` instead of `[]` in `Utilization` which seems to be the issue on your side. Please check the actual code and the output as Ansgar wrote – Robert Dyjas Aug 10 '18 at 12:13
  • 1
    @robdy He'll also need to add `-Depth` to the JSON export, lest the nested disk objects get mangled. – Ansgar Wiechers Aug 10 '18 at 12:15
  • 2
    Since it's javascript throwing an error, maybe include details about which platform/engine you're using and the code with which you parse the json – Mathias R. Jessen Aug 10 '18 at 12:52
  • @AnsgarWiechers updated, sorry it was a old file. – Nicolai Aug 10 '18 at 13:53
  • 1
    Your code wouldn't generate your updated JSON output either. Please create a [mcve] that demonstrates the problem your facing and update your question with *that* code and the output created by *that* code. Copy and paste. Do not fabricate things. Do not type from memory. Do not paraphrase. And you still haven't shown the actual code that is throwing the error nor the actual (full) error message. – Ansgar Wiechers Aug 10 '18 at 14:35

1 Answers1

0

First of all, the JSON you provided (before editing) is invalid. The issue is that in

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

there should be curly brackets {} instead of square brackets []. Here's useful site to validate your JSON.


Another important thing is that it's not possible to get such output from ConvertFrom-JSON. The output will be:

{
    "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
                               }
                 }
}

As @Ansgar Wiechers pointed out, you might also want to consider specifying -Depth parameter in your script so that the result is not trimmed unexpectedly (from what I see it default value, which is 2, will be enough in that particular case so I'm leaving it here as a reminder/best practice).


If the machine has only one disk your $disks will be empty. This is because:

$i -le $storage.Selection.Count

Will never evaluate to $true (as there's no .Count parameter for single object). You could fix it by using

$i -le @($storage.Selection).count

Credits to this answer.

Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34