Good Morning,
I have script that when ypu have a user name goes out and looks up all the machines the user is connected with and scans the machines to see which one the user is currently logged on to and returns the value of the which they are logged into. When I run the it never recognizes the computer the user is logged on to and I know that one of the computers it returns is the one they are logged to currently. I think it has to do with the line if ($CurrentUser -eq $user). I am thinking that it comparing a object to a string and is failing. Any help would be appreciated.
Thanks
function Get-UserLogonComputer
{
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact=‘Low’)]
Param
(
# The enter the username for this parameter
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
Position=0)]
[string]$UserName
)
Begin
{
}
Process
{
# Add a new line for readability
Write-Host “”
# Loop through each user in the username array
foreach ($user in $UserName) {
# Print the username we are currently looking up
Write-Host $user
# Added string formatting at the end to get rid of the Name header
$PCList = Get-ADComputer -Filter {ManagedBy -eq $user} | Select Name
$PCLIst
foreach ($PC in $PCList) {
if (Test-Connection -ComputerName $($PC.Name) -Count 1 -Quiet) {
#The following command looks Uses WMI to get the active user
$CurrentUser = Get-WMIObject -Class Win32_ComputerSystem -Computername $($PC.Name) | Select UserName
$CurrentUser.username.ToLower()
$CurrentUser.ToString()
if ($CurrentUser -eq $user) {
Write-Host “The users current logged on machine is:” $($PC.Name)
}
Else {
Write-Host “Current user is not logged on to:” $($PC.Name)
}
}
Else {
Write-Host “Can Not Connect to” $($PC.Name)
}
}
}
}
End
{
}
}