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

时间:2013-11-14 16:32:26

标签: powershell

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

更新:这是工作脚本,感谢大家的帮助! :)它将计算机打印列表拉入屏幕,然后将它们写入文件。

我找到了这个powershell代码并且它可以工作,但我希望它在显示时在用户名前面显示机器名称。我怎么能这样做呢?

所以喜欢 -

MachineName:用户名

我是一个强大的新闻...任何帮助将不胜感激!谢谢! :)

2 个答案:

答案 0 :(得分:3)

试试这个

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

答案 1 :(得分:1)

试试这个:

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