티스토리 뷰
다음을 위해 Powershell V2가 필요합니다.
$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
-------------------이 작업을 수행하는 Join-Object
PoshCode 에는 함수가 있지만 내부에 묻혀 내 Join-Collection
보내지 않습니다.
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
}
}
정의한 후에는 다음과 같이 사용할 수 있습니다.
Join-Object (Get-WmiObject -Class Win32_Processor) (Get-WmiObject -Class Win32_BaseBoard) |
Select Name, Description, Manufacturer, Product
또는 변수를 유지하고 다음과 같이하십시오.
$cpu = Get-WmiObject -Class Win32_Processor
$mb = Get-WmiObject -Class Win32_BaseBoard
Join-Object $cpu $mb | Select Name, Description, Manufacturer, Product
-------------------이건 어때?
echo $cpu $mb | Select-Object Name, Description, Manufacturer, Product | ConvertTo-Csv -NoTypeInformation
-------------------필요한 속성이있는 사용자 지정 powershell 개체에 필요한 속성을 언제든지 추가 한 다음 cvs를 덤프 할 수 있습니다. 이 페이지 를 보시고 필요한 것이 있는지 확인하십시오.
-------------------이것이 당신이 찾고있는 것입니까? $ cpu와 $ mb 모두 Name 및 Manufacturer 속성이 있기 때문에 예상했던 것보다 약간 더 길었습니다. 그러나 주요 아이디어는 해싱을 통한 사용자 지정 개체입니다.
($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
-------------------두 개체 중 하나에서 시작한 다음 다른 개체에서 속성을 추가 할 수 있습니다.
$cpu | Select-Object Name, Description |
add-member noteproperty Manufacturer $mb.Manufacturer -PassThru |
add-member noteproperty Product $mb.Product -PassThru
-------------------Join-Object
, 설명 에서 파워 쉘, 하나에 두 개의 테이블을 조인하는 가장 좋은 방법은 무엇인가? 더 많은 기능이 있지만 다음과 같이 사용할 수도 있습니다.
$cpu | Select-Object Name, Description | Join-Object ($mb | Select-Object Manufacturer, Product)
출처
https://stackoverflow.com/questions/1903233