使哈希表中的固定变量动态化

时间:2018-08-09 09:30:08

标签: powershell dynamic

我有一个带有3个固定值的哈希表,但现在我希望脚本自动识别我有多少个值,然后动态创建记录。

$ComuterSystem = Get-CimInstance -ClassName Win32_ComputerSystem  
$cpu = (Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select Average).Average
$platte = (Get-PhysicalDisk)
$ssds = (Get-PhysicalDisk | ? model -Match 'ssd') 
$disksum = (Get-PhysicalDisk).Count

$ssdsum = 0
foreach($platte in $ssds) {
    $ssdsum++
}

$disksum = (Get-PhysicalDisk).Count
$hddsum = $disksum - $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

#disk
$storage = foreach ($Computer in $ComuterSystem) {
    $LogicalDisk = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "drivetype=3" -ComputerName $ComuterSystem.Name

    $diskHash = @{}
    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
    }
}

#collector
$stats = [ordered]@{
    $computer.Name = @{
        "Utilization" = @{
            CPUusage = $cpu
            RAMusage = $ram
        }
        "" = @{
            SSDsum = $ssdsum
            HDDsum = $hddsum
            Disksum = $disksum
        }
    }
    "Storage" = @{
        Festplatte1 = $storage.Selection[0]
        Festplatte2 = $storage.Selection[1]
        Festplatte3 = $storage.Selection[2]
    }
}

#convertor
$stats | ConvertTo-Json | Add-Content -Path C:\Temp\yyy.json

输出是这个

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

但是,如果我只有2个硬盘而不是3个硬盘,那么我有问题,也不知道如何做。

也许有人可以帮助我并添加动态。

2 个答案:

答案 0 :(得分:0)

使用循环使用磁盘信息构建哈希表:

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

然后将哈希表放入您的$stats哈希表中:

$stats = [ordered]@{
    $computer.Name = @{
        "Utilization" = @{
            CPUusage = $cpu
            RAMusage = $ram
        }
        "" = @{
            SSDsum = $ssdsum
            HDDsum = $hddsum
            Disksum = $disksum
        }
    }
    "Storage" = $disks
}

答案 1 :(得分:0)

您基本上可以执行与此操作相同的操作:

$diskHash = @{}
foreach ($disk in ($LogicalDisk.Where({$_.DeviceID}))) {
        $diskHash.Add($disk.DeviceID, $disk.Size)
}

旁边:请注意,$ LogicalDisk没有.Where方法。

改为使用$LogicalDisk | ? {$null -ne $_.DeviceId}

可能需要您重写脚本。与其将收集器部分编写为已声明的有序哈希表,不如编写几行在创建值时创建哈希表,或者甚至更好的是,为脚本的每个部分编写一个函数并将其最终捆绑在一个小的脚本中

以下是如何构造其中一个功能的示例:

function Get-RamStat {
    param (
        $ComputerName = (Get-CimInstance -ClassName Win32_ComputerSystem).Name
    )
    $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName
    @{
        ComputerName = $ComputerName
        FreePhysicalMemory = $OS.FreePhysicalMemory
        TotalVisibleMemorySize = $OS.TotalVisibleMemorySize
        Ram = math::round(($os.TotalVisibleMemorySize - $os.FreePhysicalMemory)/$os.TotalVisibleMemorySize * 10000) / 100
    } | Write-Output
}

它仍然是相同的数据,但是现在您可以先排序数据的收集,然后再排序数据的格式。您可以通过循环运行该函数来扩大规模。

请原谅冗长的回答方式。我是写SO-answer的新手。 希望对您有所帮助:)