简体   繁体   English

使用powershell收集远程计算机上登录的用户名?

[英]using powershell to gather logged on user names on remote computers?

$MachineList = Get-Content -Path "E:\ps\comp list\Test Computers.txt"; # One system name per line 
foreach ($Machine in $MachineList)
{ 
($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace root\cimv2 -Class Win32_ComputerSystem -erroraction silentlycontinue)[0].UserName); 

Write-Output ($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace root\cimv2 -Class Win32_ComputerSystem -erroraction silentlycontinue)[0].UserName) | Out-File "E:\ps\comp output\Test Computers.txt" -Append
}

Update: Here's the working script, thanks all for the help! 更新:这是工作脚本,感谢大家的帮助! :) It pulls in a list of computers prints to the screen and then also writes them to a file. :)它将计算机打印列表拉到屏幕上,然后还将它们写入文件中。

I found this powershell code and it works but I'd like it to display the machine name in front of the username when it displays. 我找到了该powershell代码,并且可以正常工作,但是我希望它在显示时在用户名前面显示计算机名称。 How can I get it to do that? 我怎样才能做到这一点?

So like - 就像-

MachineName: Username MachineName:用户名

I'm a powershell newb... any help would be appreciated! 我是新手...任何帮助将不胜感激! Thanks! 谢谢! :) :)

Try this 尝试这个

$MachineList = Get-Content -Path c:\ListOfMachines.txt; # One system name per line
foreach ($Machine in $MachineList){
    ($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName);
}

Try this: 尝试这个:

Get-Content -Path c:\ListOfMachines.txt | % {
  Get-WmiObject -ComputerName $_ -Namespace root\cimv2 -Class Win32_ComputerSystem -erroraction silentlycontinue | % {
    "$($_.Name): $($_.username)"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM