Home > Mobile >  query from server users | Powershell msg
query from server users | Powershell msg

Time:12-01

query user /server:$SERVER

how can I filter only active users, for send to each user in server msg

$Session_Users = query user /server:$SERVER | Where-Object {$_ -match "Active"} 
$Session_Users


ForEach ($user in $Session_Users){

$TheMessage = "X"


& msg $user /v /Time:120 $TheMessage
}

btw I cant do & msg * , due security issue in server I can only to active users that why I need to filter them.

CodePudding user response:

You can use a similar technique to an old answer of mine: Using Qwinsta to export a shortened list of disconnected sessions

That example should work to get the users, or you can substitute query and do something like this:

function Get-RemoteUser
{
    param ($ComputerName = "ServerABC")

    query user /server:$ComputerName |
        ForEach-Object {$_ -replace "\s{2,18}","," } | 
            ConvertFrom-Csv
}

$computer = 'ServerDEF'
$message = 'Hello'

Get-RemoteUser -ComputerName $computer |
    Where-Object State -eq 'Active' |
        ForEach-Object {
            msg $_.UserName /Server:$computer $message
        }

CodePudding user response:

$message = 'XY'
query user /server:$SERVER | Where-Object {$_ -match "Active"} |         
ForEach-Object {
            & msg $_.UserName $message
        }

This the logic behind, and is not work.

that part is right is give the right list of active users but I cant loop it user by user...

query user /server:$SERVER | Where-Object {$_ -match "Active"} 
  • Related