none
How to get userPrincipalName with Vbscript RRS feed

  • Question

  • I want to get userPrincipalName with Vbscript, so I test "whoami /upn", it is fast and can run without network.

    the follow ADSI code also can get userPrincipalName, but it need network and a little bit slower.

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

    so if there is another way to get userPrincipalName without network.

    PS, I know I can use "exec" to obtain return value of "whoami /upn", but it will pup-up new window, and cannot be hid.

    Tuesday, November 5, 2013 7:28 AM

Answers

  • Set environment variable to UPN

    for /f %i in ('whoami /upn') do @set upn=%i


    ¯\_(ツ)_/¯

    • Marked as answer by Unknown32 Monday, November 11, 2013 1:53 AM
    Friday, November 8, 2013 7:15 AM

All replies

  • strUsername = "BBell"
    
    Const ADS_SCOPE_SUBTREE = 2
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand = CreateObject("ADODB.Command")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    Set objCommand.ActiveConnection = objConnection
    
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
    
    objCommand.CommandText = _
        "SELECT userPrincipalName FROM 'LDAP://dc01/dc=domain,dc=topleveldomain,dc=local' " & _
            "WHERE objectCategory='user' " & _
                "AND sAMAccountName='" & strUsername & "'"
    Set objRecordSet = objCommand.Execute
    
    objRecordSet.MoveFirst
    Do Until objRecordSet.EOF
        strUPN = objRecordSet.Fields("userPrincipalName").Value
        WScript.Echo strUPN
        objRecordSet.MoveNext
    Loop

    Obviously - you can pipe in the current logged on user by using:

    Set objNetwork = CreateObject("Wscript.Network") Wscript.Echo "The current user is " & objNetwork.UserName

    and setting strUsername to objNetwork.Username

    • Proposed as answer by Ben Bell Tuesday, November 5, 2013 10:08 AM
    Tuesday, November 5, 2013 9:48 AM
  • All methods of obtaining UPN require a domain.  UPN is an Active Directory attribute.

    ¯\_(ツ)_/¯

    Tuesday, November 5, 2013 1:15 PM
  • Yes, I know UPN is an Active Directory attribute, but i find "whoami /upn" is so fast to get UPN,  so I think maybe upn will save in OS when logon domain account.

    I can use ADSI to get UPN, but it has a bit slow, especially for VPN users

    Friday, November 8, 2013 2:25 AM
  • Looks like "whoami /upn" uses the GetUserNameEx function to get that information.  You could get at that method from a PowerShell script using some C# code and P/Invoke, but from a VBScript, someone would have to write a COM DLL to wrap that function.

    Personally, I'd just find a way to make calling whoami.exe meet your needs.

    Friday, November 8, 2013 4:18 AM
  • Yes, I know UPN is an Active Directory attribute, but i find "whoami /upn" is so fast to get UPN,  so I think maybe upn will save in OS when logon domain account.

    I can use ADSI to get UPN, but it has a bit slow, especially for VPN users

    What is slow. ADSI takes one line of code and it is as fast as anything. If you add it to a logon scrip and set an environment variable then it will be as fast as whoami. You can also use whoami to se a variable at logon.


    ¯\_(ツ)_/¯

    Friday, November 8, 2013 7:07 AM
  • Set environment variable to UPN

    for /f %i in ('whoami /upn') do @set upn=%i


    ¯\_(ツ)_/¯

    • Marked as answer by Unknown32 Monday, November 11, 2013 1:53 AM
    Friday, November 8, 2013 7:15 AM
  • I measured this as best I could and I see no differences between this and whoami getting upn.

    Set ads = CreateObject("ADSystemInfo")
    Set user = GetObject("LDAP://" & ads.UserName)
    WScript.Echo user.Get("userPrincipalName")
    


    ¯\_(ツ)_/¯

    Friday, November 8, 2013 7:39 AM
  • Yes, I can dump it and get it
    Monday, November 11, 2013 2:02 AM