【发布时间】:2019-05-12 08:14:27
【问题描述】:
如果能帮助我使用 powershell 转换一些数据,我将不胜感激,但我不知道从哪里开始。
我的数据 csv 如下所示:
System Name | Key | Value
S1 | cpu | Xeon ...
S1 | cores | 8
S1 | cpu | Xeon ...
S1 | cores | 8
S1 | RAM1 | 8
S1 | RAM2 | 8
...
S2 | cpu | Xeon ...
我需要创建一个如下所示的表:
System Name | CPUType | CPUs | Cores
S1 | Xeon .. | 2 | 16
S2 | Xeon .. | 1 | 4
我试过的是这样的:
$csvPath = "C:\Users\rene\Desktop\Report_Machine_Hardware.csv"
$csv = Import-Csv $csvPath | Group-Object machineName
$hash = @{}
foreach ($obj in $csv) {
# Werte pro Server
$cpuCores = 0
$cpus = 0
$cputype = ""
#$obj | fl
foreach ($grp in $obj.Group)
{
#$grp | fl
if ($grp.attributeName -eq "NumberOfCores")
{
$cpuCores = $cpuCores + [int]$grp.attributeValue
#write-host $grp.attributeValue
}
if ($grp.attributeName -eq "ProcessorId")
{
$cpus++
}
if ($grp.attributeName -eq "Name")
{
$cputype = $grp.attributeValue
}
}
#Write-Host "Host $($grp.machineName) hat $cpus CPUs mit $cpuCores CPU Cores vom Typ $cputype"
$out = @($grp.machineName,$cputype,$cpus,$cpuCores)
$hash.Add($grp.machineName,$out)
}
Export-CSV C:\temp\test.csv -InputObject $hash -NoTypeInformation
对于每个系统,我得到一个包含系统名称和我的值的数组,但现在我需要将每个单独的数组带入一个带有标题的列表形式,以再次将其导出到 csv 中。最后 4 行不能按需要工作。 很可能有一种简单的方法可以做到这一点,但我认为现在有某种心理障碍。 任何人都可以帮助我吗?
问候 勒内
【问题讨论】:
标签: arrays powershell csv