VMware Cloud Community
jamie20
Enthusiast
Enthusiast
Jump to solution

Get-hvlocalsession for multiple users

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?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Since Get-GVLocalSession doesn't take any parameters, I can only think of adding a Where-clause.

$users = 'user1','user2','user3'

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


Get-HVLocalSession |

Where{$users -contains $_.NamesData.UserName.Split('\')[1]} |

Select @{N="Emp Id";E={$_.NamesData.UserName.Split('\')[1]}},

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

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


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

View solution in original post

30 Replies
LucD
Leadership
Leadership
Jump to solution

Since Get-GVLocalSession doesn't take any parameters, I can only think of adding a Where-clause.

$users = 'user1','user2','user3'

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


Get-HVLocalSession |

Where{$users -contains $_.NamesData.UserName.Split('\')[1]} |

Select @{N="Emp Id";E={$_.NamesData.UserName.Split('\')[1]}},

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

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


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

jamie20
Enthusiast
Enthusiast
Jump to solution

Its working Lucd :smileyblush:

We are using SQL server. Is it possible to retrieve these same historic data through powercli?

For example session details (starttime,disconnecttime) of yesterday

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Isn't that possible via Get-HVEvent and then filtering on the required types of events?


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

As per your instruction. I tried below script

For Login Time:

$users = Get-Content -path D:\hlist.txt

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

$events.events | where{$users -contains $_.UserName} |

select @{N="Emp Id";E={$_.UserName.Split('\')[1]}},

@{N="Login Time";E={$_.eventtime}}

For Logoff time:

$users = Get-Content -path D:\hlist.txt

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

$events.events | where{$users -contains $_.UserName} |

select @{N="Emp Id";E={$_.UserName.Split('\')[1]}},

@{N="Logoff Time";E={$_.eventtime}}

Its working but I need both login and logout time in a single script. How to do that?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do the events also include the stationId?
Then you could all the events (logon and logoff) and sort on stationId and time.


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

No lucd...I couldnt find any id in the events.:smileyconfused:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you could still collect all events (logon & logoff) and sort them on time.

Consecutive entries should then (in theory) be the start and end of a session.
But this will obviously not work if a user connects from different stations and has parallel sessions.


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

In my environment users access from single station only.

Is it possible to merge the below two set of scripts as one script?

$users = Get-Content -path D:\hlist.txt

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

$events.events | where{$users -contains $_.UserName} |

select @{N="Emp Id";E={$_.UserName.Split('\')[1]}},

@{N="Login Time";E={$_.eventtime}}

$users = Get-Content -path D:\hlist.txt

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

$events.events | where{$users -contains $_.UserName} |

select @{N="Emp Id";E={$_.UserName.Split('\')[1]}},

@{N="Logoff Time";E={$_.eventtime}}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you check if the following produces a list with alternating Logon/Logoff lines for the same user?

If it does, the script could find the first Logon and pair it with the following Logoff in 1 record.
And calculate the difference between the 2 timestamps for the session duration.

$users = Get-Content -path D:\hlist.txt

$events = @()


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

    Add-Member -Name Type -Value 'logon' -MemberType NoteProperty -PassThru

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

    Add-Member -Name Type -Value 'logoff' -MemberType NoteProperty -PassThru


$events.events | where{$users -contains $_.UserName} |

select Type,

    @{N="Emp Id";E={$_.UserName.Split('\')[1]}},

    @{N="Time";E={$_.eventtime}} |

Group-Object -Property 'Emp Id' |

ForEach-Object -Process {

    $_.Group | Sort-Object -Property Time

}


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Seems something is missing in the above script. Got this after running that script.

ss.PNG

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, use NoteProperty.


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Its working lucd, but it gives time as a single output. How would I differentiate login and logout time separately?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You should have the Type property that says if it concerns a Logon or a Logoff.
If in the sorted list, these lines (Logon-Logoff) follow each other, a script can combine those.

How does the result look after the sort?


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Hi lucd, this is how the result is for two users.

s.JPG

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Type property seems to be empty.
I updated the code, you shouldn't be prompted anymore


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

jamie20
Enthusiast
Enthusiast
Jump to solution

Yes lucd, its not prompting now. But the Type is still empty :smileyconfused:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then I'm at my wit's end, seems to work for me.


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

:smileylaugh:  In my work, Am using lots of your scripts from that wit's end.

Ok lucd...how to convert datetime format in eventtime?

tostring() function is not working here.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

We could try to find out what is actually there in the objects that are returned.

Try running the following.

Note that this may produce quite a bit of output, perhaps better attach the result as a file instead of including it inline.

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

Select -First 1 | Format-Custom -Depth 2


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

Select -First 1 | Format-Custom -Depth 2


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

Reply
0 Kudos