Get device name from username in SCCM

Alex
Alex used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

AlexA lack of information provides a lack of a decent solution.

Author

Commented:
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
How fast is a
Get-CMDevice 

Open in new window

for all computers?

How many computer?
How many users?
Quick shot, take Get-CMDevice out of the user loop:

$Devices = Get-CMDevice 

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

Open in new window

AlexA lack of information provides a lack of a decent solution.

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial