windows 从计算机名称列表中获取登录用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6607731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Logged on Users from a List of Computer Names
提问by PowerShell
I wanted to extract a list of users logged on to remote pc, the ps names would be fed in using a .csv file.
我想提取登录到远程 pc 的用户列表,将使用 .csv 文件输入 ps 名称。
I was able to get a command
我能够得到一个命令
Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer | Select Antecedent -Unique
to query the user names, could any one help me more on how to write this code?
查询用户名,有人可以帮助我更多地了解如何编写此代码吗?
采纳答案by Shay Levy
Assuming the csv file contains a ComputerName header:
假设 csv 文件包含一个 ComputerName 标头:
Import-Csv computers.csv | Foreach-Object{
Get-WmiObject Win32_LoggedOnUser -ComputerName $_.ComputerName | Select-Object __SERVER,Antecedent -Unique | Foreach-Object {
$domain,$user = [regex]::matches($_.Antecedent,'="([^"]+)"') | Foreach-Object {$_.Groups[1].Value}
$_ | Select-Object __SERVER,@{Name='Domain';Expression={$domain}},@{Name='User';Expression={$user}}
}
}