1
votes

Hey I want to use the date out of a txt file in my VBS script what i gotso far is :

Do while not f.AtEndOfLine
Zeile = f.readLine
Feld = split(Zeile,",")
Benutzer = Feld(0)
Gruppe = Feld(1)
Nachname = Feld(2)
Passwort = Feld(3)
ScriptP = Feld(4)
Projekt = Feld(5)
Datum = Feld(6)
Call BenuntzerAnlegen(Benutzer,Gruppe,Nachname,Passwort,ScriptP)
Loop

and It works so fat but the date doesn ist written like this: DD/MM/YYYY in the txt example: AJWKW24Sr1fe,Polo,Rsafa,AMam140981,AJWTN_Logon.cmd,23.06.2016,AJW

I wanted to use it with this :

Sub BenuntzerAnlegen (Benutzer,Gruppe,Nachname,Passwort,ScriptP)
Dim ouo, b
Set ouo = GetObject("LDAP://OU=Gruppinsr,DC=ipcenter,DC=local")
Set b = ouo.Create("user", "CN=" & Gruppe & " " & Nachname)
Dim WshShell, ret
Set WshShell = WScript.CreateObject("WScript.Shell")
set objUser = GetObject("LDAP://OU=Gruppinsr,DC=ipcenter,DC=local")


b.Put "sAMAccountName", Benutzer
b.Put "userPrincipalName", Benutzer & "@ipcenter.local"
b.Put "scriptPath", ScriptP
b.SetInfo
b.SetPassword Passwort
b.AccountDisabled = False
b.AccountExpirationDate = Datum
b.SetInfo

But it doesnt work it Keep telling me missing string or "types mismatch"

2
From where you are getting Datum in Sub BenuntzerAnlegen ? Have you created it as global variable?ManishChristian
In this script parts i posted here are the only BenutzerAnlegen Call BenuntzerAnlegen(Benutzer,Gruppe,Nachname,Passwort,ScriptP) Sub BenuntzerAnlegen (Benutzer,Gruppe,Nachname,Passwort,ScriptP)Nirispa
Im using a Txt file for the Dates because every User has his own "date" Dim fso, f, Zeile, Feld Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile ("C:\user.txt",1,0)Nirispa
I get that. What I am asking is, you have passed other variable to your sub, but I can't see Datum passed, so have you created it as global variable? How Datum is getting value?ManishChristian
Read my previous comment.ManishChristian

2 Answers

1
votes

VBScript date functions (CDate (x to Date), FormatDateTime (Date to String), ...) depend on the regional settings and the locale. This

Option Explicit

Dim aD : aD = Split("01.02.2016 2/1/2016")
Dim aL : aL = Split("en-us de_de")
Dim sd, sl, dd, fd, b
For Each sd In aD
    For Each sl in aL
           SetLocale sl
        On Error Resume Next
           dd = CDate(sd)
           If Err.Number Then
              dd = "ERROR " & Err.Number
              fd = dd
              b  = False
           Else
              fd = FormatDateTime(dd)
              b  = (#2/1/2016# = dd)
           End If
        On Error GoTo 0
           WScript.Echo Join(Array(sl, GetLocale, sd, dd, fd, CStr(b)), vbTab)
    Next
Next

output:

cscript 37968649.vbs
en-us   1033    01.02.2016      ERROR 13        ERROR 13        False
de_de   1031    01.02.2016      01.02.2016      01.02.2016      True
en-us   1033    2/1/2016        2/1/2016        2/1/2016        True
de_de   1031    2/1/2016        02.01.2016      02.01.2016      False

proves that CDate fails (often silently) if the input string's format doesn't match the current locale. So calling

dtDatum = CDate(Feld(6))

while de_de (de-de ?) is active, should give you correct dates for strings in German format.

P.S.:

The line

b.AccountExpirationDate = Datum

'works', because Datum is a global variable; so passing it via arguments is a better practice, but doesn't solve the "type mismatch" problem.

Update wrt comment:

If your locale is non-german and you feed a german date string like "23.06.2016" to VBScript's date functions, you get an error:

>> WScript.Echo GetLocale()
>> d = CDate("23.06.2016")
>>
1033
Error Number:       13
Error Description:  Type mismatch

So parse your input file with a german locale.

Alternatively, you could use the amarican format in the file.

0
votes

If I understood your requirement clearly, you want the date as 23/06/2016 and not as 23.06.2016. If so, just change the line from:

Datum = Feld(6)

to this:

Datum = Replace(Feld(6), ".", "/")

And then you need to pass Datum to your sub that you are calling, like this:

Call BenuntzerAnlegen(Benutzer,Gruppe,Nachname,Passwort,ScriptP,Datum)

And your Sub should be like this:

Sub BenuntzerAnlegen (Benutzer,Gruppe,Nachname,Passwort,ScriptP,Datum)  

Also, per your example data:

AJWKW24Sr1fe,Polo,Rsafa,AMam140981,AJWTN_Logon.cmd,23.06.2016,AJW

Datum will be Feld(5) and not Feld(6). So change it.