Home All Groups Group Topic Archive Search About

Limit user logon time in a domain Windows Server 2003 script



Author
28 Nov 2007 1:35 PM
Gustavo
Hello scritpting guys:

I need your help again, I need to set the limit user logon time, but reading
from a txt or csv file, I found this code from Richard, I think it's ok, but
I need to reed it from a txt file.

I hope you can help me.

Author
28 Nov 2007 3:49 PM
Jeffery Hicks [MVP]
Usually this is done with account restrictions in Active Directory or even a
GPO.  Or are you trying to read a list of users from a text file and make
the change to the user account?

--
Jeffery Hicks
Microsoft PowerShell MVP
http://www.scriptinganswers.com
http://www.powershellcommunity.org

Now Available: WSH and VBScript Core: TFM
Coming Soon: Windows PowerShell: TFM 2nd Ed.

Show quote
"Gustavo" <Gust***@discussions.microsoft.com> wrote in message
news:DFD3C9D1-CD52-4896-882F-7C247B5A414C@microsoft.com...
> Hello scritpting guys:
>
> I need your help again, I need to set the limit user logon time, but
> reading
> from a txt or csv file, I found this code from Richard, I think it's ok,
> but
> I need to reed it from a txt file.
>
> I hope you can help me.
Author
28 Nov 2007 4:40 PM
Gustavo
Hi Jeffery, that's right, I'll read the users from a txt file, to set the
logonhours.
I ran this code:
****************//*******************
' LogonHours.vbs
' VBScript program to document hours of the week when a given Active
' Directory user is allowed to logon, using the logonHours attribute.
'
' ----------------------------------------------------------------------
' Copyright (c) 2002 Richard L. Mueller
' Hilltop Lab web site - http://www.rlmueller.net
' Version 1.0 - November 10, 2002
' Version 1.1 - February 19, 2003 - Standardize Hungarian notation.
' Version 1.2 - May 19, 2003 - Bug fixes.
' Version 1.3 - January 25, 2004 - Modify error trapping.
'
' This script is designed to be run at a command prompt, using the
' Cscript host. For example:
' cscript //nologo LogonHours.vbs DistinguishedName
' DistinguishedName can be similar to:
'     "cn=TestUser,ou=Sales,dc=MyDomain,dc=com"
'
' You have a royalty-free right to use, modify, reproduce, and
' distribute this script file in any way you find useful, provided that
' you agree that the copyright owner above has no warranty, obligations,
' or liability for such use.

Option Explicit

Dim objShell, lngBias, arrstrDayOfWeek, strUserDN
Dim arrbytLogonHours(20)
Dim arrintLogonHoursBits(167)
Dim bytLogonHours, lngBiasKey
Dim bytLogonHour, intLogonHour, strLine
Dim objUser, k, intCounter, intLoopCounter, j, m

' Check for required argument.
If (Wscript.Arguments.Count = 0) Then
    Wscript.Echo "Error, required argument missing."
    Wscript.Echo "LogonHours.vbs"
    Wscript.Echo "Program to document allowed logon hours"
    Wscript.Echo "Syntax:"
    Wscript.Echo "cscript LogonHours.vbs DN"
    Wscript.Echo "where DN is the DistinguishedName of an AD user."
    Wscript.Echo "For example, DN could be:"
    Wscript.Echo "  cn=TestUser,ou=Sales,dc=MyDomain,dc=com"
    Wscript.Quit(1)
End If

strUserDN = Wscript.Arguments(0)

' Bind to the specified user object with the LDAP provider.
On Error Resume Next
Set objUser = GetObject("LDAP://" & strUserDN)
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "User not found in Active Directory"
    Wscript.Echo strUserDN
    Wscript.Quit(1)
End If
On Error GoTo 0

' Determine the time zone bias from the local registry.
Set objShell = CreateObject("Wscript.Shell")
lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _
    & "TimeZoneInformation\Bias")
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
lngBias = Round(lngBias/60)

arrstrDayOfWeek = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")

Wscript.Echo "User: " & objUser.name

' Retrieve the user's logonHours attribute.
objUser.GetInfoEx Array("logonHours"), 0
bytLogonHours = objUser.Get("logonHours")

