用下面这个函数能获取远程或本地电脑的当前登录用户,同时附加了它的计算机名,所以当你查询多台电脑时将知道结果从哪里来。
1
2
3
4
5
6
7
8
9
10
|
function
Get-LoggedOnUser
{
param
(
[String[]]
$ComputerName
=
$env:COMPUTERNAME
)
$ComputerName
|
ForEach-Object
{
(quser /SERVER:
$_
)
-replace
'\s{2,}'
,
','
|
ConvertFrom-CSV
|
Add-Member
-MemberType
NoteProperty
-Name
ComputerName
-Value
$_
-PassThru
}
}
|
使用方法:
1 将代码保存到C盘比如是Get-LoggedOnUser.ps1
2启动powershell控制台
Import-Module C:\.\Get-LoggedOnUser.ps1
3 使用方法:
本文转自handsome7038 51CTO博客,原文链接:http://blog.51cto.com/lixiaosong/1357022