简体   繁体   中英

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. How can I get it to do that?

So like -

MachineName: Username

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)"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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