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.
$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 [i]Learn PowerShell Toolmaking in a Month of Lunches[/i] as a good way to get a better handle on the patterns that work best in the shell :).
You must be logged in to reply to this topic.