Skip to content

Instantly share code, notes, and snippets.

@gislig gislig/UserPing.ps1
Created May 10, 2018

What would you like to do?
Ping User on the Network
#Script created by Gísli Guðmundsson
#Created for Aron Gauti Birgisson
#Create Objects to set info about the computers who user logged on to
function setPingUser($ComputerName, $IPAddress, $User){
New-Object psobject -Property @{
#Ping the user
function pingUser($SamAccountName, $OSType){
#Finds the user with samaccountname
$FindUser = Get-ADUser -Filter "SamAccountName -eq '$SamAccountName'"
#If the user exists enter the if statement
$Computers = Get-ADComputer -Filter "OperatingSystem -like '*$OSType*'" -Properties IPv4Address, OperatingSystem
write-host "Pinging user $SamAccountName - Will take some time based on the network size" -ForegroundColor Yellow
foreach($Computer in $Computers){
#If computer responds to a network call enter the if statement
if(Test-Connection -ComputerName $Computer.Name -ErrorAction SilentlyContinue){
#Get last logged on user on remote computer
$UserFound = Get-WinEvent -ComputerName $Computer.Name -FilterHashtable @{Logname='Security';ID=4672} -MaxEvents 1 -ErrorAction SilentlyContinue | select @{N='User';E={$_.Properties[1].Value}} | where { $_.User -eq $SamAccountName }
#If the user is found on a specified computer enter the if statement
setPingUser -ComputerName $Computer.Name -IPAddress $Computer.IPv4Address -User $UserFound.User
write-host "User $SamAccountName does not exist, please type in the samaccount name" -ForegroundColor Red
#Example for OSType are
# Server
# Windows Server 2016
# Windows 7
# Enterprise
pingUser -SamAccountName "gisli" -OSType "Server"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.