我有一个关于处理我从VMWARE vROPS 6.X中提取的数据的问题。
基本上我编写了一个脚本来从套件API中提取指标数据。我几乎有我想要的格式,但我需要将Metric和Timestamp列进一步分开。
基本上我构建了一个foreach
循环然后嵌套了另一个循环,但我没有按正确的顺序获取指标和时间戳(所以我从下面的代码中删除了它)。
当前输出:
"resourceId","Timestamp","METRIC","value" "ef951a38-3063-477d-af32-baa6d2744357","1466085599999 1466171999999","cpu:1|costop_summation","4.6296298710836307E-4 0.0 4.5836298710836307E-4 0.0" "ef951a38-3063-477d-af32-baa6d2744357","1466085599999 1466171999999","mem|usage_average","12.678446789582571 15.390000343322754"
期望的输出:
"resourceId","Timestamp","METRIC","value" "ef951a38-3063-477d-af32-baa6d2744357","1466085599999","cpu:1|costop_summation","4.6296298710836307E-4 0.0" "ef951a38-3063-477d-af32-baa6d2744357","1466171999999","cpu:1|costop_summation","4.5836298710836307E-4 0.0" "ef951a38-3063-477d-af32-baa6d2744357","1466085599999","mem|usage_average","12.678446789582571" "ef951a38-3063-477d-af32-baa6d2744357","1466171999999","mem|usage_average","15.390000343322754"
我的代码:
#Call vROPS SUITE-API with Invoke-Rest
#Take all certs.
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
#date
[DateTime]$StartDate = (Get-date).adddays(-5)
[DateTime]$EndDate = (Get-date)
$StartDateEpoc = Get-Date -Date $StartDate -UFormat %s
$EndDateEpoc = Get-Date -Date $EndDate -UFormat %s
#Variables
$username = "admin"
$password = "password"
$secPw = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object PSCredential -ArgumentList $username,$secPw
$ContentType = "application/xml;charset=utf-8"
$header = New-Object "System.Collections.Generic.Dictionary[[String,[String]]"
$header.Add("Accept", 'application/xml')
#intervalType=
$SECONDS = 'SECONDS'
$MINUTES = 'MINUTES'
$HOURS = 'HOURS'
$DAYS = 'DAYS'
#rollUpType=
$AVG = 'AVG'
$MAX = 'MAX'
$SUM = 'SUM'
$MIN = 'MIN'
$COUNT = 'COUNT'
Invoke-RestMethod -Method GET -uri "https://192.168.0.125/suite-api/api/resources/stats?resourceId=ef951a38-3063-477d-af32-baa6d2744357&resourceId=1ef459e5-789e-446b-9852-3dc92c43e74a&statKey=cpu|usage_average&rollUpType=$AVG&intervalType=$DAYS" -Credential $cred -ContentType $ContentType -Headers $header -OutFile d:\quickcheck.xml
[xml]$Data = Get-Content 'D:\quickcheck.XML'
$report = @()
$resources = $Data.'stats-of-resources'
$UUIDS = $Resource.'resourceId'
foreach ($Resource in $Resources.'stats-of-resource') {
foreach ($node in $Resource.'stat-list'.stat)
{
#Collection Date, not run time
$MetricName = $node.statKey.Key
$Values = @($node.data -replace '( \d\.\d) ',"`$1`n" -split "`n")
$Timestamps = @($node.timestamps -split ' ')
for ($i=0; $i -lt $Values.Count -and $i -lt $Timestamps.Count; $i++) {
$report += New-Object PSObject -Property @{
METRIC = $MetricName
resourceId = $Resource.'resourceId'
Timestamp = $Timestamps[$i]
value = $Values[$i]
}
}
}
$report | Export-Csv D:\reprop.csv -NoTypeInformation
答案 0 :(得分:0)
在空格处分割$Timestamps
,在每隔一个空格分割$Values
,并为每个结果的片段对创建一个对象,例如像这样:
$Values = @($Values -split ' ')
$Timestamps = @($Timestamps -split ' ')
for ($i=0; $i -lt $Values.Count -and $i -lt $Timestamps.Count; $i++) {
$report += New-Object PSObject -Property @{
METRIC = $MetricName
resourceId = $Resource.'resourceId'
Timestamp = $Timestamps[$i]
value = $Values[$i]
}
}