Get SIP Address using VBScript - vbscript

I am trying to get a user's SIP address so I can use a JavaScript object to check their presence in Office Communicator. Here is a script I found that is similar to what I am looking to do.
Option Explicit
DIM objConnection, objCommand
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
Dim objOU, objUser, strUPN, strSIP, SIPLine
' Bind to the OU object.
Set objOU = GetObject("LDAP://chkenergy.net/DC=chkenergy,DC=net")
' Enumerate all users in the OU.
objOU.Filter = Array("user")
For Each objUser In objOU
' Skip computer objects.
If (objUser.Class = "user") Then
strUPN = objUser.userPrincipalName
strSIP = objUser.get("msRTCSIP-PrimaryUserAddress")
wscript.echo strSIP
End If
Next
Basically, I can get their username from AD, and I would like to pass that in and get their SIP address (strSIP) back. Is there a way to fix this code to do that task specifically?

The problems of your posted vbscript are
It enumerates the user on the client side, which will take a lot of time to find the correct user. Similarly, instead of pulling all the records from the database and do the comparison on your client side, you would run a SQL query. Right?
The enumeration is done at one single level only. You have to fix your code to do recursive enumeration. However, if you fix it to do recursive enumeration, it's going to take even longer time and even more resources to do your job.
Before I answer your question, here are some basic background knowlege on Active Directory.
User objects on Active Directory contains a number of attributes.
In particular, samAccountName is your pre-Windows 2000 name.
userPrincipalName is in the format of user#domain.name
You can actully execute a query using an ADO connection object. Since you are binded to an Active Directory, you can execute a LDAP query. The LDAP query string contains four parts.
Root path, where we start the search.
LDAP filter
Returned attributes
Search scope
The LDAP query string that you should use should be something like
<LDAP://chkenergy.net/DC=chkenergy,DC=net>;(&(objectClass=user)(samAccountName=yourusername));msRTCSIP-PrimaryUserAddress;subtree
The root path in the above example is <LDAP://chkenergy.net/DC=chkenergy,DC=net>.
The LDAP filter is (&(objectClass=user)(samAccountName=yourusername)). Of course, you need to replace yourusername to something else inside your code. I am assuming you can pass in a samAccountName. If that's not the case, you need to modify the filter yourself.
Returned attributes is msRTCSIP-PrimaryUserAddress. I think that's what you need. Right?
I am assuming you are trying to search for all user objects under the same domain. So, your search scope should be subtree
Here is a complete sample that I guess it should do your job
userName = "harvey"
ldapStr = "<LDAP://chkenergy.net/DC=chkenergy,DC=net>;(&(objectClass=user)(samAccountName=" & userName & "));msRTCSIP-PrimaryUserAddress;subtree"
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"
Set rs = conn.Execute(ldapStr)
While Not rs.EOF
wscript.echo rs.Fields("msRTCSIP-PrimaryUserAddress")
rs.MoveNext
Wend

Related

Linking to Oracle tables in Access using VB6: Error 3000?

