Created
May 10, 2018 09:24
-
-
Save gislig/113ae47cbb781ff8cc5fa543c6818c3a to your computer and use it in GitHub Desktop.
Ping User on the Network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 @{ | |
ComputerName=$ComputerName; | |
IPAddress=$IPAddress; | |
User=$User; | |
} | |
} | |
#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 | |
if($FindUser){ | |
$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 } | |
#$UserFound | |
#If the user is found on a specified computer enter the if statement | |
if($UserFound){ | |
setPingUser -ComputerName $Computer.Name -IPAddress $Computer.IPv4Address -User $UserFound.User | |
} | |
} | |
} | |
}else{ | |
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