473,387 Members | 1,485 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,387 software developers and data experts.

Getting logonHours

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("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

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

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?
May 16 '07 #1
6 4401
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 nameManagement Classes >>
Desktop Settings ><account nameUser Accounts ><user accountUser
Logon Sessions ><session item>
"thl1000" wrote:
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("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

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

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?
May 16 '07 #2
Hi,

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

"PlatinumBay" wrote:
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 nameManagement Classes >>
Desktop Settings ><account nameUser Accounts ><user accountUser
Logon Sessions ><session item>
"thl1000" wrote:
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("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

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

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?
May 16 '07 #3
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("object Sid")(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

"thl1000" <th*****@discussions.microsoft.comwrote in message
news:DD**********************************@microsof t.com...
Hi,

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

"PlatinumBay" wrote:
>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 nameManagement Classes >>
Desktop Settings ><account nameUser Accounts ><user account>
User
Logon Sessions ><session item>
"thl1000" wrote:
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("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

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

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?

Jun 3 '07 #4
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

"PlatinumBay" wrote:
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("object Sid")(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

"thl1000" <th*****@discussions.microsoft.comwrote in message
news:DD**********************************@microsof t.com...
Hi,

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

"PlatinumBay" wrote:
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 nameManagement Classes >>
Desktop Settings ><account nameUser Accounts ><user account>
User
Logon Sessions ><session item>
"thl1000" wrote:

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("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

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

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?


Jun 4 '07 #5
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("object Sid")(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

"thl1000" <th*****@discussions.microsoft.comwrote in message
news:1F**********************************@microsof t.com...
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

"PlatinumBay" wrote:
>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("objec tSid")(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

"thl1000" <th*****@discussions.microsoft.comwrote in message
news:DD**********************************@microso ft.com...
Hi,

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

"PlatinumBay" wrote:

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 nameManagement Classes >>
Desktop Settings ><account nameUser Accounts ><user account>

User
Logon Sessions ><session item>
"thl1000" wrote:

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("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

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

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?



Jun 4 '07 #6
Hi Steve,

i will test the code next weekend... :-)

Regards Thomas

"PlatinumBay" wrote:
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("object Sid")(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

"thl1000" <th*****@discussions.microsoft.comwrote in message
news:1F**********************************@microsof t.com...
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

"PlatinumBay" wrote:
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("object Sid")(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

"thl1000" <th*****@discussions.microsoft.comwrote in message
news:DD**********************************@microsof t.com...
Hi,

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

"PlatinumBay" wrote:

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 nameManagement Classes >>
Desktop Settings ><account nameUser Accounts ><user account>
>>
User
Logon Sessions ><session item>
"thl1000" wrote:

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("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
Jun 4 '07 #7

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

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
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
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
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: Si | last post by:
I'm writing an HttpModule to intecept web method calls and retrieve custom authentication information from their SOAP headers. I have the httpModules tag in web.config and that appears to be...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
2
by: MSK | last post by:
Hi, Continued to my earlier post regaring "Breakpoints are not getting hit" , I have comeup with more input this time.. Kindly give me some idea. I am a newbie to .NET, recently I installed...
4
by: R.Manikandan | last post by:
Hi In my code, one string variable is subjected to contain more amount of characters. If it cross certain limit, the string content in the varabile is automatically getting truncated and i am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.