Login Script to pull users IP Phone from AD and then create a registry key in HKCU with that value

I need a login script to pull the IP Phone from Active Directory and add the value to:

HKEY_CURRENT_USER/SOFTWARE/XYZ/PhoneClient

I found the script below on another website but unfortunately it thread ends with the original post'er saying.. they did a little tweaking to what was suggested and got it working! But they never posted what they done.

The suggested login script is

Dim objSysInfo, objUser
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
set strtelno = objuser.telephonenumber ' Currently logged in User

Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\shoreline teleworks\shoreware client\"
KeyPath = "SOFTWARE\shoreline teleworks\shoreware client"
strValueName = "AgentID"
strValue = strtelno ' <-- This sets the Telephone Number
objRegistry.CreateKey HKEY_CURRENT_USER, strKeyPath, strvaluename, strvalue


I'm guessing i need to add an LDAP path on line 3 >> Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

Something like >> LDAP://OU=OurUSerGroup,DC=OurDomain,DC=com

I've researched objuser for IP Phone and it says use >> objuser.ipPhone (instead of line 4 >> set strtelno = objuser.telephonenumber ' Currently logged in User)

Then edit lines 9 and 10 >>
strKeyPath = "SOFTWARE\shoreline teleworks\shoreware client\"
KeyPath = "SOFTWARE\shoreline teleworks\shoreware client"

To our value >> SOFTWARE/XYZ/PhoneClient

I want to run this as a login script via Group Policy to around 50 users.
rookeydooksAsked:
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.

Chris DentPowerShell DeveloperCommented:
This should do it.
try {
    $adUser = ([ADSISearcher]"(sAMAccountName=$env:USERNAME)").FindOne()
    $ipPhone = $adUser.Properties['ipPhone'][0]

    Set-ItemProperty HKCU:\SOFTWARE\XYZ\PhoneClient -Name SomeValueName -Value $ipPhone
} catch {
    # Would you like to log any errors?
}

Open in new window

Note that I made up a property name under the registry key, you might want to fix that. The key is assumed to exist before the script runs. Is that reasonable?
0
rookeydooksAuthor Commented:
Hi Chris, Yes the value exists.

Its called Endpoint
0
Chris DentPowerShell DeveloperCommented:
My search filter is a bit broader than I thought. the snippet above is PowerShell (which you can use as a login script language, so it's not absurd).  

Updated the name, added Force to overwrite any existing value.
try {
    $adUser = ([ADSISearcher]"(sAMAccountName=$env:USERNAME)").FindOne()
    $ipPhone = $adUser.Properties['ipPhone'][0]

    Set-ItemProperty HKCU:\SOFTWARE\XYZ\PhoneClient -Name Endpoint -Value $ipPhone -Force
} catch {
    # Would you like to log any errors?
}

Open in new window

The VbScript version will work, it's more complex, but you don't need to add an LDAP path, AD system info is used to return information about the current user. ADSystemInfo is (a lot) harder to use in PowerShell, so the example above simply performs a small search against Active Directory.

Are you happy with PowerShell for this?
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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

rookeydooksAuthor Commented:
Should i be able to save the below in to a notepad file, save as script.vbs and run it from the desktop of a windows 7 machine to test ?

try {
    $adUser = ([ADSISearcher]"(sAMAccountName=$env:USERNAME)").FindOne()
    $ipPhone = $adUser.Properties['ipPhone'][0]

    Set-ItemProperty HKCU:\SOFTWARE\XYZ\PhoneClient -Name Endpoint -Value $ipPhone
} catch {
    # Would you like to log any errors?
}
0
Chris DentPowerShell DeveloperCommented:
No, .ps1, as a PowerShell logon script. One tick, I'll give you the VbScript fix as well just in case. Then you can choose which to use.
0
Chris DentPowerShell DeveloperCommented:
VbScript version, untested.
Const HKEY_CURRENT_USER = &H80000001
Const REG_PATH = "SOFTWARE\XYZ\PhoneClient"

Dim objSysInfo : Set objSysInfo = CreateObject("ADSystemInfo")
Dim objUser : Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

Dim strIPPhone : strIPPhone = objUser.Get('ipPhone')

Dim objReg : Set objReg = GetObject("winmgmts:\\.\root\default:StdRegProv")

objReg.SetStringValue HKEY_CURRENT_USER, REG_PATH, "Endpoint", strIPPhone

Open in new window

0
rookeydooksAuthor Commented:
Just saved your last script (below) as .ps1 and ran from the desktop. It worked a treat. My IP Phone value is now in the registry under endpoint.

Thank you so much Chris.

try {
    $adUser = ([ADSISearcher]"(sAMAccountName=$env:USERNAME)").FindOne()
    $ipPhone = $adUser.Properties['ipPhone'][0]

    Set-ItemProperty HKCU:\SOFTWARE\XYZ\PhoneClient -Name Endpoint -Value $ipPhone -Force
} catch {
    # Would you like to log any errors?
}
0
rookeydooksAuthor Commented:
Excellent help and support from Chris Dent. My issue was resolved in minutes.

Thank you so much Chris.
0
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
Regedit

From novice to tech pro — start learning today.