我有一個代碼,它給我如下輸出
ESXi Datastore ReadIOPS WriteIOPS ReadLatency[ms] WriteLatency[ms] ReadRate[KBps] WriteRate[KBps]
---- --------- -------- --------- --------------- ---------------- -------------- ---------------
999_TEST8AP EV_8190 0 1 0 3 0 13
ESXi Datastore ReadIOPS WriteIOPS ReadLatency[ms] WriteLatency[ms] ReadRate[KBps] WriteRate[KBps]
---- --------- -------- --------- --------------- ---------------- -------------- ---------------
999_TANS2AP DEV_8190 0 2 0 1 0 14
我需要在 html 中撰寫此輸出。請讓我知道如何為每個服務器只顯示一個標題而不是多個標題。
也請幫我把它寫成 html。未來將有多個資料存盤和虛擬機,我需要在其中執行此代碼
這是代碼
[CmdletBinding()]
param(
[String]$vcenter=""
)
Begin {
#Ignore invalid certificate
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false -Verbose
try {
#Connect to VCSA
Connect-VIServer -Server $vcenter
}
catch {
Write-Host "Incorrect vCenter creds!"
$PSCmdlet.ThrowTerminatingError($PSItem)
}
#Collect list of datastore names
if (-not (test-path -path C:\datastore_list.txt)) {
Disconnect-VIServer $vcenter -Confirm:$false
Write-Error 'The file datastore_list.txt does not exist!' -ErrorAction Stop
}
else {
$list1 = Get-Content C:\datastore_list.txt
}
#Collect list of ESXi servers
if (-not (test-path -path C:\esxi_list.txt)) {
Disconnect-VIServer $vcenter -Confirm:$false
Write-Error 'The file esxi_list.txt does not exist!' -ErrorAction Stop
}
else {
$list2 = Get-Content C:\esxi_list.txt
}
}
Process {
#Function to collect datastore performance stats
Function datastore_perf ($host_name) {
$stat_array =@()
For ($i = 0; $i -lt $list1.count; $i ){
$datastore_name = $list1
if($datastore_name) {
$instance_id = (Get-Datastore $datastore_name).ExtensionData.Info.Vmfs.Uuid
$t1 = Get-Stat -Entity $host_name -Stat datastore.numberReadAveraged.average -MaxSamples 1 -Realtime -Instance $instance_id
$t2 = Get-Stat -Entity $host_name -Stat datastore.numberWriteAveraged.average -MaxSamples 1 -Realtime -Instance $instance_id
$t3 = Get-Stat -Entity $host_name -Stat datastore.totalReadLatency.average -MaxSamples 1 -Realtime -Instance $instance_id
$t4 = Get-Stat -Entity $host_name -Stat datastore.totalWriteLatency.average -MaxSamples 1 -Realtime -Instance $instance_id
$t6 = Get-Stat -Entity $host_name -Stat datastore.read.average -MaxSamples 1 -Realtime -Instance $instance_id
$t7 = Get-Stat -Entity $host_name -Stat datastore.write.average -MaxSamples 1 -Realtime -Instance $instance_id
$stat_object = New-Object System.Object
$read_iops = $t1[0].Value
$write_iops = $t2[0].Value
$read_latency = $t3[0].Value
$write_latency = $t4[0].Value
$read_avg = $t6[0].Value
$write_avg = $t7[0].Value
$stat_object | Add-Member -Type NoteProperty -Name ESXi -Value "$host_name"
$stat_object | Add-Member -Type NoteProperty -Name Datastore -Value "$datastore_name"
$stat_object | Add-Member -Type NoteProperty -Name ReadIOPS -Value "$read_iops"
$stat_object | Add-Member -Type NoteProperty -Name WriteIOPS -Value "$write_iops"
$stat_object | Add-Member -Type NoteProperty -Name ReadLatency[ms] -Value "$read_latency"
$stat_object | Add-Member -Type NoteProperty -Name WriteLatency[ms] -Value "$write_latency"
$stat_object | Add-Member -Type NoteProperty -Name ReadRate[KBps] -Value "$read_avg"
$stat_object | Add-Member -Type NoteProperty -Name WriteRate[KBps] -Value "$write_avg"
$stat_array = $stat_object
}
}
cls
$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@
$stat_array | ConvertTo-Html -head $Header | Out-File -FilePath C:\stat_report.html -Append
}
}
}
End {
$result = $list2 | ForEach-Object { datastore_perf -host_name $PSItem }
}
html 示例

