2017-06-12 41 views
2

我需要检查许多服务器的保修,但我在https://www.powershellgallery.com/packages/HPWarranty/2.6.2中找到的模块返回的输出似乎是散列表,第一列包含我想成为我的行的内容。PowerShell - 从散列表转置结果

下面的脚本将返回这个地方,每5行中的字段重复 - output1.csv:

TYPE System.Management.Automation.PSCustomObject

“组件”, “Codecount”
“的SerialNumber”, “CZ36092P5H”
“ProductNumber”, “727021-B21”
“OverallEntitlementStartDate”, “2016年3月4日”
“OverallEntitlementEndDate”, “2019年4月2日”
“ActiveEntitlement”, “真”
“的SerialNumber”, “CZ36092P5K”
“ProductNumber”, “727021-B21”
“OverallEntitlementStartDate”, “2016年3月4日”
“OverallEntitlementEndDate”,“2019 -04-02"
“ActiveEntitlement”, “真”

我怎样才能转的输出,这样的SerialNumber,ProductNumber,OverallEntitlementStartDate,OverallEntitlementEndDate和ActiveEntitlement是列?

# variables 
$dest_path = "C:\Scripts\HPE\HPWarranty" 
$export_date = Get-Date -Format o | ForEach-Object {$_ -replace ":", "-"} 
$myScriptName = $MyInvocation.MyCommand.Name 
$transcriptPath = $dest_path + "\" + $myScriptName + "_transcript_" + $export_date + ".txt" 
$csvPath = $dest_path + "\" + "hpe_list1.csv" 

#Start transcript of script activities and set transcript location 
start-transcript -append -path $transcriptPath | Out-Null 

# import serials & part numbers to be processed 
$csv_info = Import-Csv $csvPath 

foreach ($line in $csv_info) { 
    $hash = (Get-HPEntWarrantyEntitlement -ProductNumber $line.ProductNumber -SerialNumber $line.SerialNumber) 
    &{$hash.getenumerator() | 
     ForEach-Object {new-object psobject -Property @{Component = $_.name;Codecount=$_.value}} 
    } | Export-Csv "C:\Scripts\HPE\HPWarranty\output1.csv" -Append 
} 

# Stop Transcript 
Stop-Transcript | Out-Null 

hpe_list1.csv脚本过程包含了两个服务器的详细信息:

ProductNumber,的SerialNumber
727021-B21,CZ36092P5H
727021-B21,CZ36092P5K

+0

使用[选择-对象(https://technet.microsoft.com/en-us/library/ee176955.aspx)列在管道 – t0mm13b

回答

2

将输出散列表投射到[pscustomobject]

$WarrantyInfo = foreach ($line in $csv_info) { 
    [pscustomobject](Get-HPEntWarrantyEntitlement -ProductNumber $line.ProductNumber -SerialNumber $line.SerialNumber) 
} 
$WarrantyInfo | Export-Csv "C:\Scripts\HPE\HPWarranty\output1.csv" 
+0

谢谢BenH这正是我需要的! – George