How to Add Distribution Groups to contacts with wsf script
-
Wednesday, June 04, 2008 11:20 PMBelow is my wsf code...messy but it works.
I like to call it LCSAddGroups.wsf
It is mostly cut and past from LCSAddContacts.wsf
My company has about 100 users and 7 departments.
Each department has a distribution group email.
I add no contacts to my users, only the distribution groups for each department and then I manage membership of the groups in AD.
New employees are added to their department email group...and when OCS syncs with AD they automatically appear in communicator.
The only thing I need to do with OCS anymore is run this script on new employees.
Distribution Groups are just like other groups in Communicator except that it has a property called ExternalURL which is formatted like this:
<groupExtension groupType="dg"><email>DistributionGroupEmailAddress@domain.com</email></groupExtension>
Paramers you can use with my script:
/UsersFile:c:\users.txt - add groups to all users in users.txt
/Delete - deletes all contacts and groups from user - can be used with /UsersFile
If you call the script with no parameters it will prompt you for a SIP address.
It seems that you need to add 1 contact to a new user before you can add distribution groups.
I always create 1 contact...delete it...and then run this script to create the distribution groups.
Hope this helps someone...It took me a long time to figure this out.
<?xml version="1.0"?>
<package>
<comment>
LCSAddGroups.wsf
This WMI script progmatically adds Distribution Lists for LCS Users.
</comment>
<job>
<?job error="True" debug="True" ?>
<script language="VBScript">
<![CDATA[
' VBScript source code
Option Explicit
' Contant decleration section
Const cComputer = "."
Const cWMINameSpace = "root/cimv2"
Const cWMIUserClass = "MSFT_SIPESUserSetting"
Const cWMIUserContactGroupClass = "MSFT_SIPESUserContactGroupData"
Const cWMIUserContactClass = "MSFT_SIPESUserContactData"
Const cWMIUserACEClass = "MSFT_SIPESUserACEData"
Const cGroupNameAllContacts = "~"
'-------------------------------------------------------------------------------
' Function: GetUserInstanceID
' Description: This function will return the InstanceID of SIP User
' Parameters: ByVal strPrimaryURI - SIP address of User
' whoes InstanceID has to be retrived
' Returns: Variant string - (either -1 or InstanceID of SIP User)
'-------------------------------------------------------------------------------
Function GetUserInstanceID (ByVal strPrimaryURI)
Dim vReturn : vReturn = -1
If Not IsEmpty(strPrimaryURI) Then
Dim oUserInstances : Set oUserInstances = GetObject("winmgmts:\\" & cComputer).ExecQuery("SELECT * FROM " & cWMIUserClass & " WHERE PrimaryURI = '" & strPrimaryURI & "'")
If Not oUserInstances.Count < 1 Then
Dim oInstance
For Each oInstance In oUserInstances
vReturn = oInstance.InstanceID
Next
End If
End If
GetUserInstanceID = vReturn
End Function
'-------------------------------------------------------------------------------
' Function: ParseUserLineItem
' Description: This function will parse each line item from text file
' with the users and add contacts for them.
' Parameters: ByVal strLineItem - Line item that has to be parsed
' for user for which contacts have to be added.
' Returns:
'-------------------------------------------------------------------------------
Function ParseUserLineItem (strLineItem)
Dim vItems : vItems = Split(strLineItem, ":")
If UBound(vItems) > 0 Then
Dim vUsers
Dim vUser
Select Case UCase(vItems(0))
Case "SIP"
vUser = "sip:" & Trim(vItems(1))
If fDelete Then
Wscript.echo vUser
DeleteContacts(vUser)
DeleteGroups(vUser)
Else
AddContactGroups(vUser)
End If
Case Else
WScript.Echo ""
WScript.Echo "@------------------------------------------------------------"
WScript.Echo "[Err] Invalid Line item in " & strUsersFile & ": '" & strLineItem & "'"
WScript.Echo "@------------------------------------------------------------"
End Select
Else
WScript.Echo ""
WScript.Echo "@------------------------------------------------------------"
WScript.Echo "[Err] Invalid Line in " & strUsersFile & ": '" & strLineItem & "'"
WScript.Echo "@------------------------------------------------------------"
End If
End Function
'-------------------------------------------------------------------------------
' Function: AddContactGroup
' Description: Adds Contact for given User
' Parameters: UserInstanceID, Name
' Returns: Variant rReturn - (Either null or GroupID)
'-------------------------------------------------------------------------------
Function AddContactGroup(ByVal UserInstanceID, ByVal Name, ByVal strDistributionGroupEmailAddress)
Set oInstances = GetObject("winmgmts:\\" & cComputer).ExecQuery("SELECT * FROM " & cWMIUserContactGroupClass & " WHERE UserInstanceID = '" & UserInstanceID & "'")
For Each oInstance In oInstances
Wscript.echo "GroupID: " & oInstance.GroupID
Wscript.echo "Name: " & oInstance.Name
Wscript.echo "ExternalURL: " & oInstance.ExternalURL
Wscript.echo "UserInstanceID: " & oInstance.UserInstanceID
Wscript.echo "NewGroup: " & Name
Wscript.echo ""
Next
Wscript.Echo "User groups: " & oInstances.Count
Dim vReturn : vReturn = null
Dim oInstances
Dim oInstance
Set oInstances = GetObject("winmgmts:\\" & cComputer).Get(cWMIUserContactGroupClass)
Set oInstance = oInstances.SpawnInstance_
oInstance.UserInstanceID = UserInstanceID
oInstance.ExternalURL = "<groupExtension groupType=" & Chr(34) & "dg" & Chr(34) & "><email>" & strDistributionGroupEmailAddress & "</email></groupExtension>"
oInstance.Name = Name
On Error Resume Next
oInstance.Put_ 2 'Put flag 2 for createOnly
If Err.Number = 0 Then
strCounter = strCounter + 1
End If
Err.Clear
On Error Goto 0
AddContactGroup = vReturn
End Function
Function DeleteContacts(ByVal strPrimaryURI)
Dim UserInstanceId
UserInstanceId = GetUserInstanceID(strPrimaryURI)
Dim oInstances
Dim oInstance
Set oInstances = GetObject("winmgmts:\\" & cComputer).ExecQuery("SELECT * FROM " & cWMIUserContactClass & " WHERE UserInstanceID = '" & UserInstanceID & "'")
If Not oInstances.Count < 1 Then
For Each oInstance In oInstances
On Error Resume Next
oInstance.Delete_
If Err.number = vbEmpty Then
WScript.Echo "[+] Delete successful for contact: " & oInstance.SIPURI
vReturn = 0
Else
WScript.Echo "[-] Delete failed for contact: " & oInstance.SIPURI & " " & Err.number & ": " & Err.Description
vReturn = Err.number
End If
On Error Goto 0
Next
End If
End Function
Sub DeleteGroups (ByVal strPrimaryURI)
Dim UserInstanceId
UserInstanceId = GetUserInstanceID(strPrimaryURI)
Dim oInstances
Dim oInstance
Set oInstances = GetObject("winmgmts:\\" & cComputer).ExecQuery("SELECT * FROM " & cWMIUserContactGroupClass & " WHERE UserInstanceID = '" & UserInstanceID & "'")
For Each oInstance In oInstances
If Not oInstance.Name = "~" Then
On Error Resume Next
oInstance.Delete_
If Err.number = vbEmpty Then
WScript.Echo "[+] Delete successful for group: " & oInstance.Name
vReturn = 0
Else
WScript.Echo "[-] Delete failed for group: " & oInstance.Name & " " & Err.number & ": " & Err.Description
vReturn = Err.number
End If
On Error Goto 0
End If
Next
End Sub
Sub AddContactGroups (ByVal strPrimaryURI)
Dim strUserInstanceId
strUserInstanceId = GetUserInstanceID(strPrimaryURI)
Dim strGroupID
Wscript.echo strPrimaryURI
strGroupID = AddContactGroup(strUserInstanceId, "NameOfGroup1", "EmailOfDistributionGroup1@domain.com")
strGroupID = AddContactGroup(strUserInstanceId, "NameOfGroup2", "EmailOfDistributionGroup2@domain.com")
strGroupID = AddContactGroup(strUserInstanceId, "NameOfGroup3", "EmailOfDistributionGroup3@domain.com")
End Sub
]]>
</script>
<script language="VBScript">
<![CDATA[
Option Explicit
Dim strCounter : strCounter = 0
Dim strPrimaryURI
Dim fDelete : fDelete = WScript.Arguments.Named.Exists("delete")
If Not WScript.Arguments.Named.Exists("usersfile") Then
strPrimaryURI = "sip:" & InputBox("Enter SIP address of user." & VbCrLf & _
"You can use /UsersFile:C:\users.txt parameter if you want to add groups to many users." & VbCrLf & _
"Each line in users.txt need to have the format sip:users@domain.com", "SIP address")
If fDelete Then
DeleteContacts(strPrimaryURI)
DeleteGroups(strPrimaryURI)
Else
AddContactGroups(strPrimaryURI)
End If
Else
Dim strUsersFile : strUsersFile = LCase(Trim(WScript.Arguments.Named.Item("usersfile")))
WScript.Echo "@------------------------------------------------------------"
WScript.Echo "@ Text file with Users: " & strUsersFile
WScript.Echo "@------------------------------------------------------------"
WScript.Echo ""
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim oUsersFile
Dim i : i = 0
'Reading Users from file and adding contacts
If oFS.FileExists(strUsersFile) Then
Set oUsersFile = oFS.OpenTextFile(strUsersFile, 1, false)
Dim strUserSIPAddress
Dim strLineItem
Do Until oUsersFile.AtEndOfStream
strLineItem = Trim(oUsersFile.ReadLine)
ParseUserLineItem(strLineItem) ' Parse each line item and add contacts for it
Loop
Else
fShowUsage = True
WScript.Echo "[Err] Invalid users input file: " & strUsersFile
End If
End If
Wscript.Echo "Created " & strCounter & " groups."
]]>
</script>
</job>
</package>- Moved by OCS Forum Support [MSFT]Moderator Thursday, December 17, 2009 7:12 PM forum migration (From:Contacts)
All Replies
-
Thursday, June 05, 2008 7:28 AMHi

great job
I was looking for something like this. -
Wednesday, December 02, 2009 6:07 AMWow - I've searched all day for something like this - I can't believe Microsoft hasn't built this in to the product. One question though - is it possible for this script to run without all the user intervention? I'm running it and hitting enter about 15 times a user. Is there something I should tweak to just let it run?
Thanks!
- Bill -
Wednesday, June 09, 2010 3:13 PM
You might want to use OCSCM - OCS Contact manager version 2, this application help you to push a contact list to all/some users moreover you can build multiple contact lists and populate/push to a different groups of users, beside many other great OCS administration features.
http://www.ocscm.com