using user name to get-computer name

Welcome Forums General PowerShell Q&A using user name to get-computer name

This topic contains 3 replies, has 2 voices, and was last updated by

 
Member
5 years, 1 month ago.

  • Author
    Posts
  • #10944

    Participant
    Points: 0
    Rank: Member

    $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

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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
    
    

The topic ‘using user name to get-computer name’ is closed to new replies.

One thought on “using user name to get-computer name

  1. Pingback: How To Get Computer Name In Sql | Togoods

Comments are closed.