VBS Script to add Lync Contacts to all users who are a member of a Group

This is based off and extends the LyncAddContacts.vbs script found here: http://www.expta.com/2011/01/introducing-lyncaddcontacts.html

After much searching on the web I was unable to find a script to meet my needs with Lync, so I put this together and cleaned it up a bit. This of course requires the LyncAddContacts script with the dbimpexp.exe tool pulled from the Lync install DVD or iso. You will also need a template user (which can be created with a mailbox, account hidden from exchange addressbook, and account diabled after adding all the contacts and groups to the Lync). You will also need to run and probably schedule task this with an account that has all the permissions needed to pull export and import contacts for your lync users.

To use this script place in same directory as your other scripts on the Lync server and change the 3 const variables located near the top of the script. Enjoy

Update: I changed the script a little, turns out I should not use a @ symbol in a string so I replaced it with chr(64). Also another gottcha I ran into was that run in path needs to be your scripts folder with exe and both vbs scripts if you are using the task scheduler. Also this can be resource intensive as it updates all the users directly through SQL. So ether adjust your resources acordingly or only run this during early morning or late evenings.

'LyncAddContactGroups.vbs
'Script Used to import Contact Groups to all users in Lyncmplate
'Author: Paul Cardelli
'Date last Modified: 3/17/12
'-------------------------------------------------------------------------
 Option Explicit
 Dim objRootDSE, strDomain, objGroup, objUser, WShell, arrMemberOf, strMember, strSIPTemplate

 strSIPTemplate = "LyncTemplateUser" & Chr(64) & "domain.com"
 Const strGroupCN = "LDAP://CN=All Users,ou=User Groups,"
 Const strLyncScriptPath = "d:\Scripts\"

 Set WShell = WScript.CreateObject("Wscript.Shell")

 WShell.Run "cscript " & strLyncScriptPath & "LyncAddContacts.vbs /backup backup.xml", 0, False
 WShell.Run "cscript " & strLyncScriptPath & "LyncAddContacts.vbs " & strTemplateSIP, 0, False

' Retrieve domain information
 Set objRootDSE = GetObject("LDAP://RootDSE")
 strDomain = objRootDSE.Get("DefaultNamingContext")
 Set objGroup = GetObject(strGroupCN & strDomain)
 objGroup.GetInfo
 arrMemberOf = objGroup.GetEx("member")

'Pull all e-mail Addresses into an array, and apply Template to each User
 For Each strMember in arrMemberOf
     Set objUser = GetObject("LDAP://" & strMember)
     WShell.Run "cscript " & strLyncScriptPath & "LyncAddContacts.vbs /import " & objUser.mail, 0, False
 Next

WScript.Quit
Advertisements

VBS script sub routine to send e-mail