I am trying to link an Oracle table to access using the following Visual Basic 6.0 code:
Dim objApp, objDB, objTable As Object
Dim strFile, strConnect, strLocalTable, strServerTable As String
strFile = "C:\path\to\base.mdb"
strLocalTable = "local"
strServerTable = "BASE.TABLE_NAME"
strConnect = "ODBC;Driver={Microsoft ODBC for Oracle};ConnectString=name.world;Uid=username;Pwd=password;"
Set objApp = CreateObject("Access.Application")
objApp.OpenCurrentDatabase strFile
Set objDB = objApp.CurrentDb()
Set objTable = objDB.CreateTableDef(strLocalTable)
objTable.Connect = strConnect
objTable.SourceTableName = strServerTable
objDB.TableDefs.Append objTable 'Generates 3000 Error
objDB.TableDefs.Refresh
On the second to last row I get (loosely translated from swedish by me) "Run time error 3000: Reserved error (-7778). There is no message for this error."
Any ideas on why this may be? I am told this code has worked before, so it could possibly be some kind of version conflict with updated software. The database is in Access 2000 format, and Access 2013 is installed on the computer (however, saving the database as Access 2013 does not help). Or is there something wrong with the connection string perhaps?
EDIT: I tried using a DSN in the connection string:
strConnect = "ODBC;Driver={Microsoft ODBC for Oracle};DSN='test';"
I get the same error, even though I can use that very DSN to link the tables manually in Access.
Also (as I stated in the comments) changing some of the information in the connection string (like deliberately providing an incorrect username) leads to a different error (3146: Connection failed). This leads me to believe that the connection to the database works, since it seems to be able to differentiate between good and bad credentials.
Try this connection string and leave out the 'world.' part
ODBC;DRIVER={Oracle in orahome32};UID=userId;PWD=password;SERVER=servername;dbq=servername
(I was having trouble earlier today with connections that left the dbq out)
Or maybe your existing one will work, but regardless...I think Access likes you to create the table default in one swoop and not break things up so.....
Instead of this:
Set objTable = objDB.CreateTableDef(strLocalTable)
objTable.Connect = strConnect
objTable.SourceTableName = strServerTable
Try This:
Set objTable = objDB.CreateTableDef(strLocalTable, dbAttachSavePWD, strServerTable, strConnect)
(NOTE: the dbAttachSavePWD will help avoid users getting prompted for password every time they touch the table; leave it out if that is not desired)

Get list of ALM project AND domains names in VBScript (QC11 OTA)

I am trying to list QC11 project and domain name in combo box on form load() but I am getting error object required,code I am using:
Dim tdc As New TDAPIOLELib.TDConnection
Dim projectList As Customization
Dim Project As Customization
Dim Domain As Customization
Set tdc = CreateObject("TDApiOle80.TDConnection")
tdc.InitConnectionEx "https://xyz/omu"
For Each Domain In TheTDConnection.DomainsList
Set projectList = tdc.GetAllVisibleProjectDescriptors
For Each Project In projectList
ComboBox1.AddItem (Project.Name)
ComboBox2.AddItem (Project.DomainName)
Next Project
Next Domain
If that's really the code you are using, then for a start this line is probably generating an error:
For Each Domain In TheTDConnection.DomainsList
Based on the rest of your code "TheTDConnection" should be "tdc":
For Each Domain In tdc.DomainsList
Oh, and to be doing this you should almost certainly be logged in first by calling tdc.Login... rather than just connected to the server.
On a related note, the DomainsList property is deprecated. I think you can just loop through the List of ProjectDescriptor objects returned by GetAllVisibleProjectDescriptors since that covers all projects under all domains that the current logged on user has access to.
Edit: this is a complete solution based on the original question. Here's working tested code that will cycle through the domains/projects that the provided user has access to. This assumes you have the QC/ALM Connectivity add-in installed (required).
If you are running this piece of VBScript on a 64 bit machine you need to run it using the 32bit version of wscript.exe: C:\Windows\SysWOW64\wscript.exe "c:\somewhere\myscript.vbs"
msgbox "Creating connection object"
Dim tdc
Set tdc = CreateObject("TDApiOle80.TDConnection")
msgbox "Connecting to QC/ALM"
tdc.InitConnectionEx "http://<yourServer>/qcbin/"
msgbox "Logging in"
tdc.Login "<username>", "<password>"
Dim projDesc
msgbox "Getting project descriptors"
Set projectDescriptors = tdc.GetAllVisibleProjectDescriptors
For Each desc In projectDescriptors
msgbox desc.DomainName & "\" & desc.Name
Next
msgbox "Logging out"
tdc.Logout
msgbox "Disconnecting"
tdc.Disconnect
msgbox "Releasing connection"
tdc.ReleaseConnection
Edit 2:
If you want to parse the resulting XML from sa.GetAllDomains into a list of ALL domain\project items on the server you can do this (This is VBScript since the original question & tag still mention it, and has been tested):
Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load "C:\yourXmlFile.xml"
Set objRoot = objDoc.documentElement
For Each domain in objRoot.selectNodes("TDXItem")
For Each project in domain.selectNodes("PROJECTS_LIST/TDXItem")
msgbox domain.selectSingleNode("DOMAIN_NAME").text & "\" & project.selectSingleNode("PROJECT_NAME").text
Next
Next