' Populate a byte array.
For k = 1 To LenB(bytLogonHours)
    arrbytLogonHours(k - 1) = AscB(MidB(bytLogonHours, k, 1))
Next

' Populate a bit array, offset by the time zone bias.
j = 0
For Each bytLogonHour In arrbytLogonHours
    For k = 7 To 0 Step -1
        m = 8*j + k - lngBias
        If (m < 0) Then
            m = m + 168
        End If
        If (bytLogonHour And 2^k) Then
            arrintLogonHoursBits(m) = 1
        Else
            arrintLogonHoursBits(m) = 0
        End If
    Next
    j = j + 1
Next

' Output the bit array, one day per line, 24 hours per day.
intCounter = 0
intLoopCounter = 0
Wscript.Echo "Day"
Wscript.Echo "of   ------- Hour of the Day -------"
Wscript.Echo "Week M-3 3-6 6-9 9-N N-3 3-6 6-9 9-M"
For Each intLogonHour In arrintLogonHoursBits
    If (intCounter = 0) Then
        strLine = arrstrDayOfWeek(intLoopCounter) & "  "
        intLoopCounter = intLoopCounter + 1
    End If
    strLine = strLine & intLogonHour
    intCounter  = intCounter + 1
    If (intCounter = 3) Or (intCounter = 6) Or (intCounter = 9) _
            Or (intCounter = 12) Or (intCounter = 15) Or (intCounter = 18) _
            Or (intCounter = 21) Then
        strLine = strLine & " "
    End If
    If (intCounter = 24) Then
        Wscript.Echo strLine
        intCounter = 0
    End If
Next

' Clean up.
Set objUser = Nothing
Set objShell = Nothing
********************//********************
and I got this error:
the directory property cannot be found int the cache

in this line:
bytLogonHours = objUser.Get("logonHours")

What's wrong?

Show quote
"Jeffery Hicks [MVP]" wrote:

> Usually this is done with account restrictions in Active Directory or even a
> GPO.  Or are you trying to read a list of users from a text file and make
> the change to the user account?
>
> --
> Jeffery Hicks
> Microsoft PowerShell MVP
> http://www.scriptinganswers.com
> http://www.powershellcommunity.org
>
> Now Available: WSH and VBScript Core: TFM
> Coming Soon: Windows PowerShell: TFM 2nd Ed.
>
> "Gustavo" <Gust***@discussions.microsoft.com> wrote in message
> news:DFD3C9D1-CD52-4896-882F-7C247B5A414C@microsoft.com...
> > Hello scritpting guys:
> >
> > I need your help again, I need to set the limit user logon time, but
> > reading
> > from a txt or csv file, I found this code from Richard, I think it's ok,
> > but
> > I need to reed it from a txt file.
> >
> > I hope you can help me.
>
Author
28 Nov 2007 6:55 PM
Richard Mueller [MVP]
Gustavo wrote:

> Hi Jeffery, that's right, I'll read the users from a txt file, to set the
> logonhours.
> I ran this code:
> ****************//*******************
> ' LogonHours.vbs
> ' VBScript program to document hours of the week when a given Active
> ' Directory user is allowed to logon, using the logonHours attribute.
> '
> ' ----------------------------------------------------------------------
> ' Copyright (c) 2002 Richard L. Mueller

.... snip

> ' Retrieve the user's logonHours attribute.
> objUser.GetInfoEx Array("logonHours"), 0
> bytLogonHours = objUser.Get("logonHours")

....snip

> ********************//********************
> and I got this error:
> the directory property cannot be found int the cache
>
> in this line:
> bytLogonHours = objUser.Get("logonHours")
>
> What's wrong?

My fault. The program raises that error if the user never had logonHours
assigned. The ADUC GUI shows the user is allowed to logon during all hours
of the week, but the logonHours attribute for the user does not have a value
assigned. That accounts for the error you got. I need to revise the program
LogonHours.vbs to trap this error and display the allowed logon hours (all
hours of the week) correctly.

However, if your intent is to modify or assign logon hours this program will
not help. It only documents the current assignment. The only scripting
method I have seen to assign logon hours is to copy the attribute from one
user to another. For example, this link from the Technet Scripting Center:

