DSMOD script to set Users with blank UPN to same as email address

We are trying to write a script to locate all our user accounts that do not have the UPN field populated.  Then we need to add in the UPN to be the same as the Email address of the user.  So far I have been unable to locate how to script this. Any help or input is appreciated.    
asburycollegeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Henrik JohanssonSystems engineerCommented:
Copy code snippet into notepad and save file as *.cmd or *.bat
The script below will set UPN for all users in the domain to be e-mail address if having e-mail address set.
for /F "tokens=1,2" %%a in ('dsquery user -limit 0 ^|dsget user -samid -email^|find "@"') do (
  dsquery user -samid %%a|dsmod user -upn %%b
)

Open in new window

0
asburycollegeAuthor Commented:
I forgot to clarify in the original question.  We only need to have this performed on users whose UPN is currently blank.  We have some legacy users that do not have this information and some of the new technology we are implement utilizes the UPN field for authentication.  Is there a way to ONLY get and change users with a blank UPN field?
0
Henrik JohanssonSystems engineerCommented:
The previous script will do it if you don't have any user that UPN set, but is different to e-mail address
For example upn=jdoe@domain.com will be overidden if email=john.doe@domain.com
Modified script below to exclude those that already has UPN set to xxx@*.
To only run it on a sub-OU change the first dsquery to be
dsquery user -limit 0 "OU=OU-path,DC=domain,DC=com"
0
Henrik JohanssonSystems engineerCommented:
The previous script will do it if you don't have any user that UPN set, but is different to e-mail address
For example upn=jdoe@domain.com will be overidden if email=john.doe@domain.com
Modified script below to exclude those that already has UPN set to xxx@*.
To only run it on a sub-OU change the first dsquery to be
dsquery user -limit 0 "OU=OU-path,DC=domain,DC=com"
for /F "tokens=1" %%a in ('dsquery user -limit 0 ^|dsget user -samid -upn^|find /v "@"') do (
  for /F "tokens=1,2" %%u in ('dsquery user -samid %%a ^|dsget user -samid -email^|find "@"') do (  
    dsquery user -samid %%a|dsmod user -upn %%v
  )
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.