{"id":29,"date":"2011-10-07T13:58:05","date_gmt":"2011-10-07T17:58:05","guid":{"rendered":"http:\/\/powershell.erichcottom.com\/?p=29"},"modified":"2013-05-28T01:21:32","modified_gmt":"2013-05-28T05:21:32","slug":"who","status":"publish","type":"post","link":"https:\/\/powershell.erichcottom.com\/?p=29","title":{"rendered":"Who"},"content":{"rendered":"<p>This is my attempt to create a command similar to Linux&#8217;s who command.\u00a0 Basically all this is doing is using WMI to see who the logged on user is, this is the key line:<\/p>\n<p><code>Get-WMIObject Win32_Computersystem -Comp $Hostname -ev myError -ea SilentlyContinue | Select Name, UserName<\/code><\/p>\n<p>Here is the full script with help doc and exception handling:<\/p>\n<pre class=\"brush: powershell; gutter: true\">##############################\r\n# Author: Erich Cottom\r\n# Copyright: Erich Cottom http:\/\/erichcottom.com\r\n# Title: Who\r\n# Description: This script gets the current loged on user of the local or remote computer\r\n##############################\r\n\r\n# who 1.0.0 - inital release\r\n# who 1.0.1 - added default local hostname fixed trap exception\r\n# who 1.1.0 - trap statment now only for dns\r\n# who 1.1.1 - fixed dns trap to reset on fail\r\n# bug - rdp user appear as not logged on\r\n\r\n&lt;#\r\n.SYNOPSIS\r\nThis script acts like Linux's who command\r\n\r\n.DESCRIPTION\r\nThis command uses WMI to get the user who is currently logged onto the system.\r\n\r\n.PARAMETER Hostnames\r\nA list of Hostnames to inquire about logged on users\r\n\r\n.EXAMPLE\r\nwho.ps1 mycomp\r\n\r\nName                                                        UserName\r\n----                                                        --------\r\nmycomp                                                      MYCOMP\\myuser\r\n\r\n.EXAMPLE\r\nwho.ps1 pc1,pc2,pc3,pc4,pc5\r\n\r\nName                                                        UserName\r\n----                                                        --------\r\npc1                                                         DOMAIN\\user1\r\npc2                                                         DOMAIN\\user2\r\npc3                                                         DOMAIN\\user3\r\npc4                                                         DOMAIN\\user4\r\npc5                                                         DOMAIN\\user5\r\n\r\n.NOTES\r\n.LINK\r\n#&gt;\r\n\r\n##############\r\n# MAIN\r\n##############\r\n\r\nparam\r\n(\r\n\t[parameter(Mandatory=$true,ValueFromPipeline=$true)] $Hostnames = $env:computername\r\n)\r\n\r\n$Hostnames = $Hostnames | get-unique\r\n\r\nforeach ($Hostname in $Hostnames)\r\n{\r\n\t# Test if hostname will resolve to ip\r\n\t$DnsTest = [System.Net.Dns]::GetHostEntry(\"$hostname\")\r\n\ttrap [System.Net.Sockets.SocketException]\r\n\t{\r\n\r\n\t\tWrite-Output \"$hostname has no dns reccord\" | Write-Host -ForegroundColor Red -BackgroundColor Black\r\n\r\n\t\t$DnsTest.AddressList = $null\r\n\t\tcontinue\r\n\t}\r\n\tif($DnsTest.AddressList -ne $null)\r\n\t{\r\n\t\t# Ping test\r\n\t\t$Ping = new-object System.Net.NetworkInformation.Ping\r\n\t\t$Reply = $Ping.Send($Hostname)\r\n\t\t$PingTest = $Reply.status\r\n\t\tif ($PingTest -eq \"Success\")\r\n\t\t{\r\n\t\t\t# Get logged on user\r\n\t\t\tGet-WMIObject Win32_Computersystem -Comp $Hostname -ev myError -ea SilentlyContinue | Select Name, UserName\r\n\r\n\t\t\t# RPC error if WMI call fails\r\n\t\t\tif ($MyError -ne $null)\r\n\t\t\t{\r\n\r\n\t\t\t\tWrite-Output \"The RPC server on $hostname is unavailable\" | Write-Host -ForegroundColor Red -BackgroundColor Black\r\n\r\n\t\t\t\tcontinue\r\n\t\t\t}\r\n\t\t}\r\n\t\t# Ping test errors\r\n\t\telseif ($PingTest -eq \"TimedOut\")\r\n\t\t{\r\n\r\n\t\t\tWrite-Output \"Ping Failed: $hostname appears to be down\" | Write-Host -ForegroundColor Red -BackgroundColor Black\r\n\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\r\n\t\t\tWrite-Output \"Other ping error: $PingTest\" | Write-Host -ForegroundColor Red -BackgroundColor Black\r\n\r\n\t\t}\r\n\t}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is my attempt to create a command similar to Linux&#8217;s who command.\u00a0 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 &hellip; <a href=\"https:\/\/powershell.erichcottom.com\/?p=29\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,7,8],"tags":[25],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-poshtested","category-powershell","category-scripts","tag-wmi"],"_links":{"self":[{"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":10,"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":43,"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions\/43"}],"wp:attachment":[{"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/powershell.erichcottom.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}