【问题标题】:Datatransformation with PowerShell使用 PowerShell 进行数据转换
【发布时间】: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


    【解决方案1】:

    如上所述 - 我也认为代码中的数据和您的属性名称不匹配。

    数据格式:

    尝试改用 PS 对象并使用 csv 中的属性名称:

    $csvPath = "C:\temp\book1.csv"
    $csv = Import-Csv $csvPath | Group-Object SystemName
    ForEach ($obj in $csv) {
    
    # Werte pro Server
    $cpuCores = 0
    $cpus = 0
    $cputype = ""
    
    
    
    #$obj | fl
    
    $NewOutput = foreach ($grp in $obj.Group)
    {
        if ($grp.key -eq "cores")
        {
            $cpuCores = $cpuCores + [int]$grp.Value
            #write-host $grp.attributeValue
        }
    
        if ($grp.key -eq "CPU")
        {
            $cpus++
            $cputype = $grp.Value
        }
    }
    
    
    #Write-Host "Host $($grp.machineName) hat $cpus CPUs mit $cpuCores CPU Cores vom Typ $cputype"
    $data = new-object psobject
    $data | add-member @{SystemName = $grp.SystemName;CPUType = $cputype;CPUs = $cpus;CPUcores =$cpuCores}
    $data
    }
    $NewOutput | Select SystemName, CPUType, CPUs, CPUCores | Export-CSV C:\temp\test.csv -NoTypeInformation
    

    【讨论】:

    • 由于内部 ForEach 没有任何输出(仅分配变量)$NewOutput 为空,无法将任何内容导出到文件中。屏幕输出结果来自$data 否则 (+1)
    【解决方案2】:

    基于scepticalist 很好的答案,但是
    我会使用 [PSCustomObject] (PSv3+) 并将输出分配给外部 ForEach:

    ## Q:\Test\2018\12\11\SO_53722443.ps1
    $csvPath = ".\Hardware.csv"
    $csv = Import-Csv $csvPath | Group-Object SystemName
    
    $NewOutput = ForEach ($obj in $csv) {
        $cpuCores = $cpus = $cputype = $Null
        ForEach ($grp in $obj.Group){
            if ($grp.key -eq "cores"){
                $cpuCores += [int]$grp.Value
            }
            if ($grp.key -eq "CPU") {
                $cpus++
                $cputype = $grp.Value
            }
        }
        [PSCustomObject]@{
            SystemName = $grp.SystemName
            CPUType    = $cputype
            CPUs       = $cpus
            CPUcores   = $cpuCores
        }
    }
    $NewOutput
    $NewOutput | Export-CSV .\test.csv -NoTypeInformation
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-29
      • 2022-01-22
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2018-02-23
      相关资源
      最近更新 更多