服务器用户查询|Powershell消息

p5cysglq  于 2022-12-18  发布在  Shell
关注(0)|答案(2)|浏览(132)
query user /server:$SERVER

我怎样才能只过滤活动用户,发送到每个用户在服务器消息

$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
}

顺便说一句,我不能做& msg *,由于安全问题,在服务器上,我只能积极的用户,这就是为什么我需要过滤他们。

fdbelqdn

fdbelqdn1#

你可以用一个类似的技巧来回答我以前的一个问题:Using Qwinsta to export a shortened list of disconnected sessions
这个例子应该可以用来获取用户,或者你可以替换query并做如下操作:

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
        }
y1aodyip

y1aodyip2#

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

这背后的逻辑,是行不通的。
那部分是正确的是给予出正确的活跃用户列表,但我不能一个用户一个用户地循环它。

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

相关问题