Needed something to send a e-mail from a VBS script when things were not going as planned, and thus came this sub. (If you plan to use this on a system with SMTP configured, then omit the configuration field lines)

 
Sub SendNotification (EmailFrom, EmailTo, EmailSubject, ErrorMessage, FileAttachment)
	Set Notify = CreateObject("CDO.Message")
 	With Notify
 		.From = EmailFrom
 		.To = EmailTo
 		.Subject = EmailSubject
 		.TextBody = ErrorMessage
 		.AddAttachment FileAttachment
 		.Configuration.Fields.Item _
 			("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
 		.Configuration.Fields.Item _
 			("http://schemas.microsoft.com/cdo/configuration/sendusing")="smtp.domain.com"
 		.Configuration.Fields.Item _
			("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 
		.Configuration.Fields.Update
 		.Send
 	End With
 	Set Notify = Nothing
End Sub

Just Change the smtp.domain.com to either the FQDN or IP of your mail server.

Here is an example of how to call the routine in your script:

 
  SendNotifcation _
"johndoe@domain.com", "janedoe@domain.com", "Error in Script", "Error In line 12", "c:\Logs.txt"

Changing Static DNS Search List Using a VBS login Script

This script is fuctional, but rare occurance of no DNS servers has happen when either the delete takes longer then expected or something happens at login that inerupts the completion of the login script. This is why I added a pre-check to see if the change is even needed in the first place.

The only Values needed to be changed are arrNewDNSServerSearchOrder which is the list of new DNS server you wish to add, strOneOfTheOldDNSServers which is a common DNS server already used that you may wish to change or add to, and strOneOfTheNewDNSServers which is one of the new DNS servers you plan to add so the script does not need to fully run at every login (only when change is needed).

If there are any ideas for fixing the rare “No DNS issue” or a better way to do this in PowerShell, please comment. Otherwise enjoy.

'==============================================================
'Script to update DNS settings (Such as adding more then one DNS server to the targeted NIC)
'
'By Paul - https://networksavy.wordpress.com
'Date: 08/23/2010
'==============================================================

'====================================================
'New List of DNS Servers to Apply
'====================================================
arrNewDNSServerSearchOrder = Array( "192.168.1.2", _
                                    "192.168.2.2", _
                                    "192.168.3.2"  )
                                    
'to find the right adapter(s) to change:
' This is the value that checks the right nic (A common DNS server that is already used)
strOneOfTheOldDNSServers = "192.168.2.19"
' This is to check if a value is already correct (So the replacement does not run again if not needed)
strOneOfTheNewDNSServers = "192.168.1.19"


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


Set colNetCards = objWMIService.ExecQuery _
    ("Select DNSServerSearchOrder From Win32_NetworkAdapterConfiguration")

'============================================
'
' Test if the script even needs to be applied
' If the new DNS server exists then the script quits
'
'============================================
For Each objNetCard in colNetCards
  blnFound = 0
  arrDNSServerSearchOrder = objNetCard.DNSServerSearchOrder
  	If Not IsNull(arrDNSServerSearchOrder) Then
    	For i = 0 to UBound(arrDNSServerSearchOrder)
      		If (arrDNSServerSearchOrder(i) = strOneOfTheNewDNSServers) Then
				WScript.Quit
        Exit For
      End If
    Next    
  End If
Next

'============================================
'
' Replaces DNS Servers with new servers
'
'============================================
For Each objNetCard in colNetCards
  blnFound = 0
  arrDNSServerSearchOrder = objNetCard.DNSServerSearchOrder
  If Not IsNull(arrDNSServerSearchOrder) Then
    For i = 0 to UBound(arrDNSServerSearchOrder)
      If (arrDNSServerSearchOrder(i) = strOneOfTheOldDNSServers) Then
        arrDNSServerSearchOrder = Array()
        objNetCard.SetDNSServerSearchOrder(arrDNSServerSearchOrder)
        wscript.sleep 600
        arrDNSServerSearchOrder = arrNewDNSServerSearchOrder
        objNetCard.SetDNSServerSearchOrder(arrDNSServerSearchOrder)
        Exit For
      End If
    Next    
  End If
Next

WScript.Quit

VBS Script based on date

Now this may be a little out there for some people but I created a VBS logon script that would popup a website based on the current date on the computer. Now keep in mind the trick is the format of the date.

'########################
' SCHEDULEDPOPUPS.vbs
' Scheduled URL Message Popups
' Author: Paul - https://networksavy.wordpress.com
' Date Created: 8/17/2010
' Used to Schedule System Wide Popup Messages at login
'#########################
' PopUpBetween (STARTDate,ENDDATE,POPUPURL,WIDTH,HEIGHT,fromLEFT,fromTOP)           Popup between these dates
'
' Date format is #YYYY-MM-DD#
'
'Example:
'PopUpBetween #2010-08-02#, #2010-08-04#, 470, 410, 300, 250, "http://www.google.com"
'This launches google between dates 8/2/2010 and 8/4/2010
'
'On Error Resume Next
'===================================================
' Scheduled Popups  Enter Schedule here One per line
'===================================================
'            |  START   |  |   END    |  W  |  H |  L |  T |  URL for popup
PopUpBetween #2010-08-02#, #2010-08-04#, 470, 410, 300, 250, "http://www.google.com"
'            |  START   |  |   END    |  W  |  H |  L |  T |  URL for popup

'-------------------------------------------------------------------
'  Sub for popups Between Two Dates, Date format is #YYYY-MM-DD#
'-------------------------------------------------------------------
Sub PopUpBetween (STARTDate,ENDDATE,WIDTH,HEIGHT,fromLEFT,fromTOP,POPUPURL)
	If (Date + 1) > STARTDATE And (Date - 1) < ENDDATE Then ' Ensures that the current date falls within the start and end date
		InternetExplorer POPUPURL,WIDTH,HEIGHT,fromLEFT,fromTOP ' Executes POPUPURL using the InternetExplorer Sub                                                          
	End If
End Sub

'----------------------------------------
' Sub for Internet Explorer
'----------------------------------------

Sub InternetExplorer (POPUPURL,WIDTH,HEIGHT,fromLEFT,fromTOP)
	With WScript.CreateObject ("InternetExplorer.Application")        'Start Internet Explorer
	  .Navigate POPUPURL                                '<------------ Navigate IE to a URL
      .fullscreen = 0                                   '<------------ Toggle Fullscreen mode 0=off 1=on
      .left       = fromLEFT                            '<------------ Possition from the left of screen
      .top        = fromTOP                             '<------------ Possition from the top of screen
      .Width      = WIDTH                               '<------------ Width of IE window
      .Height     = HEIGHT                              '<------------ Height of IE window
      .ToolBar    = 0                                   '<------------ Toggle IE Toolbar 0=off 1=on
      .MenuBar    = 0                                   '<------------ Toggle IE Menubar 0=off 1=on
      .StatusBar  = 0                                   '<------------ Toggle IE Statusbar 0=off 1=on
      .StatusText = ""                                  '<------------ Set status text in the status bar
      .Visible    = 1                                   '<------------ Toggle IE Statusbar 0=off 1=on
 	End With
End Sub

Now one could take this logic and use it with other tasks.