Powershell Change Code Search Range Usernames One Username Script Name Queryusersforlastlo Q43782423
POWERSHELL
How do I change this code to search a range of usernames and notjust by one username?
#********************************************************************************
#
# Script Name: QueryUsersForLastLogon.ps1
# Version: 1.0
# Location:
# Applies to: Computers
#
# Description: This script searches for a specific, logged on useron all or
# specific Computers by checking the process “explorer.exe” and itsowner.
#
#********************************************************************************
#Set variables
$progress = 0
#Get Admin Credentials
Function Get-Login {
Clear-Host
Write-Host “Please provide admin credentials (for exampleDOMAINadmin.user and your password)”
$Global:Credential = Get-Credential
}
Get-Login
#Get Username to search for
Function Get-Username {
Clear-Host
$Global:Username = Read-Host “Enter username you wantto search for”
if ($Username -eq $null){
Write-Host “Username cannot beblank, please re-enter 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
#Get Computername Prefix for large environments
Function Get-Prefix {
Clear-Host
$Global:Prefix = Read-Host “Enter a prefix ofComputernames to search on (CXX*) use * as a wildcard or enter * tosearch on all computers”
Clear-Host
}
Get-Prefix
#Start search
$computers = Get-ADComputer -Filter {Enabled -eq ‘true’ -andSamAccountName -like $Prefix}
$CompCount = $Computers.Count
Write-Host “Searching for $Username on $Prefix on $CompCountComputers`n”
#Start main foreach loop, search processes on allcomputers
foreach ($comp in $computers){
$Computer = $comp.Name
$Reply = $null
$Reply = test-connection $Computer -count 1-quiet
if($Reply -eq ‘True’){
if($Computer -eq$env:COMPUTERNAME){
#Getexplorer.exe processes without credentials parameter if the queryis executed on the localhost
$proc = gwmiwin32_process -ErrorAction SilentlyContinue -computer $Computer-Filter “Name = ‘explorer.exe'”
}
else{
#Getexplorer.exe processes with credentials for remote hosts
$proc = gwmiwin32_process -ErrorAction SilentlyContinue -Credential $Credential-computer $Computer -Filter “Name = ‘explorer.exe'”
}
#If $proc isempty return msg else search collection of processes forusername
if([string]::IsNullOrEmpty($proc)){
write-host”Failed to check $Computer!”
}
else{
$progress++
ForEach ($p in$proc) {
$temp = ($p.GetOwner()).User
Write-Progress -activity “Working…” -status”Status: $progress of $CompCount Computers checked”-PercentComplete (($progress/$Computers.Count)*100)
if ($temp -eq $Username){
write-host “$Username is logged on$Computer”
}
}
}
}
}
write-host “Search done!”
Expert Answer
Answer to POWERSHELL How do I change this code to search a range of usernames and not just by one username? # ********************…
OR