PowerShell Query to find a users computer name in SCCM

We rely on Remote Assistance. Usually, I just type “msra /offerra” in to my PowerShell session and lookup a the user’s computer name in the SCCM report named “Computers for a specific user name”. I wanted to make that process quicker. I wrote the following script to query SCCM for the “list of computer’s who’s last logged on user” is the person I am looking for.

FUNCTION JBMURPHY-SCCM-GetComputerByLastLoggedOnUser {
Param([parameter(Mandatory = $true)]$SamAccountName,
	$SiteName="JBM",
	$SCCMServer="SCCMServer.domain.local")
	$SCCMNameSpace="root\sms\site_$SiteName"
	Get-WmiObject -namespace $SCCMNameSpace -computer $SCCMServer -query "select Name from sms_r_system where LastLogonUserName='$SamAccountName'" | select Name
}

Let me know if this is useful to you!


Posted

in

,

by

Tags:

Comments

9 responses to “PowerShell Query to find a users computer name in SCCM”

  1. VertigoRay Avatar

    Well done, after modifying the two obvious variables that needed to be change ($SiteName & $SCCMServer) to match my SCCM info, I only had to tweak your SQL query to remove that rogue comma…

    “select Name from sms_r_system where LastLogonUserName=’$SamAccountName’”

    Thanks for saving me the time of hashing this out myself. 😉

  2. jbmurphy Avatar

    Thanks for taking the time to reply. I have remove the “rouge comma” ! Glad this was helpful to you.

  3. VertigoRay Avatar

    Hey Jeffrey,
    I took your function a little farther and back-referenced this post since it was an expansion of your code. Hope you enjoy:
    http://go.vertigion.com/PowerShell-Get-CompByUser

  4. jbmurphy Avatar

    Sweet! Thanks for taking your time to let me know! I like it when the code I write is helpful.

  5. Ben Avatar
    Ben

    You saved my life. Thank you!

  6. Shane Avatar
    Shane

    I had to do something similar but ended up writing a custom SCCM Report that took a plain text variable which I then queried from powershell to create an object that was returned for a simple menu to select which would then launch the remote control tool for that computer ID. Why go to such great complication. Answer = Firewall. Powershell goodness….

  7. Armand Avatar

    A bit late to the party, but very useful (saved me some time!)

    I re-used your function for our purposes and edited it a bit (to accept a inputlist with samaccountnames and export the output to a CSV file direclty)

    Thanks for sharing!

  8. thk4this Avatar
    thk4this

    Thanks, this was quit helpful.
    To make it more useful for me, I changed the Select call to:

    Select-Object Name, @{Name=”Timestamp”;Expression={$_.ConvertToDateTime($_.LastLogonTimestamp)}}

  9. thk4this Avatar
    thk4this

    oh yeah, and the WMI query to:

    “select Name,LastLogonTimestamp from …