473,218 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Get logonHours from AD

Hi,

i need help getting the logonHours from AD for a specific user.
I found a vbscript doing exactly what i want:

On Error Resume Next
Dim arrLogonHoursBytes(20)
Dim arrLogonHoursBits(167)
arrDayOfWeek = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")

Set objUser = GetObject("LDAP://cn=TEST01,ou=abc,dc=mydom,dc=local")
arrLogonHours = objUser.Get("logonHours")
For i = 1 To LenB(arrLogonHours)
arrLogonHoursBytes(i-1) = AscB(MidB(arrLogonHours, i, 1))
'WScript.Echo "MidB returns: " & MidB(arrLogonHours, i, 1)
'WScript.Echo "arrLogonHoursBytes: " & arrLogonHoursBytes(i-1)
'wscript.echo vbcrlf
Next

intCounter = 0
intLoopCounter = 0
'WScript.echo "Day Byte 1 Byte 2 Byte 3"
For Each LogonHourByte In arrLogonHoursBytes
arrLogonHourBits = GetLogonHourBits(LogonHourByte)

If intCounter = 0 Then
WScript.STDOUT.Write arrDayOfWeek(intLoopCounter) & Space(2)
intLoopCounter = intLoopCounter + 1
End If

For Each LogonHourBit In arrLogonHourBits
WScript.STDOUT.Write LogonHourBit
intCounter = 1 + intCounter

If intCounter = 8 or intCounter = 16 Then
Wscript.STDOUT.Write Space(1)
End If
' If intCounter >= 1 Then
' Wscript.STDOUT.Write ";"
' End If

If intCounter = 24 Then
WScript.echo vbCr
intCounter = 0
End If
Next
Next

Function GetLogonHourBits(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
GetLogonHourBits = arrBits
End Function

My problem is to convert thios code to vb.net. I tried it with the following
code, but it returns bullshit....
Public Function Get_LogOnHours() As String

Dim entry As DirectoryEntry = New
DirectoryEntry("LDAP://cn=TEST01,ou=abc,dc=mydom,dc=local")

Dim oSearcher As DirectorySearcher = New DirectorySearcher(entry)

Dim strLogOnHours As String = ""

Dim arrLogonHoursBytes(20) As Long

Dim arrLogonHoursBits(167) As Long

Dim arrDayOfWeek() = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}

Dim i As Integer

oSearcher.Filter = "(&(objectClass=user) (sAMAccountName=" & strUser & "))"

oSearcher.PropertiesToLoad.Add("logonHours")

Try

Dim oResult As SearchResult

oResult = oSearcher.FindOne

If Not oResult.GetDirectoryEntry().Properties("logonHours ").Value.ToString =
"" Then

For i = 1 To
Len(oResult.GetDirectoryEntry().Properties("logonH ours").Value.ToString)

arrLogonHoursBytes(i-1) =
Asc(Mid(oResult.GetDirectoryEntry().Properties("lo gonHours").Value.ToString,
i, 1))

Next

Dim intCounter As Integer = 0

Dim intLoopCounter As Integer = 0
For Each LogonHourByte As Object In arrLogonHoursBytes

Dim arrLogonHourBits() = GetLogonHourBits(LogonHourByte)

If intCounter = 0 Then

strLogOnHours += arrDayOfWeek(intLoopCounter) & " "

intLoopCounter += 1

End If
For Each LogonHourBit As Integer In arrLogonHourBits

strLogOnHours += LogonHourBit & ";"

intCounter += 1

If intCounter = 24 Then

strLogOnHours += vbnewline

intCounter = 0

End If

Next

Next

Get_LogOnHours = strLogOnHours

End If

Catch ex As Exception

MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)

Get_LogOnHours = Nothing

Finally

entry.Dispose()

entry = Nothing

oSearcher = Nothing

End Try

End Function

Private Function GetLogonHourBits(ByVal x) As Object

Dim arrBits(7)

Dim i As Integer

For i = 7 To 0 Step -1

If x And 2 ^ i Then

arrBits(i) = 1

Else

arrBits(i) = 0

End If

Next

GetLogonHourBits = arrBits

End Function

Anyideas to get this to work?

Regards Thomas

May 16 '07 #1
0 1461

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: RF | last post by:
Hi, I am using the DirectoryServices class in .NET and would like to find some information regarding "userAccountControl". I'm adding a user atttributes (C#): user.Properties.Add(512); //...
2
by: JBrek | last post by:
Hi, How can i set the LogonHours to users, using System.DirectoryServices? I think I must create a byte array, but it doesn't work. Thanks in advance, JBrek
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
0
by: Uchi | last post by:
I was able to get the full details about the user by searching. but it does nt have a value called logonhours. how do i get the logonhours of a user using ldap in php Thanks. Uchi
0
by: Uchi | last post by:
Any idea why Im i getting this error Warning: ldap_get_values_len(): Cannot get the value(s) of attribute Decoding error in ... Im trying to read logonhours attribute of a user in Active...
1
by: vhg119 | last post by:
Can someone please link me to some resource listing all the user attributes names? I'm writing some C# code using the System.DirectoryServices assembly. This particular function is suppose to...
6
by: =?Utf-8?B?dGhsMTAwMA==?= | last post by:
Hi NG, i need to list the logonHours for a specific user. I'm trying to convert code from vbscript (is working) to vb.net, but the vb.net code does not work. Here are the code listings: 1:...
1
by: dotyet | last post by:
Hi Everyone, I am looking at a lot of entries similar to the following in my Windows 'Audit' Event viewer. They are all success events and not failures. The environment is: DB2 8.2 FP 14...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.