Getting logonHours

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?dGhsMTAwMA==?=

    #1

    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 arrLogonHoursBy tes(20)
    Dim arrLogonHoursBi ts(167)
    arrDayOfWeek = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")

    Set objUser = GetObject("LDAP ://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
    arrLogonHours = objUser.Get("lo gonHours")


    For i = 1 To LenB(arrLogonHo urs)
    arrLogonHoursBy tes(i-1) = AscB(MidB(arrLo gonHours, i, 1))
    Next

    intCounter = 0
    intLoopCounter = 0

    For Each LogonHourByte In arrLogonHoursBy tes
    arrLogonHourBit s = GetLogonHourBit s(LogonHourByte )

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

    For Each LogonHourBit In arrLogonHourBit s
    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 GetLogonHourBit s(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
    GetLogonHourBit s = arrBits
    End Function

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

    2. vb.net

    Public Function Get_LogOnHours( ) As String

    Dim entry As DirectoryEntry = New
    DirectoryEntry( "LDAP://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")

    Dim oSearcher As DirectorySearch er = New DirectorySearch er(entry)

    Dim strLogOnHours As String = ""

    Dim arrLogonHoursBy tes(20) As Long

    Dim arrLogonHoursBi ts(167) As Long

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

    Dim i As Integer

    oSearcher.Filte r = "(&(objectClass =user) (sAMAccountName =" & strUser & "))"

    oSearcher.Prope rtiesToLoad.Add ("logonHours ")

    Try

    Dim oResult As SearchResult

    oResult = oSearcher.FindO ne

    If Not oResult.GetDire ctoryEntry().Pr operties("logon Hours").Value.T oString =
    "" Then

    For i = 1 To
    Len(oResult.Get DirectoryEntry( ).Properties("l ogonHours").Val ue.ToString)

    arrLogonHoursBy tes(i-1) =
    Asc(Mid(oResult .GetDirectoryEn try().Propertie s("logonHours") .Value.ToString ,
    i, 1))

    Next

    Dim intCounter As Integer = 0

    Dim intLoopCounter As Integer = 0


    For Each LogonHourByte As Object In arrLogonHoursBy tes

    Dim arrLogonHourBit s() = GetLogonHourBit s(LogonHourByte )

    If intCounter = 0 Then

    strLogOnHours += arrDayOfWeek(in tLoopCounter) & " "

    intLoopCounter += 1

    End If


    For Each LogonHourBit As Integer In arrLogonHourBit s

    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", MessageBoxButto ns.OK,
    MessageBoxIcon. Error)

    Get_LogOnHours = Nothing

    Finally

    entry.Dispose()

    entry = Nothing

    oSearcher = Nothing

    End Try

    End Function

    Private Function GetLogonHourBit s(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

    GetLogonHourBit s = 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?
  • =?Utf-8?B?UGxhdGludW1CYXk=?=

    #2
    RE: Getting logonHours

    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_LogonSess ion object exposes a property called
    StartTime, which is when the user logged on. You could then do a simple
    DateTime.Now.Su btract(<StartTi me>).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 arrLogonHoursBy tes(20)
    Dim arrLogonHoursBi ts(167)
    arrDayOfWeek = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
    >
    Set objUser = GetObject("LDAP ://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
    arrLogonHours = objUser.Get("lo gonHours")
    >
    >
    For i = 1 To LenB(arrLogonHo urs)
    arrLogonHoursBy tes(i-1) = AscB(MidB(arrLo gonHours, i, 1))
    Next
    >
    intCounter = 0
    intLoopCounter = 0
    >
    For Each LogonHourByte In arrLogonHoursBy tes
    arrLogonHourBit s = GetLogonHourBit s(LogonHourByte )
    >
    If intCounter = 0 Then
    WScript.STDOUT. Write arrDayOfWeek(in tLoopCounter) & Space(2)
    intLoopCounter = intLoopCounter + 1
    End If
    >
    For Each LogonHourBit In arrLogonHourBit s
    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 GetLogonHourBit s(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
    GetLogonHourBit s = arrBits
    End Function
    >
    ############### #######
    >
    2. vb.net
    >
    Public Function Get_LogOnHours( ) As String
    >
    Dim entry As DirectoryEntry = New
    DirectoryEntry( "LDAP://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
    >
    Dim oSearcher As DirectorySearch er = New DirectorySearch er(entry)
    >
    Dim strLogOnHours As String = ""
    >
    Dim arrLogonHoursBy tes(20) As Long
    >
    Dim arrLogonHoursBi ts(167) As Long
    >
    Dim arrDayOfWeek() = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
    >
    Dim i As Integer
    >
    oSearcher.Filte r = "(&(objectClass =user) (sAMAccountName =" & strUser & "))"
    >
    oSearcher.Prope rtiesToLoad.Add ("logonHours ")
    >
    Try
    >
    Dim oResult As SearchResult
    >
    oResult = oSearcher.FindO ne
    >
    If Not oResult.GetDire ctoryEntry().Pr operties("logon Hours").Value.T oString =
    "" Then
    >
    For i = 1 To
    Len(oResult.Get DirectoryEntry( ).Properties("l ogonHours").Val ue.ToString)
    >
    arrLogonHoursBy tes(i-1) =
    Asc(Mid(oResult .GetDirectoryEn try().Propertie s("logonHours") .Value.ToString ,
    i, 1))
    >
    Next
    >
    Dim intCounter As Integer = 0
    >
    Dim intLoopCounter As Integer = 0
    >
    >
    For Each LogonHourByte As Object In arrLogonHoursBy tes
    >
    Dim arrLogonHourBit s() = GetLogonHourBit s(LogonHourByte )
    >
    If intCounter = 0 Then
    >
    strLogOnHours += arrDayOfWeek(in tLoopCounter) & " "
    >
    intLoopCounter += 1
    >
    End If
    >
    >
    For Each LogonHourBit As Integer In arrLogonHourBit s
    >
    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", MessageBoxButto ns.OK,
    MessageBoxIcon. Error)
    >
    Get_LogOnHours = Nothing
    >
    Finally
    >
    entry.Dispose()
    >
    entry = Nothing
    >
    oSearcher = Nothing
    >
    End Try
    >
    End Function
    >
    Private Function GetLogonHourBit s(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
    >
    GetLogonHourBit s = 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?

    Comment

    • =?Utf-8?B?dGhsMTAwMA==?=

      #3
      RE: Getting logonHours

      Hi,

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

      "PlatinumBa y" 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_LogonSess ion object exposes a property called
      StartTime, which is when the user logged on. You could then do a simple
      DateTime.Now.Su btract(<StartTi me>).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 arrLogonHoursBy tes(20)
      Dim arrLogonHoursBi ts(167)
      arrDayOfWeek = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")

      Set objUser = GetObject("LDAP ://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
      arrLogonHours = objUser.Get("lo gonHours")


      For i = 1 To LenB(arrLogonHo urs)
      arrLogonHoursBy tes(i-1) = AscB(MidB(arrLo gonHours, i, 1))
      Next

      intCounter = 0
      intLoopCounter = 0

      For Each LogonHourByte In arrLogonHoursBy tes
      arrLogonHourBit s = GetLogonHourBit s(LogonHourByte )

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

      For Each LogonHourBit In arrLogonHourBit s
      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 GetLogonHourBit s(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
      GetLogonHourBit s = arrBits
      End Function

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

      2. vb.net

      Public Function Get_LogOnHours( ) As String

      Dim entry As DirectoryEntry = New
      DirectoryEntry( "LDAP://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")

      Dim oSearcher As DirectorySearch er = New DirectorySearch er(entry)

      Dim strLogOnHours As String = ""

      Dim arrLogonHoursBy tes(20) As Long

      Dim arrLogonHoursBi ts(167) As Long

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

      Dim i As Integer

      oSearcher.Filte r = "(&(objectClass =user) (sAMAccountName =" & strUser & "))"

      oSearcher.Prope rtiesToLoad.Add ("logonHours ")

      Try

      Dim oResult As SearchResult

      oResult = oSearcher.FindO ne

      If Not oResult.GetDire ctoryEntry().Pr operties("logon Hours").Value.T oString =
      "" Then

      For i = 1 To
      Len(oResult.Get DirectoryEntry( ).Properties("l ogonHours").Val ue.ToString)

      arrLogonHoursBy tes(i-1) =
      Asc(Mid(oResult .GetDirectoryEn try().Propertie s("logonHours") .Value.ToString ,
      i, 1))

      Next

      Dim intCounter As Integer = 0

      Dim intLoopCounter As Integer = 0


      For Each LogonHourByte As Object In arrLogonHoursBy tes

      Dim arrLogonHourBit s() = GetLogonHourBit s(LogonHourByte )

      If intCounter = 0 Then

      strLogOnHours += arrDayOfWeek(in tLoopCounter) & " "

      intLoopCounter += 1

      End If


      For Each LogonHourBit As Integer In arrLogonHourBit s

      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", MessageBoxButto ns.OK,
      MessageBoxIcon. Error)

      Get_LogOnHours = Nothing

      Finally

      entry.Dispose()

      entry = Nothing

      oSearcher = Nothing

      End Try

      End Function

      Private Function GetLogonHourBit s(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

      GetLogonHourBit s = 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?

      Comment

      • PlatinumBay

        #4
        Re: Getting logonHours

        Hi,

        I think I got it:

        You will need the following imports:

        Imports System.Security .Principal
        Imports System.Director yServices

        Code:

        Sub Main()
        Dim localMachine As New DirectoryEntry( "WinNT://" &
        Environment.Mac hineName)
        Dim admGroup As DirectoryEntry =
        localMachine.Ch ildren.Find("ad ministrators", "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.WriteLi ne(member.Name)

        Dim si As New
        SecurityIdentif ier(CType(membe r.Properties("o bjectSid")(0), Byte()), 0)
        Dim sid As String = si.Value
        Console.WriteLi ne(sid)

        Dim lh As Array = CType(member.Pr operties("Login Hours").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.ReadLin e()
        End Sub


        Hope this helps

        - Steve



        "thl1000" <thl1000@discus sions.microsoft .comwrote in message
        news:DDC25A33-6AF8-4925-8922-7B2C4D262EC9@mi crosoft.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.
        >
        "PlatinumBa y" 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_LogonSess ion object exposes a property called
        >StartTime, which is when the user logged on. You could then do a simple
        >DateTime.Now.S ubtract(<StartT ime>).TotalHour s 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 arrLogonHoursBy tes(20)
        Dim arrLogonHoursBi ts(167)
        arrDayOfWeek = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
        >
        Set objUser = GetObject("LDAP ://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
        arrLogonHours = objUser.Get("lo gonHours")
        >
        >
        For i = 1 To LenB(arrLogonHo urs)
        arrLogonHoursBy tes(i-1) = AscB(MidB(arrLo gonHours, i, 1))
        Next
        >
        intCounter = 0
        intLoopCounter = 0
        >
        For Each LogonHourByte In arrLogonHoursBy tes
        arrLogonHourBit s = GetLogonHourBit s(LogonHourByte )
        >
        If intCounter = 0 Then
        WScript.STDOUT. Write arrDayOfWeek(in tLoopCounter) & Space(2)
        intLoopCounter = intLoopCounter + 1
        End If
        >
        For Each LogonHourBit In arrLogonHourBit s
        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 GetLogonHourBit s(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
        GetLogonHourBit s = arrBits
        End Function
        >
        ############### #######
        >
        2. vb.net
        >
        Public Function Get_LogOnHours( ) As String
        >
        Dim entry As DirectoryEntry = New
        DirectoryEntry( "LDAP://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
        >
        Dim oSearcher As DirectorySearch er = New DirectorySearch er(entry)
        >
        Dim strLogOnHours As String = ""
        >
        Dim arrLogonHoursBy tes(20) As Long
        >
        Dim arrLogonHoursBi ts(167) As Long
        >
        Dim arrDayOfWeek() = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
        >
        Dim i As Integer
        >
        oSearcher.Filte r = "(&(objectClass =user) (sAMAccountName =" & strUser &
        "))"
        >
        oSearcher.Prope rtiesToLoad.Add ("logonHours ")
        >
        Try
        >
        Dim oResult As SearchResult
        >
        oResult = oSearcher.FindO ne
        >
        If Not
        oResult.GetDire ctoryEntry().Pr operties("logon Hours").Value.T oString =
        "" Then
        >
        For i = 1 To
        Len(oResult.Get DirectoryEntry( ).Properties("l ogonHours").Val ue.ToString)
        >
        arrLogonHoursBy tes(i-1) =
        Asc(Mid(oResult .GetDirectoryEn try().Propertie s("logonHours") .Value.ToString ,
        i, 1))
        >
        Next
        >
        Dim intCounter As Integer = 0
        >
        Dim intLoopCounter As Integer = 0
        >
        >
        For Each LogonHourByte As Object In arrLogonHoursBy tes
        >
        Dim arrLogonHourBit s() = GetLogonHourBit s(LogonHourByte )
        >
        If intCounter = 0 Then
        >
        strLogOnHours += arrDayOfWeek(in tLoopCounter) & " "
        >
        intLoopCounter += 1
        >
        End If
        >
        >
        For Each LogonHourBit As Integer In arrLogonHourBit s
        >
        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", MessageBoxButto ns.OK,
        MessageBoxIcon. Error)
        >
        Get_LogOnHours = Nothing
        >
        Finally
        >
        entry.Dispose()
        >
        entry = Nothing
        >
        oSearcher = Nothing
        >
        End Try
        >
        End Function
        >
        Private Function GetLogonHourBit s(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
        >
        GetLogonHourBit s = 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?

        Comment

        • =?Utf-8?B?dGhsMTAwMA==?=

          #5
          Re: Getting logonHours

          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

          "PlatinumBa y" wrote:
          Hi,
          >
          I think I got it:
          >
          You will need the following imports:
          >
          Imports System.Security .Principal
          Imports System.Director yServices
          >
          Code:
          >
          Sub Main()
          Dim localMachine As New DirectoryEntry( "WinNT://" &
          Environment.Mac hineName)
          Dim admGroup As DirectoryEntry =
          localMachine.Ch ildren.Find("ad ministrators", "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.WriteLi ne(member.Name)
          >
          Dim si As New
          SecurityIdentif ier(CType(membe r.Properties("o bjectSid")(0), Byte()), 0)
          Dim sid As String = si.Value
          Console.WriteLi ne(sid)
          >
          Dim lh As Array = CType(member.Pr operties("Login Hours").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.ReadLin e()
          End Sub
          >
          >
          Hope this helps
          >
          - Steve
          >
          >
          >
          "thl1000" <thl1000@discus sions.microsoft .comwrote in message
          news:DDC25A33-6AF8-4925-8922-7B2C4D262EC9@mi crosoft.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.

          "PlatinumBa y" 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_LogonSess ion object exposes a property called
          StartTime, which is when the user logged on. You could then do a simple
          DateTime.Now.Su btract(<StartTi me>).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 arrLogonHoursBy tes(20)
          Dim arrLogonHoursBi ts(167)
          arrDayOfWeek = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")

          Set objUser = GetObject("LDAP ://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
          arrLogonHours = objUser.Get("lo gonHours")


          For i = 1 To LenB(arrLogonHo urs)
          arrLogonHoursBy tes(i-1) = AscB(MidB(arrLo gonHours, i, 1))
          Next

          intCounter = 0
          intLoopCounter = 0

          For Each LogonHourByte In arrLogonHoursBy tes
          arrLogonHourBit s = GetLogonHourBit s(LogonHourByte )

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

          For Each LogonHourBit In arrLogonHourBit s
          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 GetLogonHourBit s(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
          GetLogonHourBit s = arrBits
          End Function

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

          2. vb.net

          Public Function Get_LogOnHours( ) As String

          Dim entry As DirectoryEntry = New
          DirectoryEntry( "LDAP://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")

          Dim oSearcher As DirectorySearch er = New DirectorySearch er(entry)

          Dim strLogOnHours As String = ""

          Dim arrLogonHoursBy tes(20) As Long

          Dim arrLogonHoursBi ts(167) As Long

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

          Dim i As Integer

          oSearcher.Filte r = "(&(objectClass =user) (sAMAccountName =" & strUser &
          "))"

          oSearcher.Prope rtiesToLoad.Add ("logonHours ")

          Try

          Dim oResult As SearchResult

          oResult = oSearcher.FindO ne

          If Not
          oResult.GetDire ctoryEntry().Pr operties("logon Hours").Value.T oString =
          "" Then

          For i = 1 To
          Len(oResult.Get DirectoryEntry( ).Properties("l ogonHours").Val ue.ToString)

          arrLogonHoursBy tes(i-1) =
          Asc(Mid(oResult .GetDirectoryEn try().Propertie s("logonHours") .Value.ToString ,
          i, 1))

          Next

          Dim intCounter As Integer = 0

          Dim intLoopCounter As Integer = 0


          For Each LogonHourByte As Object In arrLogonHoursBy tes

          Dim arrLogonHourBit s() = GetLogonHourBit s(LogonHourByte )

          If intCounter = 0 Then

          strLogOnHours += arrDayOfWeek(in tLoopCounter) & " "

          intLoopCounter += 1

          End If


          For Each LogonHourBit As Integer In arrLogonHourBit s

          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", MessageBoxButto ns.OK,
          MessageBoxIcon. Error)

          Get_LogOnHours = Nothing

          Finally

          entry.Dispose()

          entry = Nothing

          oSearcher = Nothing

          End Try

          End Function

          Private Function GetLogonHourBit s(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

          GetLogonHourBit s = 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?
          >
          >
          >

          Comment

          • PlatinumBay

            #6
            Re: Getting logonHours

            thl1000,

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

            Sub TestUserLoginHo urs()
            Dim localMachine As New DirectoryEntry( "WinNT://" &
            Environment.Mac hineName)
            Dim admGroup As DirectoryEntry =
            localMachine.Ch ildren.Find("ad ministrators", "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.WriteLi ne(member.Name)

            Dim si As New
            SecurityIdentif ier(CType(membe r.Properties("o bjectSid")(0), Byte()), 0)
            Dim sid As String = si.Value
            Console.WriteLi ne(sid)

            Dim lh As Array = CType(member.Pr operties("Login Hours").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(b a.Get(x) & " ")
            Next
            Console.WriteLi ne()
            Next
            Console.WriteLi ne()
            Next
            End Sub


            Hope that helps,

            Steve

            "thl1000" <thl1000@discus sions.microsoft .comwrote in message
            news:1FE88DF0-CA84-4F94-A88E-F53DE0D34AFC@mi crosoft.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
            >
            "PlatinumBa y" wrote:
            >
            >Hi,
            >>
            >I think I got it:
            >>
            >You will need the following imports:
            >>
            >Imports System.Security .Principal
            >Imports System.Director yServices
            >>
            >Code:
            >>
            > Sub Main()
            > Dim localMachine As New DirectoryEntry( "WinNT://" &
            >Environment.Ma chineName)
            > Dim admGroup As DirectoryEntry =
            >localMachine.C hildren.Find("a dministrators", "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.WriteLi ne(member.Name)
            >>
            > Dim si As New
            >SecurityIdenti fier(CType(memb er.Properties(" objectSid")(0), Byte()), 0)
            > Dim sid As String = si.Value
            > Console.WriteLi ne(sid)
            >>
            > Dim lh As Array =
            >CType(member.P roperties("Logi nHours").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.ReadLin e()
            > End Sub
            >>
            >>
            >Hope this helps
            >>
            > - Steve
            >>
            >>
            >>
            >"thl1000" <thl1000@discus sions.microsoft .comwrote in message
            >news:DDC25A3 3-6AF8-4925-8922-7B2C4D262EC9@mi crosoft.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.
            >
            "PlatinumBa y" 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_LogonSess ion object exposes a property called
            >StartTime, which is when the user logged on. You could then do a
            >simple
            >DateTime.Now.S ubtract(<StartT ime>).TotalHour s 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 arrLogonHoursBy tes(20)
            Dim arrLogonHoursBi ts(167)
            arrDayOfWeek = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
            "Sat")
            >
            Set objUser = GetObject("LDAP ://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
            arrLogonHours = objUser.Get("lo gonHours")
            >
            >
            For i = 1 To LenB(arrLogonHo urs)
            arrLogonHoursBy tes(i-1) = AscB(MidB(arrLo gonHours, i, 1))
            Next
            >
            intCounter = 0
            intLoopCounter = 0
            >
            For Each LogonHourByte In arrLogonHoursBy tes
            arrLogonHourBit s = GetLogonHourBit s(LogonHourByte )
            >
            If intCounter = 0 Then
            WScript.STDOUT. Write arrDayOfWeek(in tLoopCounter) & Space(2)
            intLoopCounter = intLoopCounter + 1
            End If
            >
            For Each LogonHourBit In arrLogonHourBit s
            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 GetLogonHourBit s(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
            GetLogonHourBit s = arrBits
            End Function
            >
            ############### #######
            >
            2. vb.net
            >
            Public Function Get_LogOnHours( ) As String
            >
            Dim entry As DirectoryEntry = New
            DirectoryEntry( "LDAP://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
            >
            Dim oSearcher As DirectorySearch er = New DirectorySearch er(entry)
            >
            Dim strLogOnHours As String = ""
            >
            Dim arrLogonHoursBy tes(20) As Long
            >
            Dim arrLogonHoursBi ts(167) As Long
            >
            Dim arrDayOfWeek() = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
            "Sat"}
            >
            Dim i As Integer
            >
            oSearcher.Filte r = "(&(objectClass =user) (sAMAccountName =" & strUser
            &
            "))"
            >
            oSearcher.Prope rtiesToLoad.Add ("logonHours ")
            >
            Try
            >
            Dim oResult As SearchResult
            >
            oResult = oSearcher.FindO ne
            >
            If Not
            oResult.GetDire ctoryEntry().Pr operties("logon Hours").Value.T oString
            =
            "" Then
            >
            For i = 1 To
            Len(oResult.Get DirectoryEntry( ).Properties("l ogonHours").Val ue.ToString)
            >
            arrLogonHoursBy tes(i-1) =
            Asc(Mid(oResult .GetDirectoryEn try().Propertie s("logonHours") .Value.ToString ,
            i, 1))
            >
            Next
            >
            Dim intCounter As Integer = 0
            >
            Dim intLoopCounter As Integer = 0
            >
            >
            For Each LogonHourByte As Object In arrLogonHoursBy tes
            >
            Dim arrLogonHourBit s() = GetLogonHourBit s(LogonHourByte )
            >
            If intCounter = 0 Then
            >
            strLogOnHours += arrDayOfWeek(in tLoopCounter) & " "
            >
            intLoopCounter += 1
            >
            End If
            >
            >
            For Each LogonHourBit As Integer In arrLogonHourBit s
            >
            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", MessageBoxButto ns.OK,
            MessageBoxIcon. Error)
            >
            Get_LogOnHours = Nothing
            >
            Finally
            >
            entry.Dispose()
            >
            entry = Nothing
            >
            oSearcher = Nothing
            >
            End Try
            >
            End Function
            >
            Private Function GetLogonHourBit s(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
            >
            GetLogonHourBit s = 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?
            >>
            >>
            >>

            Comment

            • =?Utf-8?B?dGhsMTAwMA==?=

              #7
              Re: Getting logonHours

              Hi Steve,

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

              Regards Thomas

              "PlatinumBa y" wrote:
              thl1000,
              >
              In that case, check out the following code which utilizes the BitArray class
              in the System.Collecti ons namespace:
              >
              Sub TestUserLoginHo urs()
              Dim localMachine As New DirectoryEntry( "WinNT://" &
              Environment.Mac hineName)
              Dim admGroup As DirectoryEntry =
              localMachine.Ch ildren.Find("ad ministrators", "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.WriteLi ne(member.Name)
              >
              Dim si As New
              SecurityIdentif ier(CType(membe r.Properties("o bjectSid")(0), Byte()), 0)
              Dim sid As String = si.Value
              Console.WriteLi ne(sid)
              >
              Dim lh As Array = CType(member.Pr operties("Login Hours").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(b a.Get(x) & " ")
              Next
              Console.WriteLi ne()
              Next
              Console.WriteLi ne()
              Next
              End Sub
              >
              >
              Hope that helps,
              >
              Steve
              >
              "thl1000" <thl1000@discus sions.microsoft .comwrote in message
              news:1FE88DF0-CA84-4F94-A88E-F53DE0D34AFC@mi crosoft.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

              "PlatinumBa y" wrote:
              Hi,
              >
              I think I got it:
              >
              You will need the following imports:
              >
              Imports System.Security .Principal
              Imports System.Director yServices
              >
              Code:
              >
              Sub Main()
              Dim localMachine As New DirectoryEntry( "WinNT://" &
              Environment.Mac hineName)
              Dim admGroup As DirectoryEntry =
              localMachine.Ch ildren.Find("ad ministrators", "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.WriteLi ne(member.Name)
              >
              Dim si As New
              SecurityIdentif ier(CType(membe r.Properties("o bjectSid")(0), Byte()), 0)
              Dim sid As String = si.Value
              Console.WriteLi ne(sid)
              >
              Dim lh As Array =
              CType(member.Pr operties("Login Hours").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.ReadLin e()
              End Sub
              >
              >
              Hope this helps
              >
              - Steve
              >
              >
              >
              "thl1000" <thl1000@discus sions.microsoft .comwrote in message
              news:DDC25A33-6AF8-4925-8922-7B2C4D262EC9@mi crosoft.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.

              "PlatinumBa y" 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_LogonSess ion object exposes a property called
              StartTime, which is when the user logged on. You could then do a
              simple
              DateTime.Now.Su btract(<StartTi me>).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 arrLogonHoursBy tes(20)
              Dim arrLogonHoursBi ts(167)
              arrDayOfWeek = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
              "Sat")

              Set objUser = GetObject("LDAP ://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")
              arrLogonHours = objUser.Get("lo gonHours")


              For i = 1 To LenB(arrLogonHo urs)
              arrLogonHoursBy tes(i-1) = AscB(MidB(arrLo gonHours, i, 1))
              Next

              intCounter = 0
              intLoopCounter = 0

              For Each LogonHourByte In arrLogonHoursBy tes
              arrLogonHourBit s = GetLogonHourBit s(LogonHourByte )

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

              For Each LogonHourBit In arrLogonHourBit s
              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 GetLogonHourBit s(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
              GetLogonHourBit s = arrBits
              End Function

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

              2. vb.net

              Public Function Get_LogOnHours( ) As String

              Dim entry As DirectoryEntry = New
              DirectoryEntry( "LDAP://cn=TEST01,ou=ab c,dc=mydom,dc=l ocal")

              Dim oSearcher As DirectorySearch er = New DirectorySearch er(entry)

              Dim strLogOnHours As String = ""

              Dim arrLogonHoursBy tes(20) As Long

              Dim arrLogonHoursBi ts(167) As Long

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

              Dim i As Integer

              oSearcher.Filte r = "(&(objectClass =user) (sAMAccountName =" & strUser
              &
              "))"

              oSearcher.Prope rtiesToLoad.Add ("logonHours ")

              Try

              Dim oResult As SearchResult

              oResult = oSearcher.FindO ne

              If Not
              oResult.GetDire ctoryEntry().Pr operties("logon Hours").Value.T oString
              =
              "" Then

              For i = 1 To
              Len(oResult.Get DirectoryEntry( ).Properties("l ogonHours").Val ue.ToString)

              arrLogonHoursBy tes(i-1) =
              Asc(Mid(oResult .GetDirectoryEn try().Propertie s("logonHours") .Value.ToString ,
              i, 1))

              Next

              Dim intCounter As Integer = 0

              Dim intLoopCounter As Integer = 0


              For Each LogonHourByte As Object In arrLogonHoursBy tes

              Dim arrLogonHourBit s() = GetLogonHourBit s(LogonHourByte )

              If intCounter = 0 Then

              strLogOnHours += arrDayOfWeek(in tLoopCounter) & " "

              intLoopCounter += 1

              End If


              For Each LogonHourBit As Integer In arrLogonHourBit s

              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", MessageBoxButto ns.OK,
              MessageBoxIcon. Error)

              Get_LogOnHours = Nothing

              Finally

              Comment

              Working...