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.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 9 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
new_user
Posts: 157
Last visit: Tue May 06, 2014 5:46 pm

Enclose attribute (variable) in quotes ""

Post by new_user »

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 523 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Enclose attribute (variable) in quotes ""

Post by jvierra »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Enclose attribute (variable) in quotes ""

Post by jvierra »

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
Last visit: Tue May 06, 2014 5:46 pm

Re: Enclose attribute (variable) in quotes ""

Post by new_user »

Thank you for the help, I will give that a shot.
This topic is 9 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked