PowerShell
PowerShell copied to clipboard
24692+13:29
Hi Warren First, many thanks for the script, it is very helpful.
From time to time the Idle Time appears as "24692+13:29". Then errors appear in the script. I have changed the following lines:
Line 138: From: if($array.count -lt 9){ To: if(($array.count -lt 9) -and ($sessions[$_] -notlike '24692+13:29')){
Line 149: From: $temp.IdleTime = $array[5] To: if ($array[5] -eq '24692+13:29'){ $temp.IdleTime = "0" } Else { $temp.IdleTime = $array[5] }
So, hope it helps others. Stefan
Fixed the 24692+13:29 problem including the parse idle part, Dropped the Array bit because it is superfluous:
alternative script part
#The output of query.exe is dynamic.
#We just split the string in fixed parts exept for the LogonTime, it's length is the rest of
the string
if($sessions[$_].length -gt 5){
$temp.Username = $sessions[$_].Substring(1,22).trim()
$temp.SessionName = $sessions[$_].Substring(23,19).trim()
$temp.Id = $sessions[$_].Substring(42,4).trim()
$temp.State = $sessions[$_].Substring(46,8).trim()
$temp.IdleTime = $sessions[$_].Substring(54,11).trim()
$temp.LogonTime = $sessions[$_].Substring(65).trim()
}
#parseIdleTime
#if specified, parse idle time to timespan
#quick function to handle minutes or hours:minutes
function Convert-ShortIdle {
param($string)
if($string -match "\:"){
[timespan]$string
}else{
New-TimeSpan -Minutes $string
}
}
if($parseIdleTime){
$string = $temp.idletime
#. means less than a minute
if($string -eq "24692+13:29" -or $string -eq "." -or $string -eq "none"){
$temp.idletime = [timespan]"0:00"
}
#to the left of + is days
elseif($string -match "\+"){
$days = New-TimeSpan -days ($string -split "\+")[0]
$hourMin = Convert-ShortIdle ($string -split "\+")[1]
$temp.idletime = $days + $hourMin
}
#hours and minutes
else{
$temp.idletime = Convert-ShortIdle $string
}
}