http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true

The problem is that the logonHours attribute is datatype OctetString, which
is a byte array, and VBScript cannot declare or create a byte array.
VBScript can read a byte array, but cannot even modify it. Over the years
I've tried various techniques and all have failed. However, I have been able
to assign values to the logonHours attribute in VB where you can declare
variables as Byte().

I will modify the program LogonHours.vbs, but I want to test before posting
the new script to my web site. If your intent is to run the program
LogonHours.vbs to document the allowed logon hours for a list of users, that
can be done. Let me first fix the basic program and then I'll post back with
a version that reads names from a text file.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
Author
28 Nov 2007 7:26 PM
Gustavo
thanks Richard for your help, I you're right I intent to set the logonhours
to the users listed in txt file.

Show quote
"Richard Mueller [MVP]" wrote:

> Gustavo wrote:
>
> > Hi Jeffery, that's right, I'll read the users from a txt file, to set the
> > logonhours.
> > I ran this code:
> > ****************//*******************
> > ' LogonHours.vbs
> > ' VBScript program to document hours of the week when a given Active
> > ' Directory user is allowed to logon, using the logonHours attribute.
> > '
> > ' ----------------------------------------------------------------------
> > ' Copyright (c) 2002 Richard L. Mueller
>
> .... snip
>
> > ' Retrieve the user's logonHours attribute.
> > objUser.GetInfoEx Array("logonHours"), 0
> > bytLogonHours = objUser.Get("logonHours")
>
> ....snip
>
> > ********************//********************
> > and I got this error:
> > the directory property cannot be found int the cache
> >
> > in this line:
> > bytLogonHours = objUser.Get("logonHours")
> >
> > What's wrong?
>
> My fault. The program raises that error if the user never had logonHours
> assigned. The ADUC GUI shows the user is allowed to logon during all hours
> of the week, but the logonHours attribute for the user does not have a value
> assigned. That accounts for the error you got. I need to revise the program
> LogonHours.vbs to trap this error and display the allowed logon hours (all
> hours of the week) correctly.
>
> However, if your intent is to modify or assign logon hours this program will
> not help. It only documents the current assignment. The only scripting
> method I have seen to assign logon hours is to copy the attribute from one
> user to another. For example, this link from the Technet Scripting Center:
>
> http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true
>
> The problem is that the logonHours attribute is datatype OctetString, which
> is a byte array, and VBScript cannot declare or create a byte array.
> VBScript can read a byte array, but cannot even modify it. Over the years
> I've tried various techniques and all have failed. However, I have been able
> to assign values to the logonHours attribute in VB where you can declare
> variables as Byte().
>
> I will modify the program LogonHours.vbs, but I want to test before posting
> the new script to my web site. If your intent is to run the program
> LogonHours.vbs to document the allowed logon hours for a list of users, that
> can be done. Let me first fix the basic program and then I'll post back with
> a version that reads names from a text file.
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>
Author
29 Nov 2007 1:01 AM
Richard Mueller [MVP]
I don't believe there is any way to set the logonHours attribute in
VBScript. The only option I know of is to copy the attribute from another
user.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--

