jamie20
Enthusiast
Enthusiast

Get-hvlocalsession for multiple users

Jump to solution

Hi guys,

I am trying to make a script which should give session details of multiple users such as username,login time and logout time.

I could get the required details for all the users by below script. But dont know how to get it for multiple users. ie by importing limited no of usernames in a text file.

Get-hvlocalsession | ForEach-Object -Process {

$dtformat = "dd-mm-yy HH:mm"

$ls = $_

'' | select @{N="Emp Id";E={$ls.NamesData.UserName.Replace("Domainname\","")}},

  @{N="Login Time";E={$ls.sessiondata.StartTime.ToString($dtformat)}},

  @{N="Logoff Time";E={$ls.sessiondata.DisconnectTime.ToString($dtformat)}}

}

Any help?

30 Replies
jamie20
Enthusiast
Enthusiast

Hi Lucd,

This was the result for the script.

s1.PNG

It just returned value 1.

Then I altered the script like.

$events = Get-HVEvent -HvDbServer $eventdb -TimePeriod day -MessageFilter "logged in to a new session"

$events.events | Select -First 1 | Format-Custom -Depth 2

It gave the result:

s2.PNG

0 Kudos
LucD
Leadership
Leadership

Do the logoff events produce a similar object?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jamie20
Enthusiast
Enthusiast

Yes lucd...Logoff event also has the similar object.

0 Kudos
LucD
Leadership
Leadership

Can you try something like this?
It assumes that the logoff events also have the name of the station at the end of the Message string

$array = @()

$events = Get-HVEvent -HvDbServer $eventdb -TimePeriod week -MessageFilter "logged in to a new session"

$array = $events.events |  ForEach-Object -Process {

    New-Object -TypeName PSObject -Property @{

        User = $_.UserName.Split('\')[1]

        Time = [DateTime]$_.EventTime

        Machine = $_.Message.Split(' ')[-1]

        Type = 'Logon'

    }

}


$events=Get-HVEvent -HvDbServer $eventdb -TimePeriod week -MessageFilter "logged off machine"

$array += $events.events |  ForEach-Object -Process {

    New-Object -TypeName PSObject -Property @{

        User = $_.UserName.Split('\')[1]

        Time = [DateTime]$_.EventTime

        Machine = $_.Message.Split(' ')[-1]

        Type = 'Logoff'

    }

}


$array | Group-Object -Property User,Machine -PipelineVariable group |

ForEach-Object -Process {

    $index = 0

    if($group.Group[0].Type -eq 'Logoff'){$index= 1}

    while($index -lt ($group.Group.Count - 1)){

        New-Object -TypeName PSObject -Property @{

            User = $group.Group[$index].User

            Machine = $group.Group[$index].Machine

            SessionDuration = (New-TimeSpan -Start $group.Group[$index].Time -End $group.Group[$index + 1].Time).TotalMinutes

        }

        $index += 2

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jamie20
Enthusiast
Enthusiast

Hi Lucd...I had attach the result in the attachment....Some session duration seems to be correct while others are in negative or 3,4 digit values.

0 Kudos
LucD
Leadership
Leadership

It could be that the events need to be ordered chronologically first.
Try replacing that last part with

$array | Group-Object -Property User,Machine -PipelineVariable group |

ForEach-Object -Process {

    $sorted = $group.Group | Sort-Object -Property Time

    $index = 0

    if($sorted[0].Type -eq 'Logoff'){$index= 1}

    while($index -lt ($sorted.Count - 1)){

        New-Object -TypeName PSObject -Property @{

            User = $sorted[$index].User

            Machine = $sorted[$index].Machine

            SessionDuration = (New-TimeSpan -Start $sorted[$index].Time -End $sorted[$index + 1].Time).TotalMinutes

        }

        $index += 2

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jamie20
Enthusiast
Enthusiast

Hi Lucd,

Negative values are not showing. But the timeformat is terrible. Is it possible to make it in HH:MM format?

0 Kudos
LucD
Leadership
Leadership

Try replacing that line with

SessionDuration = (New-TimeSpan -Start $sorted[$index].Time -End $sorted[$index + 1].Time).ToString("hh':'mm")


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jamie20
Enthusiast
Enthusiast

Working fine lucd...But without login , logout date and time the output seems meaningless , as it is a weekly report. :smileycry:

Is it possible to add logintime , logofftime property?

0 Kudos
LucD
Leadership
Leadership

Try with this

$array | Group-Object -Property User,Machine -PipelineVariable group |

ForEach-Object -Process {

    $sorted = $group.Group | Sort-Object -Property Time

    $index = 0

    if($sorted[0].Type -eq 'Logoff'){$index= 1}

    while($index -lt ($sorted.Count - 1)){

        New-Object -TypeName PSObject -Property @{

            User = $sorted[$index].User

            Machine = $sorted[$index].Machine

            LogonTime = $sorted[$index].Time

            LogoffTime = $sorted[$index + 1].Time

            SessionDuration = (New-TimeSpan -Start $sorted[$index].Time -End $sorted[$index + 1].Time).TotalMinutes

        }

        $index += 2

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jamie20
Enthusiast
Enthusiast

Perfect Lucd...Finally you made it. :smileycool:

Edit the script with .ToString("hh':'mm") for session duration.

Keep rocking.

0 Kudos