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?
Hi Lucd,
This was the result for the script.
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:
Do the logoff events produce a similar object?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes lucd...Logoff event also has the similar object.
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
$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
It could be that the events need to be ordered chronologically first.
Try replacing that last part with
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
Hi Lucd,
Negative values are not showing. But the timeformat is terrible. Is it possible to make it in HH:MM format?
Try replacing that line with
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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?
Try with this
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
Perfect Lucd...Finally you made it. :smileycool:
Edit the script with .ToString("hh':'mm") for session duration.
Keep rocking.