Need Help with script

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
{
}

}

$CurrentUser contains an object that has a UserName property, and its ToString() method very likely returns a blank string. Besides, you’re not capturing the result of ToString() anyway. In addition, you can’t change that property name simply by calling ToLower(); the ToLower method returns a new string; it doesn’t modify the existing one. And you don’t need to lowercase it, because PowerShell string comparisons are not case-sensitive.

What you probably wanted was “Select -ExpandProperty UserName” instead. Remove the ToLower() and ToString() calls. They’re not doing anything for you.

There’s also no reason for the properties that you have in CmdletBinding(); your function isn’t making any changes, and you haven’t implemented support for -WhatIf and -Confirm, so you shouldn’t declare support for them.

Your subexpressions $($PC.Name) should probably go inside the double quotes, not after them.

You also should decide where you want your output to go, and probably use Write-Output and Write-Verbose in lieu of the much-less-flexible Write-Host. I’ll cheerfully recommend Learn PowerShell Toolmaking in a Month of Lunches as a good way to get a better handle on the patterns that work best in the shell :).