将许多对象的输出组合成CSV

时间:2014-03-05 16:08:06

标签: powershell

我想获取四个对象的特定输出并将它们放入CSV中。

$obj1 = get-wmiobject win32_computersystem | select-object name,manufacturer,model,totalphysicalram
$obj2 = get-wmiobject win32_processor | select-object deviceid,name,maxclockspeed,numberofcores
$obj3 = get-wmiobject win32_bios | select-object serialnumber
$obj4 = get-wmiobject win32_operatingsystem | select-object osarchitecture

我想将所有这些组合到csv中的相同输出行。

如何使用PowerShell执行此操作?我应该构建一个对象数组吗?

3 个答案:

答案 0 :(得分:3)

我没有因为没有开始这个问题而对你提出错误,我不知道自己一年前去哪里使用代码。试试这个,看看它是如何打击你的......

$obj1 = get-wmiobject win32_computersystem | select-object name,manufacturer,model,TotalPhysicalMemory
$obj2 = get-wmiobject win32_processor | select-object deviceid,@{l="Proc";e={$_.name}},maxclockspeed,numberofcores
$obj3 = get-wmiobject win32_bios | select-object serialnumber
$obj4 = get-wmiobject win32_operatingsystem | select-object osarchitecture

$Combined = New-Object -Type PSObject
$obj1,$obj2,$obj3,$obj4|%{$CurObj = $_;$_|gm|?{$_.MemberType -match "NoteProperty"}|%{$NewMember = $_.Name;$Combined|Add-Member -MemberType NoteProperty -Name $NewMember -Value $CurObj.$NewMember}}

我修复了TotalPhysicalMemory(不是totalphysicalram),并更改了proc查询,因此它将Name属性重命名为Proc,这样就没有2个具有相同名称的属性。然后我将所有4个对象都送入ForEach循环,对于每个对象,我得到了它的成员并为NoteProperty成员进行过滤(去掉了Methods和什么不是)。对于每一个,我获取了成员的名称,并在我使用相同名称创建的新PSCustomObject中创建了一个属性,并从与该属性关联的原始对象中分配了值。我希望这是有道理的,我发现它有点难以遵循,我写了这个东西......

编辑: Dur ...我没有回答原来的问题,我刚完成了所有的准备工作。以下是输出方式:

$Combined|Export-CSV -Path C:\SomeFolder\MachineSpecs.CSV -NoTypeInfo

答案 1 :(得分:2)

我尝试在PSv3上运行TheMadTechnician的答案,但它不喜欢Out-CSV cmdlet。所以我用Export-CSV替换它,它工作正常。

$obj1 = get-wmiobject win32_computersystem | select-object name,manufacturer,model,TotalPhysicalMemory
$obj2 = get-wmiobject win32_processor | select-object deviceid,@{l="Proc";e={$_.name}},maxclockspeed,numberofcores
$obj3 = get-wmiobject win32_bios | select-object serialnumber
$obj4 = get-wmiobject win32_operatingsystem | select-object osarchitecture

$Combined = New-Object -Type PSObject
$obj1,$obj2,$obj3,$obj4|%{$CurObj = $_;$_|gm|?{$_.MemberType -match "NoteProperty"}|%{$NewMember = $_.Name;$Combined|Add-Member -MemberType NoteProperty -Name $NewMember -Value $CurObj.$NewMember}}
$Combined | Export-CSV -Path H:\MachineSpecs.CSV -NoTypeInfo

给出的输出: “制造商”, “型号”, “姓名”, “TotalPhysicalMemory”, “设备ID”, “maxclockspeed”, “numberofcores”, “处理”, “SERIALNUMBER”, “osarchitecture” “Hewlett-Packard”,“HP EliteBook 8760w”,“LAUCA-44A135”,“8537874432”,“CPU0”,“2201”,“4”,“Intel(R)Core(TM)i7-2670QM CPU @ 2.20GHz ”, “USH244A135”, “64位”

答案 2 :(得分:1)

这是我喜欢这个任务的可读语法,希望这会有所帮助。这将创建属性的哈希表并将其添加到PSObject,然后将其添加到数组。您可以根据需要使用生成的数组。我通常在针对远程计算机列表的foreach循环中运行它。您可以通过这种方式轻松汇总各种不同的数据进行报告。

$Results = @()

$obj1 = get-wmiobject win32_computersystem 
$obj2 = get-wmiobject win32_processor
$obj3 = get-wmiobject win32_bios
$obj4 = get-wmiobject win32_operatingsystem

  $Obj = new-object psobject -property@{
       PropertyName = $Obj2.Property
       SomeOtherPropertName = $obj1.Property
       SerialNumber = $obj3.SerialNumber
       DateCollected = (get-date).DateTime
       OSArchitecture = $obj4.OsArchitecture
  }

$Results+=$obj