Excel报告在PowerShell中格式化

时间:2017-05-04 03:42:01

标签: excel powershell

需要帮助来创建脚本以获取HPOA服务器刀片运行状况报告 问题是,当我获得查询健康时,它会在PSO对象中输出字段(IP,健康,刀片(@ {Blade1 Health} {Blade2 Health} {3} ...)) 我想要一份如下的报告

IP Bay Power Health - --- ----- -----
10.3.131.2 1开OK               2亮OK               3亮OK               4亮确定               5降级

变量的推导如下。

$ sstaInfo = {} |选择IP,Bay,Power,Health,DeviceFailure
$ sstaInfo.IP = $ ssta.IP(提供单个IP输出) $ sstaInfo.Bay = $ sstaBlades.Bay $ sstaInfo.Power = $ sstaBlades.Power $ sstaInfo.Health = $ sstaBlades.Health

我怎样才能使这个工作?

$ ssta变量具有以下输出: @ {功率=开; CurrentWattageUsed = 480;健康= OK; UnitIdentificationLED =关闭; VirtualFan = 33%; DiagnosticStatus =; Bay = 1} @ {Power = On; CurrentWattageUsed = 576;健康= OK; UnitIdentificationLED =关闭; VirtualFan = 47%; DiagnosticStatus =;湾= 2}

#------------------------------------------------------------  Input    Variable Definations

$HPOAServers =@(
[pscustomobject]@{Name='10.11.12.13'},
[pscustomobject]@{Name='10.11.12.14'}
)

$Username ="admin"
$Password ="admin"

#------------------------------------------------------------  Main Script Starts Here

# Function for connecting to OA and returning connection object on success




foreach ($HPOAServer in $HPOAServers) {

$con = Connect-HPOA $HPOAServer.Name -username $Username -password $Password

$report = @()

$ssta = Get-HPOAServerStatus -Bay All $con  

                $sstaBlade=$ssta.Blade
                Write-Host $sstaBlade
                Foreach ($sstaBlades in $sstaBlade) {
                    $i++

                    $sstaInfo = {} | Select IP, Bay, Power, Health, DeviceFailure                  
                    $sstaInfo.IP=$ssta.IP
                    $sstaInfo.Bay=$sstaBlades.Bay
                    $sstaInfo.Power=$sstaBlades.Power
                    $sstaInfo.Health=$sstaBlades.Health
$sstaInfo.DeviceFailure=$ssta.Blade.DiagnosticStatus.DeviceFailure


                }




$report += $ssta | Select-Object -Property IP
$report += $ssta.Blade | Select-Object -Property Bay, Power, Health | Format-Table *                        
$report | out-file "HPOA_Health_Report.txt" -Append         

}
Disconnect-HPOA $con

2 个答案:

答案 0 :(得分:1)

我建议你改用Export-CSV,所以在行

之下
 $report | Out-File "HPOA_Health_Report.txt" -Append

将替换为:

 $report | Export-Csv "HPOA_Health_Report.csv" -Append -NoTypeInformation

答案 1 :(得分:0)

Function HPOA () {
try
{
        Remove-Item -Path $outputfile -Force
        foreach ($HPOAServer in $HPOAServers) 
        {
            $con = Connect-HPOA $HPOAServer.Name -username $Username -password $Password -ErrorAction 'Stop'
            $ssta = Get-HPOAServerStatus -Bay All $con

        $ssta.Blade | Foreach-Object { 
        $sstaInfo = $_

        $sstaInfo | Select-Object -Property @{Name="Chassis_IP_Address";Expression={$ssta.IP}},
            @{Name="Blade_Power_Status";Expression={$_.Power}},
            @{Name="Blade_Bay_Number";Expression={$_.Bay}},
            @{Name="Blade_Health_Status";Expression={$_.Health}},
            @{Name="Blade_Diagnostic_DeviceFailure_Status";Expression={$ssta.Blade.DiagnosticStatus.DeviceFailure}}

        } | ConvertTo-Html -Title " $HPOARegionName HPOA Health Report " -Head $Header -Body "<H2> $HPOARegionName HPOA Health Report </H2>" -As Table | Out-File -Append $outputfile 
        Disconnect-HPOA $con
    }


}

catch
{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-Host $ErrorMessage
    Write-Host $FailedItem

}

}

HPOA