Enclose attribute (variable) in quotes ""

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.
Locked
User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

Enclose attribute (variable) in quotes ""

Post by new_user » Tue May 06, 2014 10:53 am

My code works fine but I have not successfully been able to have each output variable be enclosed by quotes which is a late ask so I did not want to transfer this to ps. How can I ensure each variable/attribute be written as "username";"givenName" etc instead of username;givenName? Code attached. Thank you in advance for the help.
Attachments
code.txt
(8.02 KiB) Downloaded 316 times

User avatar
jvierra
Posts: 11048
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Enclose attribute (variable) in quotes ""

Post by jvierra » Tue May 06, 2014 11:14 am

Just add quotes.
VBScript Code
Double-click the code block to select all.
qq=""""
qc=""","""

line = qq & myvar1 & qc & myvar2 & qc & myvar3 & qq
That gives you quotes and commas.

User avatar
jvierra
Posts: 11048
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Enclose attribute (variable) in quotes ""

Post by jvierra » Tue May 06, 2014 11:42 am

This is closer to what you want and is easier to manage.

You can do the header by looping the fields also. I will leave that to you.
VBScript Code
Double-click the code block to select all.
set objFSO = CreateObject("scripting.filesystemobject")
set objFile = objFSO.createtextfile(".\Review" & "_" & Year(Now()) & Month(Now()) & Day(Now()) & ".txt" , 2, True)

strDNSDomain = GetObject("LDAP://RootDSE").Get("defaultNamingContext")

strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
strAttributes = "samaccountname,lastLogonTimeStamp,useraccountcontrol,pwdLastset,employeetype,company,mail,sn,givenName,modifyTimeStamp,initials,manager,title,department,employeeid,l,streetAddress,postalCode,postofficeBox,st,manager,co,TelephoneNumber,physicalDeliveryOfficeName,mobile"
adoCommand.CommandText = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"

Set adoCommand = CreateObject("ADODB.Command")
adoCommand.ActiveConnection = adoConnection
adoCommand.Properties("Page Size") = 500
Set adoRecordset = adoCommand.Execute

Do Until adoRecordset.EOF
    With adoRecordset
    WriteField .Fields("givenName").Value
    WriteField .Fields("initials").Value
    WriteField .Fields("sn").Value
    WriteField .Fields("samaccountname").Value
    WriteField .Fields("mail").Value
    WriteField .Fields("employeeID").Value
    
    WriteField .Fields("streetAddress").Value
    WriteField .Fields("l").Value
    WriteField .Fields("st").Value
    WriteField .Fields("postalCode").Value
    WriteField .Fields("co").Value
    WriteField .Fields("physicalDeliveryOfficeName")
    
    WriteField .Fields("TelephoneNumber").Value
    WriteField .Fields("mobile").Value
    
    WriteField .Fields("company").Value
    WriteField .Fields("title").Value
    WriteField .Fields("department").Value
    WriteField .Fields("manager").Value

    WriteField .Fields("postOfficeBox").Value

    intUAC=.Fields("userAccountControl").Value
    If intUAC AND 2 Then  'ADS_UF_ACCOUNTDISABLE = 2
     	strStatus="DISABLED"
    Else
     	strStatus="ENABLED"
    End If
    WriteField strStatus
    
    WriteField ConvertDate(.Fields("pwdLastSet").Value)
    
    ' specil handling for last column just close quotes
    strDate = ConvertDate(.Fields("lastLogonTimeStamp").Value)
    objFile.WriteLine """" & strDate & """"
    .MoveNext
Loop

Wscript.echo "Script Complete"
objFile.Close

Function WriteField( strValue )
    If IsNull(strValue) Then
        objFile.Write "," 'just use comma for empty field
    Else
        objFile.Write strValue & """," ' close quote and add comma.
    End If
End Function

Function ConvertDate( adDate )

    If (TypeName(adDate) = "Object") Then
        Set objDate = .Fields("pwdLastSet").Value
        dtmPwdLastSet = Integer8Date(objDate, lngBias)
    Else
        dtmPwdLastSet = #1/1/1601#
    End If

End Function

User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

Re: Enclose attribute (variable) in quotes ""

Post by new_user » Tue May 06, 2014 12:19 pm

Thank you for the help, I will give that a shot.

Locked

Who is online

Users browsing this forum: No registered users and 4 guests