Get Computer Name and Logged User Name

Powershell - Find computers with a specific username logged in

Continuing from my comment. . .

Here's an quick and dirty approach you can take:

Function Get-Username {
Clear-Host
$Global:Username = Read-Host "Enter Username"
if ($Username -eq $null){
Write-Host "Username cannot be blank, please enter a Username"
Get-Username
}
$UserCheck = Get-ADUser $Username
if ($UserCheck -eq $null){
Write-Host "Invalid Username, please verify this is the logon id for the account"
Get-Username
}
}
Get-Username
Function Get-Computername {
Clear-Host
$Global:Prefix = Read-Host "Enter Computername"
Clear-Host
}
Get-Computername

$computers = Get-ADComputer -Filter {Enabled -eq $true -and SamAccountName -like $Prefix}

$check = Get-CimInstance -Query "SELECT Name,UserName from Win32_ComputerSystem WHERE UserName LIKE '%$userName%'" `
-ComputerName $Computers.Name`
-ErrorAction SilentlyContinue
if ($check) {
# $check.UserName/Name may return an array same name users are found.
Write-Output -InputObject ($check.UserName + " is logged into: " + $check.Name)
}
else {
Write-Output -InputObject "Unable to find $userName."
}

Seeing as you're not doing nothing with the systems that you're unable to ping, you can silence the output and only returning the results for the ones that are online. Best solution for you scenario and question, is to take advantage of the parallelization that Get-CIMInstance allows you to do when passing an array of computers to -ComputerName.

  • Write-Progress tends to be terribly slow and if you're looking for a fast solution, this will definitely slow you down.
  • Get-WMIObject is a deprecated cmdlet that has been superseded by Get-CIMInstance which offers the same functionality, with a safer remoting procedure.
  • Using -Query, we can use WQL to search at the time of query speeding up the process some more. The only downside to some admins is that it follows its own syntax.

One thing to note, if you can ping a machine, it doesn't mean you can connect to it.

Get Logged on Users from a List of Computer Names

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

Java current machine name and logged in user?

To get the currently logged in user:

System.getProperty("user.name"); //platform independent 

and the hostname of the machine:

java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " + localMachine.getHostName());

How to find PC from list of users

One way of doing this could be to loop through all computers in your environment and test each one. This of course will be SLOW

There is no example of what your CSV file looks like in the question, but if it looks something like this:


"SamAccountName","title"
"jdoe","testuser"
"rboizov","system administrator"

You can do:

# get an aray if SamAccountNames from the Csv
$userNames = Import-Csv -Path 'PathToYOurCsvFile' | Select-Object -ExpandProperty SamAccountName

# get all computer objects from AD and loop through
# this of course can take a LONG time to finish..
$result = Get-ADComputer -Filter * | ForEach-Object {
Write-Host "Testing computer $($_.Name)"
if (Test-Connection -ComputerName $_.Name -Count 1 -Quiet) {
$pc = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $_.Name
# or use: $pc = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $_.Name

$user = ($pc.UserName -split '\\')[-1]
if ($userNames -contains $user) {
[PSCustomObject]@{
'SamAccountName' = $user
'ComputerName' = $pc.Name
'IP' = (Test-Connection -ComputerName $pc.Name -Count 1).IPV4Address.IPAddressToString
}
}
}
else {
Write-Warning "Computer $($_.Name) could not be reached"
}
}

#output in console
$result

# output to Csv
$result | Export-Csv -Path 'UsersOnComputers.csv' -NoTypeInformation

There might be a faster way, but that can only work if all home directories of your users have been redirected to a central server\share. If that is the case in your environment, let me know


Experimental

The below method uses Win32_ServerConnection to find all sessions to user HomeDrive folders.

This can only work if all home directories of your users have been redirected to a central \\server\share, and of course if your permissions allow it

 # the UNC \\Server\Share name of the network share where all user homedirectories are
$usersHomePath = '\\HomesServer\HomesShare$'

# get an aray if SamAccountNames from the Csv
$userNames = Import-Csv -Path 'PathToYOurCsvFile' | Select-Object -ExpandProperty SamAccountName

# split the UNC path to get the server name and share name separate
$svr = $usersHomePath.TrimStart("\") -split '\\' #"# fix syntax highlighting for SO..
$server = $svr[0]
$share = $svr[-1]

$result = Get-CimInstance -ClassName Win32_ServerConnection -ComputerName $server | # or use: Get-WmiObject -Class Win32_ServerConnection
Where-Object { $_.ShareName -eq $share -and $userNames -contains $_.UserName } |
Select-Object @{Name = "SamAccountName"; Expression = { $_.UserName }},
@{Name = "ComputerName"; Expression = {(([System.Net.Dns]::GetHostEntry($_.ComputerName).HostName) -split "\.")[0]}},
@{Name = "IP"; Expression = { (Test-Connection -ComputerName $_.ComputerName -Count 1).IPV4Address.IPAddressToString }} |
Sort-Object SamAccountName

#output in console
$result

# output to Csv
$result | Export-Csv -Path 'UsersOnComputers.csv' -NoTypeInformation


Related Topics



Leave a reply



Submit