Show quote
"Gustavo" <Gust***@discussions.microsoft.com> wrote in message
news:9626829F-B4EF-44C6-B5C3-94C7F1121C78@microsoft.com...
> thanks Richard for your help, I you're right I intent to set the
> logonhours
> to the users listed in txt file.
>
> "Richard Mueller [MVP]" wrote:
>
>> Gustavo wrote:
>>
>> > Hi Jeffery, that's right, I'll read the users from a txt file, to set
>> > the
>> > logonhours.
>> > I ran this code:
>> > ****************//*******************
>> > ' LogonHours.vbs
>> > ' VBScript program to document hours of the week when a given Active
>> > ' Directory user is allowed to logon, using the logonHours attribute.
>> > '
>> > ' ----------------------------------------------------------------------
>> > ' Copyright (c) 2002 Richard L. Mueller
>>
>> .... snip
>>
>> > ' Retrieve the user's logonHours attribute.
>> > objUser.GetInfoEx Array("logonHours"), 0
>> > bytLogonHours = objUser.Get("logonHours")
>>
>> ....snip
>>
>> > ********************//********************
>> > and I got this error:
>> > the directory property cannot be found int the cache
>> >
>> > in this line:
>> > bytLogonHours = objUser.Get("logonHours")
>> >
>> > What's wrong?
>>
>> My fault. The program raises that error if the user never had logonHours
>> assigned. The ADUC GUI shows the user is allowed to logon during all
>> hours
>> of the week, but the logonHours attribute for the user does not have a
>> value
>> assigned. That accounts for the error you got. I need to revise the
>> program
>> LogonHours.vbs to trap this error and display the allowed logon hours
>> (all
>> hours of the week) correctly.
>>
>> However, if your intent is to modify or assign logon hours this program
>> will
>> not help. It only documents the current assignment. The only scripting
>> method I have seen to assign logon hours is to copy the attribute from
>> one
>> user to another. For example, this link from the Technet Scripting
>> Center:
>>
>> http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true
>>
>> The problem is that the logonHours attribute is datatype OctetString,
>> which
>> is a byte array, and VBScript cannot declare or create a byte array.
>> VBScript can read a byte array, but cannot even modify it. Over the years
>> I've tried various techniques and all have failed. However, I have been
>> able
>> to assign values to the logonHours attribute in VB where you can declare
>> variables as Byte().
>>
>> I will modify the program LogonHours.vbs, but I want to test before
>> posting
>> the new script to my web site. If your intent is to run the program
>> LogonHours.vbs to document the allowed logon hours for a list of users,
>> that
>> can be done. Let me first fix the basic program and then I'll post back
>> with
>> a version that reads names from a text file.
>>
>> --
>> Richard Mueller
>> Microsoft MVP Scripting and ADSI
>> Hilltop Lab - http://www.rlmueller.net
>> --
>>
>>
>>
Author
28 Nov 2007 8:04 PM
Gustavo
thanks Richard for your help, I you're right I intent to set the logonhours
to the users listed in txt file.


Show quote
"Richard Mueller [MVP]" wrote:

> Gustavo wrote:
>
> > Hi Jeffery, that's right, I'll read the users from a txt file, to set the
> > logonhours.
> > I ran this code:
> > ****************//*******************
> > ' LogonHours.vbs
> > ' VBScript program to document hours of the week when a given Active
> > ' Directory user is allowed to logon, using the logonHours attribute.
> > '
> > ' ----------------------------------------------------------------------
> > ' Copyright (c) 2002 Richard L. Mueller
>
> .... snip
>
> > ' Retrieve the user's logonHours attribute.
> > objUser.GetInfoEx Array("logonHours"), 0
> > bytLogonHours = objUser.Get("logonHours")
>
> ....snip
>
> > ********************//********************
> > and I got this error:
> > the directory property cannot be found int the cache
> >
> > in this line:
> > bytLogonHours = objUser.Get("logonHours")
> >
> > What's wrong?
>
> My fault. The program raises that error if the user never had logonHours
> assigned. The ADUC GUI shows the user is allowed to logon during all hours
> of the week, but the logonHours attribute for the user does not have a value
> assigned. That accounts for the error you got. I need to revise the program
> LogonHours.vbs to trap this error and display the allowed logon hours (all
> hours of the week) correctly.
>
> However, if your intent is to modify or assign logon hours this program will
> not help. It only documents the current assignment. The only scripting
> method I have seen to assign logon hours is to copy the attribute from one
> user to another. For example, this link from the Technet Scripting Center:
>
> http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true
>
> The problem is that the logonHours attribute is datatype OctetString, which
> is a byte array, and VBScript cannot declare or create a byte array.
> VBScript can read a byte array, but cannot even modify it. Over the years
> I've tried various techniques and all have failed. However, I have been able
> to assign values to the logonHours attribute in VB where you can declare
> variables as Byte().
>
> I will modify the program LogonHours.vbs, but I want to test before posting
> the new script to my web site. If your intent is to run the program
> LogonHours.vbs to document the allowed logon hours for a list of users, that
> can be done. Let me first fix the basic program and then I'll post back with
> a version that reads names from a text file.
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>

AddThis Social Bookmark Button