Hi PS community,
I am new and Its my first topic here.
I am running this script to get the email of a logged in user
Result are:
On test user, it gives result. i.e. write email adress of the logged in user to the specified location.
In actual, when real user logged in, file is empty.
I have attached log file.
Kindly help in this regards
powershell
# Retrieve the email address of the currently logged-in user
$usersearch = [adsisearcher]"(samaccountname=$env:USERNAME)"
$email = $usersearch.FindOne().Properties.mail
# Specify the path where you want to store the email address
$outputFilePath = "$env:APPDATA\cuser_swye360.dat"
# Write the email address to the specified file
$email | Out-File -FilePath $outputFilePath -Force
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
( !! Sometimes the preformatted text button hides behind the settings gear symbol. )
Since your script obviously works (you tested it) the issue might be the way you run it. It looks like you’re using a kind of systems management solution. I’d try to look for the issue in that direction.
For example: does the impersonation of the logged on user work properly? You could write the $env:USERNAME to the output file as well to check that.
any reason you’re using the adsisearcher? Also how is this being called? a logon script or what exactly? Are you sure it’s running in the context of the logged in user?
I can’t reproduce your error, and it seems like you also had a test user worked. It might be worth double checking that the actual user definitely has a mail property. Next I’d run it and make sure that data is in PS prior to the export. I’m sure it is but doesn’t hurt to double check. Confirm it against Get-ADUser as well.