uj5u.com熱心網友回復:
我會在您的代碼中更改一些內容:
$datastore_name = $list1-->$datastore_name = $list1[$i]- 不要
=用于添加到陣列,因為這是時間和記憶體成本(在每次迭代中,整個陣列都需要以這種方式構建)。相反,讓您的函式簡單地輸出一個 PsCustomObject 并在變數中收集所有內容$result ConvertTo-Html用換行符加入字串陣列,不用于-Append撰寫輸出 HTML 檔案- 優先
Set-Content于Out-File,因為在 PowerShell 5.1 及以下版本中,Out-File 的編碼是 Unicode (UTF16-LE),這可能不是您想要的。
嘗試
[CmdletBinding()]
param(
[String]$vcenter=""
)
Begin {
#Ignore invalid certificate
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false -Verbose
try {
#Connect to VCSA
Connect-VIServer -Server $vcenter
}
catch {
Write-Host "Incorrect vCenter creds!"
$PSCmdlet.ThrowTerminatingError($PSItem)
}
#Collect list of datastore names
if (-not (test-path -Path 'C:\datastore_list.txt')) {
Disconnect-VIServer $vcenter -Confirm:$false
Write-Error 'The file datastore_list.txt does not exist!'
}
else {
$list1 = Get-Content -Path 'C:\datastore_list.txt'
}
#Collect list of ESXi servers
if (-not (test-path -path C:\esxi_list.txt)) {
Disconnect-VIServer $vcenter -Confirm:$false
Write-Error 'The file esxi_list.txt does not exist!'
}
else {
$list2 = Get-Content 'C:\esxi_list.txt'
}
}
Process {
#Function to collect datastore performance stats
function datastore_perf ($host_name) {
for ($i = 0; $i -lt $list1.count; $i ){
$datastore_name = $list1[$i]
if($datastore_name) {
$instance_id = (Get-Datastore $datastore_name).ExtensionData.Info.Vmfs.Uuid
$t1 = Get-Stat -Entity $host_name -Stat datastore.numberReadAveraged.average -MaxSamples 1 -Realtime -Instance $instance_id
$t2 = Get-Stat -Entity $host_name -Stat datastore.numberWriteAveraged.average -MaxSamples 1 -Realtime -Instance $instance_id
$t3 = Get-Stat -Entity $host_name -Stat datastore.totalReadLatency.average -MaxSamples 1 -Realtime -Instance $instance_id
$t4 = Get-Stat -Entity $host_name -Stat datastore.totalWriteLatency.average -MaxSamples 1 -Realtime -Instance $instance_id
$t6 = Get-Stat -Entity $host_name -Stat datastore.read.average -MaxSamples 1 -Realtime -Instance $instance_id
$t7 = Get-Stat -Entity $host_name -Stat datastore.write.average -MaxSamples 1 -Realtime -Instance $instance_id
# simply output an object with the properties you need
# this output will be collected below in variable $result
[PsCustomObject]@{
'ESXi' = $host_name
'Datastore' = $datastore_name
'ReadIOPS' = $t1[0].Value
'WriteIOPS' = $t2[0].Value
'ReadLatency[ms]' = $t3[0].Value
'WriteLatency[ms]' = $t4[0].Value
'ReadRate[KBps]' = $t6[0].Value
'WriteRate[KBps]' = $t7[0].Value
}
}
}
}
$result = $list2 | ForEach-Object { datastore_perf -host_name $_ }
$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@
# write the HTML file
($result | ConvertTo-Html -Head $Header) -join [environment]::NewLine | Set-Content -Path 'C:\stat_report.html'
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372252.html
標籤:电源外壳
下一篇:如何使網格填充視窗?
