我有一个包含数据的csv文件(示例如下(:
+----------+----------+-------------------------------+
| Name | Status | Time |
+----------+----------+-------------------------------+
| Service1 | Running | "2019/10/20 07:49:40,836 GMT" |
| Service1 | Shutdown | "2019/10/15 20:07:45,021 GMT" |
| Service1 | Running | "2019/10/10 20:07:45,021 GMT" |
+----------+----------+-------------------------------+
使用这个值,我必须计算给定名称在过去30天中处于运行状态的时间。使用文件中的一个条目进行计算不是问题,但给定的名称在列名中可以出现任意次数。上面的例子只是一个简单的例子,名称Service1 2019/10/10正在运行,但2019/10/15处于关闭状态,所以这两次都没有激活。下一个活动时间发生在2019/10/20,从这个时间到当前日期都是活动的。现在我想计算一下Service1在过去30天中处于活动状态(正在运行(的时间。你能指出应该用哪种方法来计算这种差异吗?
试试这个:
$records = Import-CSV -Path 'D:service.csv' -Delimiter (Get-Culture).TextInfo.ListSeparator | select -Property @{n='Time';e={[datetime]::parseexact($_.Time.Replace('"',''),"yyyy/MM/dd HH:mm:ss,fff GMT",$null)}},* -Exclude 'Time' | Sort-Object -Property Name, Time
$completeTimeSpan = 30 * 24 * 60 * 60
$currentDateTime = Get-Date
for( $i = 0; $i -lt $records.Count; $i++ ) {
$serviceName = $records[$i].Name
$opStatus = 3 # 0 = down, 1 = up, 3 = start
$upTime = 0
$downTime = 0
$dateLastEvent = $null
while( $i -lt $records.Count -and $serviceName -eq $records[$i].Name ) {
$ts = New-TimeSpan -Start $records[$i].Time -End $currentDateTime
if( $ts.Days -le 30 ) {
if( $opStatus -eq 1 ) {
$ts = New-TimeSpan -Start $dateLastEvent -End $records[$i].Time
$uptime += $ts.TotalSeconds
if( $records[$i].Status -eq 'Shutdown' ) {
$opStatus = 0
}
}
elseif( $opStatus -eq 0 ) {
$ts = New-TimeSpan -Start $dateLastEvent -End $records[$i].Time
$downtime += $ts.TotalSeconds
if( $records[$i].Status -eq 'Running' ) {
$opStatus = 1
}
}
elseif( $opStatus -eq 3 ) {
if( $records[$i].Status -eq 'Shutdown' ) {
$opStatus = 0
}
else {
$opStatus = 1
}
$dateLastEvent = $records[$i].Time
}
}
$i++
if( $i -eq $records.Count -or $serviceName -ne $records[$i].Name ) {
"Service: $servicename Uptime: $upTime $(($uptime / $completeTimeSpan))% DownTime: $downTime $(($downtime / $completeTimeSpan))%"
if( $i -lt $records.Count ) {
$serviceName = $records[$i].Name
}
}
}
$i--
}