我需要检查许多服务器的保修,但是在https://www.powershellgallery.com/packages/HPWarranty/2.6.2中找到的模块返回的输出似乎是哈希表,并且第一列包含我要作为行的内容。
下面的脚本将在字段每5行重复的地方返回此结果-output1.csv:

如何转置输出,以使SerialNumber,ProductNumber,TotalEntitlementStartDate,TotalEntitlementEndDate和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包含两个服务器的详细信息:

最佳答案

将输出哈希表强制转换为[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"

关于powershell - PowerShell-转置哈希表的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44499423/

10-13 09:10