Avatar of Alex
Alex
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Get device name from username in SCCM

Afternoon all,

Is it possible to pipe in a list of usernames to SCCM and have it pump out the machine they last logged onto? I thought the get-CMUserDeviceAffinity would do it but it appears it won't take the username and give me a machine name.

Thanks
alex
PowershellSCCM

Avatar of undefined
Last Comment
Alex

8/22/2022 - Mon
Michael Pfister

This takes a simple text file with user names c:\Temp\user.txt and shows a list of computers they were logged on (LastLogonUser)
HTH
$SiteCode = "XXX" # Site code 
$ProviderMachineName = "sccmserver"
$initParams = @{}
#$initParams.Add("Verbose", $true) # Uncomment this line to enable verbose logging
#$initParams.Add("ErrorAction", "Stop") # Uncomment this line to stop the script on any errors
if((Get-Module ConfigurationManager) -eq $null) {
    Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams 
}

if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
    New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}
Set-Location "$($SiteCode):\" @initParams

Get-Content c:\Temp\user.txt | % {$Username = $_
    $Devices = Get-CMDevice | Where {$_.LastLogonUser -eq "$UserName"} 
    $Devices | Select  Name,LastLogonUser

  }

Open in new window

Alex

ASKER
Hey fella

So I've left that running for an hour and a half and it doesn't seem to be doing anything.

Any ideas?

Thanks
alex
Michael Pfister

How fast is a
Get-CMDevice 

Open in new window

for all computers?

How many computer?
How many users?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Michael Pfister

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Alex

ASKER
it's slow, but it works, thank you :-)