473,322 Members | 1,522 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,322 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 1471

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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.