Hi All,

I am trying to export which user is logged in on which machine, via PC names.

Any idea why below script is not working ? I am stumped :frowning:

$computername = Get-Content computers.txt
foreach ($pc in $computername){
$logged_in = (Get-WmiObject -class win32_process -ComputerName $pc | Where-Object name -Match explorer).getowner().user
$name = $logged_in.split("\")[1]
"{0}: {1}" -f $pc,$name | Out-File -Append User_On_PC.txt
}

Many Thanks.

7 Spice ups

Maybe something like that?

Foreach($computername in (Get-Content computers.txt)){
    $loggedusers = Invoke-Command -ComputerName $computername -ScriptBlock{
                       quser
                   }
    Write-Output ($computername + ":") | Out-File c:\somepath\file.txt -Append
    Write-Output $loggedusers          | Out-File c:\somepath\file.txt -Append
}
1 Spice up

If you run this for a single machine,

(Get-WmiObject -class win32_process -ComputerName $insertNameHere | Where-Object name -Match explorer).getowner().user

^ does this actually give you a ‘domain\username’ output? I just get the ‘username’

If you try to split on the and then just take the second ( [1] ) arrayobject, since it did not split, it might be blank.

(Get-WmiObject -class win32_process -ComputerName 'localhost' | Where-Object name -Match explorer).getowner().user

This only gives me the current user without the domain. So there is nothing to split afterwards on the next line.

simply

$comp=get-content C:\computers.txt
Get-WMIObject -class Win32_ComputerSystem -ComputerName $comp | select  username,pscomputername

or use SoftPerfect Network Scanner : fast, flexible, advanced

2 Spice ups
foreach ($item in $Output)
{
# check online status of computer
If ((Test-Connection -ComputerName $item.Computername -Count 1 -Quiet) -eq $true) 
    {
        Try # grab machine and logged on User
            {
            Get-WmiObject -ComputerName "$($item.Computername)" -Namespace root\cimv2 -Class Win32_ComputerSystem -ErrorAction Stop| % {"$($_.Name): $($_.username)"}
            } # Move to next computer
        Catch 
            {
            Write-Host "$($item.Computername): COMMUNICATION ISSUE" -foregroundcolor red #current user running script does not have administrator rights
            }
    }
Else
    { 
     Write-Host "$($item.Computername): OFFLINE" -foregroundcolor yellow
     Continue # Move to next computer
    }

}

Currently using this

Why my script does not check for is users that have been switched out it only checks the current logged in user

Get-CIMInstance can indicate other logged in users as the user profile is in use (loaded )

PSTerminalServices module also works on PCs and can get those sessions that are disconnected on a PC

https://psterminalservices.codeplex.com/

1 Spice up

I was looking for a similar thing a while back.

1 Spice up

You can track users logon session as well as logged-In machine through event logs.

Please check this How-To for needful steps - https://community.spiceworks.com/how_to/130398-how-to-track-user-logon-sessions-using-event-log

1 Spice up

@ Neally - Invoke command does not work as Psremoting is disabled :frowning:

@britv8 - your script doesn’t work for me mate, i modified it though;

$computer = Get-Content computers.txt
foreach ($item in $computer)
{
# check online status of computer
If ((Test-Connection -ComputerName $item.Computername -Count 1 -Quiet) -eq $true) 
    {
        Try # grab machine and logged on User
            {
            Get-WmiObject -ComputerName "$($item.Computername)" -Namespace root\cimv2 -Class Win32_ComputerSystem -ErrorAction Stop| % {"$($_.Name): $($_.username)"| Out-File -Append User_On_PC.txt}
            } # Move to next computer
        Catch 
            {
            Write-Host "$($item.Computername): COMMUNICATION ISSUE" -foregroundcolor red| Out-File -Append Issue_On_PC.txt #current user running script does not have administrator rights
            }
    }
Else
    { 
     Write-Host "$($item.Computername): OFFLINE" -foregroundcolor yellow | Out-File -Append PC_Offline.txt
     Continue # Move to next computer
    }

}

@ Jitensh - Yours works alright, with most PC’s. For some PC’s i get blank as a username.

Thanks all for your help though.

@alexw @britv8 @jitensh

Well if Jiten’s works, add validation, it is most likely blank if it can not connect to the PC.

also you can use

Get-WmiObject -Class win32_process -ComputerName $computername | 
    Where-Object{ $_.Name -eq "explorer.exe" } | 
    ForEach-Object{ ($_.GetOwner()).Domain + "\" + ($_.GetOwner()).User; }

becasue you are using get-content and not a CSV you will need to replace $Item.Computername with $item throughout my script

try this

$computer = Get-Content computers.txt
foreach ($item in $computer)
{
# check online status of computer
If ((Test-Connection -ComputerName $item -Count 1 -Quiet) -eq $true) 
    {
        Try # grab machine and logged on User
            {
            Get-WmiObject -ComputerName "$item" -Namespace root\cimv2 -Class Win32_ComputerSystem -ErrorAction Stop| % {"$($_.Name): $($_.username)"| Out-File -Append User_On_PC.txt}
            } # Move to next computer
        Catch 
            {
            Write-Host "$item : COMMUNICATION ISSUE" -foregroundcolor red| Out-File -Append Issue_On_PC.txt #current user running script does not have administrator rights
            }
    }
Else
    { 
     Write-Host "$item : OFFLINE" -foregroundcolor yellow | Out-File -Append PC_Offline.txt
     Continue # Move to next computer
    }

}
1 Spice up

@ britv8 - cheers bud - quick one, do you know why below results are not exporting ?

Write-Host "$item : OFFLINE" -foregroundcolor yellow | Out-File -Append PC_Offline.txt

Write-Host "$item : COMMUNICATION ISSUE" -foregroundcolor red | Out-File -Append Issue_On_PC.txt

It seems out-file is not working for PC’s offline.

@JitenSh - i like what you did there, how can I replace

$_.GetOwner()).Domain

with

$_.GetOwner()).Computername

Then it be perfect.

@ Neally - I found the reason i was getting blank. Because PC was sitting on lock screen and no one had logged into it.

Thanks All

@alexw @britv8 @jitensh

As you are really wanting to output to file you should use write-output

Write-Output "$item : OFFLINE" | Out-File -Append PC_Offline.txt

write-host is really only for output to the console

1 Spice up

Ta mate - working like a charm.

@britv8