Monday, November 8, 2021

 

Script to get the current user's AD details.

<#
Script will get the currently logged in user and then search for their AD attributes via ADSI.
The attributes are written to a csv file in c:\temp to be used later. They cannot be directly written to TS variables since this step has to be ran as a user account and it would have its own variable store.
#>

#Step needs to run as an AD account to pull info. Currently runs as _sccmsvc1


$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment

#Get logged in user
$user = (Get-WmiObject -Class Win32_Process -Filter 'Name="explorer.exe"').GetOwner().User

#Get AD User attributes
$searcher = [adsisearcher]"(samaccountname=$user)"
$userTitle = $searcher.FindOne().Properties.title
$userDeptNum = $searcher.FindOne().Properties.extensionattribute5
$userDeptName = $searcher.FindOne().Properties.department
$userLocation = $searcher.FindOne().Properties.extensionattribute7


$userObject = New-Object psobject -Property @{
"currentUserADID" = "$user"
"currentUserTitle" = "$userTitle"
"currentUserDeptNum" = "$userDeptNum"
"currentUserDeptName" = "$userDeptName"
"currentUserLocation" = "$userLocation"
}

$userObject | export-csv c:\temp\userAttributes.csv -NoTypeInformation

No comments:

Post a Comment