powershell - Send pop up message to specific RDP users who have not logged off -
i using function get-loggedonuser microsoft technet script center repository: get-loggedonuser
i'm invoking "msg" command (net send) on rdp server send pop message user has not logged out overnight.
i don't want broadcast message users on server; send notification individual users have been logged in since before midnight previous night.
want message show username , logon time , remind them log out.
$yesterday = [datetime]::today.tostring('m/d/yyyy hh:mm ') $notloggedout = get-loggedonuser -computername computername | where-object {$_.logontime -lt $yesterday} $script={param($command, $users, $computername, $logontime); write-host $command; &cmd /c "$command"} $command = foreach($user in $notloggedout){write-host "dear " $user.username "the system shows have been logged on since " $user.logontime "reminder: must log off @ end of everyday"} invoke-command -computername computername -scriptblock $script -argumentlist $command
$notloggedout shows 3 users should receive msg
username computername sessionname id state idletime logontime error -------- ------------ ------------ -- ----- -------- ------------ ----- user01 computername rdp-tcp#0 1 active 5 7/30/2015 9:39 user02 computername rdp-tcp#9 2 active 10 7/30/2015 8:46 user03 computername rdp-tcp#2 2 active 7/30/2015 8:46
user01 gets message.
but can't send message each user in foreach loop. user01.
i think way send arguments not right. should doing is: $script should script necessary send message users. -argumentlist should send $notloggedout variable. inside script part acesses $args[0].username (for first user , on).
try out. have rewritten code below, , works well. notice have commented out call get-loggedonuser , instead use static test data.
$midnight = [datetime]::today $notloggedout = @(@{username = "jower";computername = "jowerwin81";logontime= [datetime]::now.adddays(-2)}, @{username = "jower";computername = "jowerwin81";logontime= [datetime]::now.adddays(-1)}) | where-object {$_.logontime -lt $midnight} #get-loggedonuser -computername computername | where-object {$_.logontime -lt $midnight} $script={ foreach($user in $args) { $mess = ("dear " + $user.username + " system shows have been logged on since " + $user.logontime + " reminder: must log off @ end of everyday") write-host $mess & {msg $args.username /server:($args.computername) $args.message} -argumentlist @{username = $user.username;message=$mess} } } invoke-command -scriptblock $script -argumentlist $notloggedout
Comments
Post a Comment