VMware Horizon Community
EricNichols
Hot Shot
Hot Shot
Jump to solution

using powershell to find user sessions longer than X duration

I wan to return the session objects longer than 7 days but cant figure out the format of the duration parameter.

Get-RemoteSession -Duration ?

help says:

-Duration [<string>]

     Filter based on the duration of this session. Using the format: 'dd

     day(s) hh hour(s) mm minute(s) ss second(s)'. Zero values will be

     omitted.

Can someone show me an example?  I want to then look up that user in AD, retrieve their email address and send them an smtp email.

Here is what I have so far:

add-pssnapin vm*

$FromAddr="First_Last@domain.com"

$FromName="First Last"

$CCAddr="First_Last@domain.com"

$CCName="First Last"

$SMTPServer="smtp.mailserver.tld"

$VDISessionObjectArray=(Get-RemoteSession -duration >7 days)

foreach ($VDISessionObject in $VDISessionObjectArray)

{

$ADUserObject=get-aduser -filter 'samAccountName -like $(VDISessionObject).Username' -properties mail

$ToAddr=($ADUserObject).mail

$ToName=($ADUserObject).SurName+", "+($ADUserObject).GivenName

send-mailmessage -to "$ToName <$ToAddr>" -from "$FromName <$FromAddr>" -cc "$CCName <$CCAddr>" -subject "Please Logout" -body "please logout body" -smtpServer $SMTPServer

}

Reply
0 Kudos
1 Solution

Accepted Solutions
EricNichols
Hot Shot
Hot Shot
Jump to solution

I revisited this script recently and found a better way to determine duration of login using the startTime.

Get-RemoteSession | Where-Object {[datetime]::ParseExact($_.startTime,"ddd MMM dd HH:mm:ss PST yyyy",$null) -lt (Get-Date).AddDays(-$Duration)}

Here is the revised script:

add-pssnapin vm*

$FromAddr="First_Last@domain.com"

$FromName="First Last"

$CCAddr="First_Last@domain.com"

$CCName="First Last"

$SMTPServer="smtp.mailserver.tld"

$Duration="7"

$TimeZone="PST"

$Domain="domain.local"

$VDISessionObjectArray=(Get-RemoteSession | Where-Object {[datetime]::ParseExact($_.startTime,"ddd MMM dd HH:mm:ss $TimeZone yyyy",$null) -lt (Get-Date).AddDays(-$Duration)})

foreach ($VDISessionObject in $VDISessionObjectArray)

