开发者

Get Logged on Users from a List of Computer Names

开发者 https://www.devze.com 2023-03-18 09:22 出处:网络
I wanted to extract a list of users logged on to remote pc, the ps names would be fed in using a .csv file.

I wanted to extract a list of users logged on to remote pc, the ps names would be fed in using a .csv file.

I was able to get a command

Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer | Select 开发者_JAVA技巧Antecedent -Unique

to query the user names, could any one help me more on how to write this code?


Assuming the csv file contains a ComputerName header:

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

精彩评论

暂无评论...
验证码 换一张
取 消