0

I want to create an overview of the local computer in Powershell and output it in JSON via a hash table. Now this can have several hard disks and it must be created dynamically in the hash table.

My Code:

$name = (Get-WmiObject -Class Win32_ComputerSystem -Property
Name).Name     @{foreach ($Disk in $Disk) { $stats.Add("$platten", $Disk[0].VolumeName) }

       stats = @{ $name= @{
                 CPUusage = $CPU
                 RAMusage = $ram
                 disknames = $disknames[1]
                 SSDsum = $ssdsum
                 HDDsum = $hddsum
                 Disksum = $disksum
              }
              $disk1 = @{

              }

              $disk2 = @{

              }

              $disk3 = @{

              }
            }}    

Now I ask the hard drives and saves them in an Hash table. Then the foreach loop should go through each disk and enter the data into the other hash table.

And here comes the Error, i try to put it into the Hashtable and it did not works..

2 Answers 2

1

Your question is very unclear end incomplete. However, I think this might help you on your way:

$ComuterSystem = Get-CimInstance -ClassName Win32_ComputerSystem  

$Result = foreach ($Computer in $ComuterSystem) {
    $LogicalDisk = Get-CimInstance -ClassName win32_logicaldisk -ComputerName $ComuterSystem.Name

    # Create a new hashtable for each computer
    $diskHash = @{}

    # Foreach disk of that computer add it to the hashtable
    foreach ($disk in ($LogicalDisk.Where({$_.DeviceID}))) {
        $diskHash.Add($disk.DeviceID, $disk.Size)
    }

    [PSCustomObject]@{
        Name = $Computer.Name
        Model = $Computer.Model
        Manufacturer = $Computer.Manufacturer
        # Easiest is to simply store all data:
        LogicalDisk = $LogicalDisk
        # Or store the hashtable with your key value pair
        Disks = $diskHash
        # Or store a selection of what you need
        Selection = $LogicalDisk | Select-Object DeviceID, VolumeName, Size, FreeSpace
    }
}

$Result

$Result.Disks

$Result.LogicalDisk

$Result.Selection
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this script made my day
0

if you create a hashtable it is typically of fixed size

initialize the variable $disknames like:

$disknames = New-Object System.Collections.ArrayList

then you can add entries like you tried to

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.