Problem
You want to see the hours that a user is permitted to log onto the network.
Solution
Using a graphical user interface
- Open the ADUC snap-in.
- If you need to change domains, right-click on “Active Directory Users and Computers” in the left pane, select Connect to Domain, enter the domain name, and click OK.
- Right-click on the user and select Properties. From the Account tab, click on Logon Hours.
- Select the hours that you want to allow or disallow, and click Logon Permitted or Logon Denied. Click OK.
- Click Apply, followed by OK.
Using VBScript
Days = Array _
(“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”)
Set objUser = GetObject(“LDAP://<UserDN>“)
arrHours = objUser.Get(”
logonHours”)
For i = 1 To LenB(arrHours)
arrHoursBytes(i-1) = AscB(MidB(arrHours, i, 1))
WScript.Echo “MidB returns: ” & MidB(arrHours, i, 1)
WScript.Echo “arrHoursBytes: ” & arrHoursBytes(i-1)
wscript.echo vbcrlf
Next
intCounter = 0
intLoopCounter = 0
WScript.echo “Day Byte 1 Byte 2 Byte 3”
For Each HourByte In arrHoursBytes
arrHourBits = DisplayLogonHourBits(HourByte)
If intCounter = 0 Then
WScript.STDOUT.Write Days(intLoopCounter) & Space(2)
intLoopCounter = intLoopCounter + 1
End If
For Each HourBit In arrHourBits
WScript.STDOUT.Write HourBit
intCounter = 1 + intCounter
If intCounter = 8 or intCounter = 16 Then
Wscript.STDOUT.Write Space(1)
End If
If intCounter = 24 Then
WScript.echo vbCr
intCounter = 0
End If
Next
Next
Function DisplayLogonHourBits(x)
Dim arrBits(7)
For i = 7 to 0 Step -1
If x And 2^i Then
arrBits(i) = 1
Else
arrBits(i) = 0
End If
Next
DisplayLogonHourBits = arrBits
End Function
Discussion
Using VBScript
The logonHours attribute of a user object is represented as a binary number, rather than a simple string like most of the other attributes we’ve discussed. Because of this, manipulating it directly is a bit trickier than simply inserting a new string in place of an old one. In the VBScript example shown in this recipe, we use a VBScript function that manipulates the various bits of the attribute to produce the correct values.