using user name to get-computer name

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 3 years, 6 months ago.

  • Author
    Posts
  • #10944
    Profile photo of Jason Rojas
    Jason Rojas
    Participant

    $computerlist = Get-ADComputer -Filter * | select -expand name
    icm -ComputerName $computerlist {Get-WmiObject win32_computersystem -ComputerName 'localhost' | where username -like "crojas" | select username, name
    }

    when I run this script this is returned for all computers on my network, thank god its a small one. Am I able to do this, I am a newbie so I am experimenting.

    Cannot bind parameter 'FilterScript'. Cannot convert the "username" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
    + CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WhereObjectCommand
    + PSComputerName : ARRIS-20

    I would like to be able to enter a user name and find the computer name that the user has logged into so that I can remote into for support purposes.

    thanks for your time and knowledge

  • #10946
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Most likely, some of these computers are running PowerShell 2.0, and your "where" syntax only works on PowerShell 3.0 or later. You can try changing:

    # This:
    where username -like "crojas"
    
    # to this:
    Where-Object { $_.username -like "crojas" }
    

    Edit: On a side note, Get-WmiObject has its own remote capabilities, if you would prefer to let WMI do the work instead of PowerShell Remoting. That's up to you; it will eventually work either way, once the bugs are fixed.

  • #10977
    Profile photo of Jason Rojas
    Jason Rojas
    Participant

    I have tried that but I get the same error message. I tried to pipe in GM to see what data types it likes because according the message it does not like throwing a string at the script block.

  • #10981
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    So you ran it like this, and still got the same error message?

    $computerlist = Get-ADComputer -Filter * | select -expand name
    Invoke-Command -ComputerName $computerlist -ScriptBlock { Get-WmiObject win32_computersystem -ComputerName 'localhost' | Where-Object { $_.username -like "*crojas*" } | select username, name }
    

    If I were doing this, I'd probably rewrite it this way (not using Invoke-Command at all, since WMI already works remotely, and making a few small performance tweaks):

    $computerlist = Get-ADComputer -Filter * -Properties Name | Select-Object -ExpandProperty Name
    
    Get-WmiObject -ComputerName $computerlist -Class Win32_ComputerSystem -Filter 'UserName LIKE "%crojas%"' -Property UserName,Name |
    Select-Object -Property UserName, Name
    
    

You must be logged in to reply to this topic.