Who

This is my attempt to create a command similar to Linux’s who command.  Basically all this is doing is using WMI to see who the logged on user is, this is the key line:

Get-WMIObject Win32_Computersystem -Comp $Hostname -ev myError -ea SilentlyContinue | Select Name, UserName

Here is the full script with help doc and exception handling:

##############################
# Author: Erich Cottom
# Copyright: Erich Cottom http://erichcottom.com
# Title: Who
# Description: This script gets the current loged on user of the local or remote computer
##############################

# who 1.0.0 - inital release
# who 1.0.1 - added default local hostname fixed trap exception
# who 1.1.0 - trap statment now only for dns
# who 1.1.1 - fixed dns trap to reset on fail
# bug - rdp user appear as not logged on

<#
.SYNOPSIS
This script acts like Linux's who command

.DESCRIPTION
This command uses WMI to get the user who is currently logged onto the system.

.PARAMETER Hostnames
A list of Hostnames to inquire about logged on users

.EXAMPLE
who.ps1 mycomp

Name                                                        UserName
----                                                        --------
mycomp                                                      MYCOMP\myuser

.EXAMPLE
who.ps1 pc1,pc2,pc3,pc4,pc5

Name                                                        UserName
----                                                        --------
pc1                                                         DOMAIN\user1
pc2                                                         DOMAIN\user2
pc3                                                         DOMAIN\user3
pc4                                                         DOMAIN\user4
pc5                                                         DOMAIN\user5

.NOTES
.LINK
#>

##############
# MAIN
##############

param
(
	[parameter(Mandatory=$true,ValueFromPipeline=$true)] $Hostnames = $env:computername
)

$Hostnames = $Hostnames | get-unique

foreach ($Hostname in $Hostnames)
{
	# Test if hostname will resolve to ip
	$DnsTest = [System.Net.Dns]::GetHostEntry("$hostname")
	trap [System.Net.Sockets.SocketException]
	{

		Write-Output "$hostname has no dns reccord" | Write-Host -ForegroundColor Red -BackgroundColor Black

		$DnsTest.AddressList = $null
		continue
	}
	if($DnsTest.AddressList -ne $null)
	{
		# Ping test
		$Ping = new-object System.Net.NetworkInformation.Ping
		$Reply = $Ping.Send($Hostname)
		$PingTest = $Reply.status
		if ($PingTest -eq "Success")
		{
			# Get logged on user
			Get-WMIObject Win32_Computersystem -Comp $Hostname -ev myError -ea SilentlyContinue | Select Name, UserName

			# RPC error if WMI call fails
			if ($MyError -ne $null)
			{

				Write-Output "The RPC server on $hostname is unavailable" | Write-Host -ForegroundColor Red -BackgroundColor Black

				continue
			}
		}
		# Ping test errors
		elseif ($PingTest -eq "TimedOut")
		{

			Write-Output "Ping Failed: $hostname appears to be down" | Write-Host -ForegroundColor Red -BackgroundColor Black

		}
		else
		{

			Write-Output "Other ping error: $PingTest" | Write-Host -ForegroundColor Red -BackgroundColor Black

		}
	}
}