I need some help with a Powershell script.
Sometimes I need to copy a file from one computer to other through the DC server (using "\machine\c$") but I need a way to identify which machine a specific user is logged in on the domain...this task gets complicated when I have several users using the same machine, if not, a simple description on the AD computers identifying the respective user would solve the problem.
I found a script on the Microsoft Learn website that can provide the information I need, but the script was incomplete and the user that was developing it found a solution but didn't shared it, only left a comment saying "Finally i could solve it with the "query user" line: query user $Username /server:$Computer"...
I've tried to edit the script but I'm not managing to solve the situation, this is the script:
#Set variables
$progress = 0
#Get Admin Credentials
#Function Get-Login {
#Clear-Host
#Write-Host "Please provide admin credentials (for example #DOMAIN\admin.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 want to search for"
if ($Username -eq $null){
Write-Host "Username cannot be blank, 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 of Computernames to search on (CXX*) use * as a wildcard or enter * to search on all computers"
Clear-Host
}
Get-Prefix
#Start search
$computers = Get-ADComputer -Filter {Enabled -eq 'true' -and SamAccountName -like $Prefix}
$CompCount = $Computers.Count
Write-Host "Searching for $Username on $Prefix on $CompCount Computers`n"
#Start main foreach loop, search processes on all computers
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){
#Get explorer.exe processes without credentials parameter if the query is executed on the localhost
$proc = gwmi win32_process -ErrorAction SilentlyContinue -computer $Computer -Filter "Name = 'explorer.exe'"
}
else{
#Get explorer.exe processes with credentials for remote hosts
$proc = gwmi win32_process -ErrorAction SilentlyContinue -Credential $Credential -computer $Computer -Filter "Name = 'explorer.exe'"
}
# Get last logon date
$lastLogon = (gwmi -Class Win32_NetworkLoginProfile -ComputerName $computer.Name | Where-Object {$_.Name -eq $Username}).LastLogon
#If $proc is empty return msg else search collection of processes for username
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!"
The script has to run on Windows Server 2008 R2 because I still have people using it.
Can please someone help me finding what did the original developer meant when he said that he had solved it with the "query user" line: query user $Username /server:$Computer"...?
Thanks.
EDIT: This script is driving me nuts! The first part (login) isn't necessary if you're executing it from a administrator account on the DC server.
The script works to a certain degree, it identifies the machine that the targeted user is logged on, but it takes too long because it keeps indicating errors (the information about the session like the last logon isn't being displayed).
The guy that initially developed the script left a comment saying that he solved the problem in the "query user line", but the error I keep getting it's on the computer name query section...
I keep getting this error every time I do a search:
Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or
empty, and then try the command again.
At C:\Users\admin\Desktop\userquery.ps1:57 char:75
+ ... -Class Win32_NetworkLoginProfile -ComputerName $computer.Name | Where ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Can someone please help me figuring this out?
Thanks.