0

how can I write UTF-8 encoded strings to a textfile from VBScript? I have tried some variations but no success. So I need the text file saved in UTF-8 format. thanks in advance for your all help.

Output :

CN=™ser1 D˜,OU=TEST,OU=TEST,OU=Users,OU=contoso,DC=contoso,DC=local;10.01.2012 01:00:00
CN=Gšbson ¦LU,OU=TEST,OU=TEST,OU=Users,OU=contoso,DC=contoso,DC=local;20.12.2016 18:55:51
CN=™ZL €ET˜,OU=TEST,OU=TEST,OU=Users,OU=contoso,DC=contoso,DC=local;27.08.2013

type ExpReport.txt (as you can see no special characters)

CN=ser1 D,OU=TEST,OU=TEST,OU=Users,OU=contoso,DC=contoso,DC=local;10.01.2012 01:00:00
CN=Gbson LU,OU=TEST,OU=TEST,OU=Users,OU=contoso,DC=contoso,DC=local;20.12.2016 18:55:51
CN=ZL ET,OU=TEST,OU=TEST,OU=Users,OU=contoso,DC=contoso,DC=local;27.08.2013
cscript //nologo AcctsExpire.vbs > ExpReport.txt

Here is my code :

Option Explicit

Dim adoConnection, adoCommand
Dim objRootDSE, strDNSDomain, strFilter, strQuery, adoRecordset
Dim strDN, objShell, lngBiasKey, lngBias
Dim lngDate, objDate, dtmAcctExp, k

' Obtain local time zone bias from machine registry.
Set objShell = CreateObject("Wscript.Shell")
lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _
    & "TimeZoneInformation\ActiveTimeBias")
If (UCase(TypeName(lngBiasKey)) = "LONG") Then
    lngBias = lngBiasKey
ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
    lngBias = 0
    For k = 0 To UBound(lngBiasKey)
        lngBias = lngBias + (lngBiasKey(k) * 256^k)
    Next
End If

' Use ADO to search the domain for all users.
Set adoConnection = CreateObject("ADODB.Connection")
Set adoCommand = CreateObject("ADODB.Command")
adoConnection.Provider = "ADsDSOOBject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Determine the DNS domain from the RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

' Filter to retrieve all user objects with accounts
' that expire.
strFilter = "(&(objectCategory=person)(objectClass=user)" _
    & "(!accountExpires=0)(!accountExpires=9223372036854775807))"

strQuery = "<LDAP://" & strDNSDomain & ">;" & strFilter _
    & ";distinguishedName,accountExpires;subtree"

' Run the query.
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute

' Enumerate the recordset.
Do Until adoRecordset.EOF
    strDN = adoRecordset.Fields("distinguishedName").Value
    lngDate = adoRecordset.Fields("accountExpires")
    Set objDate = lngDate
    dtmAcctExp = Integer8Date(objDate, lngBias)
    Wscript.Echo strDN & ";" & dtmAcctExp
    adoRecordset.MoveNext
Loop
adoRecordset.Close

' Clean up.
adoConnection.Close

Function Integer8Date(ByVal objDate, ByVal lngBias)
    ' Function to convert Integer8 (64-bit) value to a date, adjusted for
    ' local time zone bias.
    Dim lngAdjust, lngDate, lngHigh, lngLow
    lngAdjust = lngBias
    lngHigh = objDate.HighPart
    lngLow = objdate.LowPart
    ' Account for bug in IADslargeInteger property methods.
    If (lngLow < 0) Then
        lngHigh = lngHigh + 1
    End If
    If (lngHigh = 0) And (lngLow = 0) Then
        lngAdjust = 0
    End If
    lngDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
        + lngLow) / 600000000 - lngAdjust) / 1440
    Integer8Date = CDate(lngDate)
End Function

Last Update :

Issue is resolved.

cscript //nologo AcctsExpire.vbs //U > ExpReport.txt

Also WSCRIPT AcctsExpire.vbs displays correct output.

Arbelac
  • 1,698
  • 6
  • 37
  • 90
  • http://stackoverflow.com/questions/2524703/save-text-file-utf-8-encoded-with-vba ;) – R3uK Oct 05 '16 at 12:50
  • thanks but I have tried it too. But no luck – Arbelac Oct 05 '16 at 13:16
  • This is VBScript not VBA. – user692942 Oct 05 '16 at 13:30
  • Possible duplicate of [Unicode characters in Windows command line - how?](http://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how) – user692942 Oct 05 '16 at 13:37
  • The issue is outputting the result of the your `WScript.Echo()` method calls to the `cmd.exe` standard output which not related directly to VBScript. – user692942 Oct 05 '16 at 13:41
  • @Lankymart as you mentioned I have reviewed those links. But I am getting same issue how can I rewrite my script? – Arbelac Oct 05 '16 at 13:43
  • **1**. What output from `type ExpReport.txt`? **2**. What should be those garbled `CN`s really? **3**. What is output from `cscript AcctsExpire.vbs` without redirecting to a file? – JosefZ Oct 05 '16 at 14:16
  • @JosefZ I have updated my question BTW What should be those garbled CNs really? No – Arbelac Oct 05 '16 at 14:29
  • @Arbelac When someone asks a question that starts with "What" it rarely ends with "Yes" or "No". – user692942 Oct 05 '16 at 15:23
  • 1
    I can't help you without an [mcve]. I doubt that `>` redirection could create a valid `UTF-8` encoded file. However, `cscript /?` says something about `//U` switch: _`//U` Use Unicode for redirected I/O from the console_. This produces a UTF16-LE file without BOM on my system… – JosefZ Oct 05 '16 at 15:28
  • Would `WScript.Echo` display correct output using `WSCRIPT AcctsExpire.vbs`? BTW, I don't care about `OU`s but only about _garbled_ parts of `CN`s. – JosefZ Oct 05 '16 at 15:40
  • @JosefZ I'll try `//U` switch and `WSCRIPT AcctsExpire.vbs` tomorrow. I have checked CNs. I can't find garbled `CN`s. I am sorry before my short answer. – Arbelac Oct 05 '16 at 17:24
  • JosefZ Actually what I want to do is : as you know we are using Encoding UTF8 for Export-CSV in powershell. What is the equavailent for VBScript? or How can I do that ? I hope my explanation is very clear. if you need more information please let me know – Arbelac Oct 05 '16 at 17:28
  • @JosefZ My issue is resolved I have updated my question. Thanks again. – Arbelac Oct 06 '16 at 05:30

0 Answers0