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