Getting logonHours

G

Guest

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: vbscript:

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))
Next

intCounter = 0
intLoopCounter = 0

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 >= 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

######################

2. vb.net

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("logonHours").Value.ToString)

arrLogonHoursBytes(i-1) =
Asc(Mid(oResult.GetDirectoryEntry().Properties("logonHours").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

#####################

The vb.net code returns a string, but the value is not the expected value.

Any ideas to get the vb.net code to work?
 
G

Guest

I am guessing by looking through your code that you are trying to figure out
how long the current user has been logged on.

There are some WMI management classes that can help here.

Specifically, the Win32_LogonSession object exposes a property called
StartTime, which is when the user logged on. You could then do a simple
DateTime.Now.Subtract(<StartTime>).TotalHours to get the hours that the user
has been logged on.

If you are using Visual Studio, you can see and generate code for this
property at:

Server Explorer >> Servers >> <machine name> >> Management Classes >>
Desktop Settings >> <account name> >> User Accounts >> <user account> >> User
Logon Sessions >> <session item>
 
G

Guest

Hi,

thanks for the answer, but you are wrong. I am trying to get the hours a
user can log on to the domain.
 
P

PlatinumBay

Hi,

I think I got it:

You will need the following imports:

Imports System.Security.Principal
Imports System.DirectoryServices

Code:

Sub Main()
Dim localMachine As New DirectoryEntry("WinNT://" &
Environment.MachineName)
Dim admGroup As DirectoryEntry =
localMachine.Children.Find("administrators", "group")
Dim members As Object = admGroup.Invoke("members", Nothing)
For Each groupMember As Object In CType(members, IEnumerable)
Dim member As New DirectoryEntry(groupMember)
Console.WriteLine(member.Name)

Dim si As New
SecurityIdentifier(CType(member.Properties("objectSid")(0), Byte()), 0)
Dim sid As String = si.Value
Console.WriteLine(sid)

Dim lh As Array = CType(member.Properties("LoginHours").Value,
Array)

' Note: the hours are in UTC

' You will need to convert the 21 bytes in the lh array into the
168 bits which represent each hour of the week.
Next

Console.ReadLine()
End Sub


Hope this helps

- Steve
 
G

Guest

Hi PaltinumBay,

converting the 21 bytes in the lh array into the
168 bits is the problem, connecting to the domain via ldap already works...

Regards Thomas
 
P

PlatinumBay

thl1000,

In that case, check out the following code which utilizes the BitArray class
in the System.Collections namespace:

Sub TestUserLoginHours()
Dim localMachine As New DirectoryEntry("WinNT://" &
Environment.MachineName)
Dim admGroup As DirectoryEntry =
localMachine.Children.Find("administrators", "group")
Dim members As Object = admGroup.Invoke("members", Nothing)
For Each groupMember As Object In CType(members, IEnumerable)
Dim member As New DirectoryEntry(groupMember)
Console.WriteLine(member.Name)

Dim si As New
SecurityIdentifier(CType(member.Properties("objectSid")(0), Byte()), 0)
Dim sid As String = si.Value
Console.WriteLine(sid)

Dim lh As Array = CType(member.Properties("LoginHours").Value,
Array)

' Note: the hours are in UTC
' You will need to convert the 21 bytes in the lh array into the
168 bits which represent each hour of the week.

For Each item As Byte In lh
Dim ba As New BitArray(New Byte() {item})

' switching the byte order by stepping backwards
For x As Integer = 7 To 0 Step -1
Console.Write(ba.Get(x) & " ")
Next
Console.WriteLine()
Next
Console.WriteLine()
Next
End Sub


Hope that helps,

Steve
 
Joined
Jan 30, 2023
Messages
1
Reaction score
0
I am guessing by looking through your code that you are trying to figure out
how long the current user has been logged on.

There are some WMI management classes that can help here.

Specifically, the Win32_LogonSession object exposes a property called
StartTime, which is when the user logged on. You could then do a simple
DateTime.Now.Subtract(<StartTime>).TotalHours to get the hours that the user
has been logged on.

If you are using Visual Studio, you can see and generate code for this
property at:

Server Explorer >> Servers >> <machine name> >> Management Classes >>
Desktop Settings >> <account name> >> User Accounts >> <user account> >> User
Logon Sessions >> <session item>

Really liked your suggestion but kindly can share the code so I can use in my excel plz

Regards,
Atif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top