Please Read: Significant Update Planned, Migrating Forum Software This Month

See more
See less

Handling null values in vbscript

  • Filter
  • Time
  • Show
Clear All
new posts

  • Handling null values in vbscript

    I've got a script that creates new users from a spreadsheet. One of the fields I want to use is initials (middle initial). Not everyone has a middle initial and when I run the script where that cell is empty, the script files with the error:

    createUsers.vbs path to script(37, 2) (null): The attribute syntax specified to the directory service is invalid.
    The script is as follows (I thought "" & would make sure the variable isn't null):

    Option Explicit
    Dim objRootLDAP, objContainer, objUser, objShell
    Dim objExcel, objSpread, intRow
    Dim strUser, strOU, strSheet
    Dim strCN, strSam, strFirst, strLast, strInitials, strExt, strPWD, strScriptPath
    strSheet = "pathToSpreadSheet.xls"
    ' Bind to Active Directory, Users container.
    Set objRootLDAP = GetObject("LDAP://rootDSE")
    Set objContainer = GetObject("LDAP://OU=NewUsers," & objRootLDAP.Get("defaultNamingContext")) 
    ' Open the Excel spreadsheet
    Set objExcel = CreateObject("Excel.Application")
    Set objSpread = objExcel.Workbooks.Open(strSheet)
    intRow = 2 'Row 1 often contains headings
    ' Here is the 'DO...Loop' that cycles through the cells
    ' Note intRow, x must correspond to the column in strSheet
    Do Until objExcel.Cells(intRow,1).Value = ""
    	strFirst = Trim(objExcel.Cells(intRow, 1).Value)
    	strLast = Trim(objExcel.Cells(intRow, 2).Value)
    	strInitials = "" & Trim(objExcel.Cells(intRow, 3).Value)
    	strExt = Trim(objExcel.Cells(intRow, 10).Value)
    	strSam = Trim(objExcel.Cells(intRow, 14).Value)
    	strCN = Trim(objExcel.Cells(intRow, 15).Value) 
    	' Build the actual User from data in strSheet.
    	Set objUser = objContainer.Create("User", "cn=" & strCN)
    	objUser.sAMAccountName = strSam
    	objUser.givenName = strFirst = strLast
    	objUser.initials = strInitials
    	objUser.telephoneNumber = ("303.963." & strExt)
    	strPWD = "password"
    	strScriptPath = "loginScript.bat"
    	' Separate section to enable account with its password
    	objUser.userAccountControl = 512
    	objUser.pwdLastSet = 0
    	objUser.SetPassword strPWD
    	objUser.Put "scriptPath", strScriptPath
    	intRow = intRow + 1
    How do I make sure strInitials isn't null without forcing HR to put something in the "Middle Initial" field in the spreadsheet?


  • #2
    Re: Handling null values in vbscript

    	If (strInitials <> "") Then objUser.initials = strInitials

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts


    • #3
      Re: Handling null values in vbscript

      Bingo! Thanks.

      For others who see this, I replaced objUser.initials = strInitials with Rems's line.