{

$samAccountName=$VDISessionObject.Username.TrimStart("$Domain")

$samAccountName=$samAccountName.TrimStart("\")

$ADUserObject=get-aduser -filter "samAccountName -like '$SamAccountName'" -properties mail

$ToAddr=($ADUserObject).mail

$ToName=($ADUserObject).SurName+", "+($ADUserObject).GivenName

send-mailmessage -to "$ToName <$ToAddr>" -from "$FromName <$FromAddr>" -cc "$CCName <$CCAddr>" -subject "Please Logout" -body "please logout body" -smtpServer $SMTPServer

}

View solution in original post

Reply
0 Kudos
5 Replies
sneddo
Hot Shot
Hot Shot
Jump to solution

I don't have access to a view environment, but I assume you tried the format that the message specified?

i.e. Get-RemoteSession -duration  '07 day(s) 00 hour(s) 00 minute(s) 00 second(s)'


Edit: I just realised this will likely return sessions less than this duration. You might need to get all sessions and filter with a where clause

Reply
0 Kudos
EricNichols
Hot Shot
Hot Shot
Jump to solution

I got this to work:

Get-RemoteSession | Where-Object {$_.duration -like '7 days*'}

but the object does not seem to be working

VDISessionObject : The term 'VDISessionObject' is not recognized as the name of a cmdlet, function, script file, or

operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and

try again.

At line:10 char:8

+ echo $(VDISessionObject).Username

+        ~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (VDISessionObject:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Any ideas?

Reply
0 Kudos
EricNichols
Hot Shot
Hot Shot
Jump to solution

That does not work and I know that there are some that equal 7 days:

PS C:\> Get-RemoteSession -duration  '7 day(s) 00 hour(s) 00 minute(s) 00 second(s)'

Get-RemoteSession : No output object matching the Duration parameter

At line:1 char:1

+ Get-RemoteSession -duration  '7 day(s) 00 hour(s) 00 minute(s) 00 second(s)'

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (vmware.view.pow...etRemoteSession:GetRemoteSession) [Get-RemoteSessi

   on], Exception

    + FullyQualifiedErrorId : No object,vmware.view.powershell.cmdlets.GetRemoteSession

Reply
0 Kudos
EricNichols
Hot Shot
Hot Shot
Jump to solution

OK, got it all working!

add-pssnapin vm*

$FromAddr="First_Last@domain.com"

$FromName="First Last"

$CCAddr="First_Last@domain.com"

$CCName="First Last"

$SMTPServer="smtp.mailserver.tld"

#$Duration currently limited to 1-8

$Duration="7"

$Domain="domain.local"

#$VDISessionObjectArray=(Get-RemoteSession | Where-Object {$_.duration -like "$Duration days*"})

$VDISessionObjectArray=(Get-RemoteSession | Where-Object {$_.duration -match "^[$Duration-9] days*|[\d]{2,3} days*"})

foreach ($VDISessionObject in $VDISessionObjectArray)

{

$samAccountName=$VDISessionObject.Username.TrimStart("$Domain")

$samAccountName=$samAccountName.TrimStart("\")

$ADUserObject=get-aduser -filter "samAccountName -like '$SamAccountName'" -properties mail

$ToAddr=($ADUserObject).mail

$ToName=($ADUserObject).SurName+", "+($ADUserObject).GivenName

send-mailmessage -to "$ToName <$ToAddr>" -from "$FromName <$FromAddr>" -cc "$CCName <$CCAddr>" -subject "Please Logout" -body "please logout body" -smtpServer $SMTPServer

}

Added separate trim for "\". Some AD accounts needed it, some didnt (weird)

Added regex to get any sesion equal to or longer than $Duration

Reply
0 Kudos
EricNichols
Hot Shot
Hot Shot
Jump to solution

I revisited this script recently and found a better way to determine duration of login using the startTime.

Get-RemoteSession | Where-Object {[datetime]::ParseExact($_.startTime,"ddd MMM dd HH:mm:ss PST yyyy",$null) -lt (Get-Date).AddDays(-$Duration)}

Here is the revised script:

add-pssnapin vm*

$FromAddr="First_Last@domain.com"

$FromName="First Last"

$CCAddr="First_Last@domain.com"

$CCName="First Last"

$SMTPServer="smtp.mailserver.tld"

$Duration="7"

$TimeZone="PST"

$Domain="domain.local"

$VDISessionObjectArray=(Get-RemoteSession | Where-Object {[datetime]::ParseExact($_.startTime,"ddd MMM dd HH:mm:ss $TimeZone yyyy",$null) -lt (Get-Date).AddDays(-$Duration)})

foreach ($VDISessionObject in $VDISessionObjectArray)

{

$samAccountName=$VDISessionObject.Username.TrimStart("$Domain")

$samAccountName=$samAccountName.TrimStart("\")

$ADUserObject=get-aduser -filter "samAccountName -like '$SamAccountName'" -properties mail

$ToAddr=($ADUserObject).mail

$ToName=($ADUserObject).SurName+", "+($ADUserObject).GivenName

send-mailmessage -to "$ToName <$ToAddr>" -from "$FromName <$FromAddr>" -cc "$CCName <$CCAddr>" -subject "Please Logout" -body "please logout body" -smtpServer $SMTPServer

}

Reply
0 Kudos