How do I get Active Directory's LDAP server url using windows API?

I've been looking for a way to get Active Directory's LDAP server url from code running as domain user. The code needs to work correctly in situation with disjoint namespace, if possible. It's unmanaged code so any .NET solutions are not an option unfortunately.
For some reason serverless binding doesn't seem to be working in this case with ADO query returning unhelpful One or more errors occurred during processing of command error when using LDAP://DC=mycompany,DC=local (that's the value of the defaultNamingContext attribute of rootDSE object).
Using the LOGONSERVER and USERDNSDOMAIN environment variables doesn't appear to be an option either because the code also needs to be able to run under the SYSTEM account and there are no such variables there.
Any ideas or hints or specific RTFM advice will be much appreciated.
Update: The DNSHostName attribute of rootDSE seems to be what I need.
I use this Visual Basic Script (VBS). Save the code as .vbs file and use ANSI charset. This script is old, but this can guide you to a better solution.
Set cn = CreateObject("ADODB.Connection")
Set cmd= CreateObject("ADODB.Command")
cn.Provider = "ADsDSOObject;"
cn.open
cmd.ActiveConnection = cn
' Root DSE required to get the default configuration naming context to
' be used as the root of the seach
set objRootDSE = getobject("LDAP://RootDSE")
' Construct the LDAP query that will find all the domain controllers
' in the domain
ldapQuery = "<LDAP://" & objRootDSE.Get("ConfigurationNamingContext") & _
">;((objectClass=nTDSDSA));ADsPath;subtree"
cmd.CommandText = ldapQuery
cmd.Properties("Page Size") = 1000
Set rs = cmd.Execute
do while rs.EOF <> True and rs.BOF <> True
' Bind to the domain controller computer object
' (This is the parent object of the result from the query)
set objDC = getobject(getobject(rs(0)).Parent)
wscript.echo objDC.dNSHostName
rs.MoveNext
Loop
cn.close
The DNSHostName attribute of rootDSE seems to be what I need.

vbscript a condition statement based off of a Membership group In Active directory

I use vbscript to autolaunch internet explorer windows through a client folder in our environment. we just introduced a new system that manages our logons and the autolaunch is based off a computer policy in that system. it can only run off of a specific computer policy. So my idea was to add a conditional statement that said. If one user logs query ad if they are apart of this group launch this window, else launch the windows we have already been launching. I have the code to create and launch websites through vbscipt, what i am looking for is the script to query ad and based on group member ship launch that windows else launch the normal ones. the system is called imprivata.
This script might do what you want:
strGroup = "GroupName"
Set objNetwork = CreateObject("WScript.Network")
strDomain = objNetwork.UserDomain
strUser = objNetwork.UserName
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser)
Dim found
found = false
For Each objGroup In objUser.Groups
If StrComp(objGroup.Name, strGroup, 1) = 0 Then
found = true
Exit For
End If
Next
' use the found variable to decide what to do
Just change "GroupName to the group you're looking for.
However, Morbo's comment is important, since if you care about indirect memberships, you'd have to recurse through all the groups found to see if any of them, or the groups they're a member of etc, are a member of the group you care about.

Get list of computers in a Workgroup using WMI VBScript

I need get list of all computers in a WORKGROUP. I tried the
Dim objComputers
Set objComputers = GetObject("WinNT://WORKGROUP")
But it is Active Directory object. Is there any way to get the computers without WinNT://?
Connect to the group object by using the full LDAP path.
Set objGroup = GetObject("LDAP://" & strGroupLDAP)
You can enumerate after with something like:
For Each strMachine In objGroup.Members
Refer to items by:
strMachine.Name or strMachine.samAccountName
EDIT: I'm confused by your question now that I reread it, what is the problem, you can use winnt:// to bind to either the domain or a machine?

Resources