开发者

Powershell "join"

Call me stupid, but I'm losing my hair with this one.

I have two results from a Get-WmiObject:

$cpu = Get-WmiObject -Class Win32_Processor 
$mb = Get-WmiObject -Class Win32_BaseBoard

Now, I can filter and output a CSV file from each one:

$cpu | Select-Object Name, Description | ConvertTo-Csv -NoTypeInformation

and

$mb | Select-Object Manufacturer, Product | ConvertTo-Csv -NoTypeInformation

But... How the hell could I join these two outputs and make a single CSV output from both? Something like:

( 
  ($cpu | Select-Object Name, Description) + 
  ($mb | Select-Object Man开发者_如何转开发ufacturer, Product) 
) | ConvertTo-Csv -NoTypeInformation

(of course, this syntax is invalid. Just to show the point)


You need Powershell V2 for the following.

$cpu = Get-WmiObject -Class Win32_Processor 
$mb = Get-WmiObject -Class Win32_BaseBoard
$props = @{            
    Name          = $cpu.Name
    Description   = $cpu.Description
    Manufacturer  = $mb.Manufacturer
    Product       = $mb.Product
    }
New-Object PSObject -Property $props | ConvertTo-Csv -NoTypeInformation


There's a Join-Object function on PoshCode for doing this, but it's buried inside Join-Collection and not exported.

 function Join-Object {
    Param(
       [Parameter(Position=0)]
       $First
    ,
       [Parameter(Position=1,ValueFromPipeline=$true)]
       $Second
    )
    BEGIN {
       [string[]] $p1 = $First | gm -type Properties | select -expand Name
    }
    Process {
       $Output = $First | Select $p1
       foreach($p in $Second | gm -type Properties | Where { $p1 -notcontains $_.Name } | select -expand Name) {
          Add-Member -in $Output -type NoteProperty -name $p -value $Second."$p"
       }
       $Output
    }
 }

Once you've defined that, you can use it like this:

Join-Object (Get-WmiObject -Class Win32_Processor) (Get-WmiObject -Class Win32_BaseBoard) | 
Select Name, Description, Manufacturer, Product

Or keep your variables, and do it like:

$cpu = Get-WmiObject -Class Win32_Processor 
$mb = Get-WmiObject -Class Win32_BaseBoard

Join-Object $cpu $mb  | Select Name, Description, Manufacturer, Product


How about like this?

echo $cpu $mb | Select-Object Name, Description, Manufacturer, Product | ConvertTo-Csv -NoTypeInformation


You can always add whatever properties you need to a custom powershell object that has what you need and then dump the cvs. Take a look at this page and see if it suites what you need.


Is this what you are looking for? Somewhat longer then I expected, because both $cpu and $mb have Name and Manufacturer properties. But the prime idea is the custom objects through hashing.

($cpu | Select-Object Name, Description),($mb | Select-Object Manufacturer, Product) | 
       Select-Object @{name='NameOrManu';expr={$_.Name + $_.Manufacturer}},  
                     @{name='DescrOrProd';expr={$_.Description + $_.Product}} | 
       ConvertTo-Csv -NoTypeInformation


You can start with either object and then add properties from the other:

$cpu | Select-Object Name, Description | 
  add-member noteproperty Manufacturer $mb.Manufacturer -PassThru | 
  add-member noteproperty Product      $mb.Product      -PassThru


The Join-Object, described in In Powershell, what's the best way to join two tables into one? has a lot more capabilities, but can also be used like this:

$cpu | Select-Object Name, Description | Join-Object ($mb | Select-Object Manufacturer, Product)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