none
Códigos para complementar o PCeX - 09/07/2007 RRS feed

  • Pergunta

  • Amigos, estou na corrida contra o tempo para soltar a versão mais atual e compatível do PCeX com o Windows Vista.

     

    Infelizmente ainda estou readaptando algumas coisas de forma que as informações sejam fornecidas com o melhor nível de acuricidade em todos os sistemas derivados do NT e x64 e em especial o Vista que tem me dado algumas dores de cabeça.

     

    Preciso de algumas informações e acho que o pessoal aqui vai me ajudar bastante.

     

    Antes para conseguirmos o último usuário que realizou logon na estação, precisávamos recuperar o valor de uma chave em "Software\Microsoft\Windows NT\CurrentVersion\WinLogon".

    No Vista esta chave não existe, então consegui criar este código abaixo:

    ------------------------------------

    strComputer = "."
    Set LoginProfiles = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2").InstancesOf ("Win32_NetworkLoginProfile")
    for each Profile in LoginProfiles
     If Not Isnull(Profile.LastLogon) Then
       WScript.Echo Profile.Name
       WScript.Echo Profile.LastLogon
     End If
    next

    -------------------------------------

    Porém, este código me traz todos os últimos usuários em todos os profiles, sendo que preciso apenas do último logo efetuado na estação.

     

    Outra dor de cabeça que estou tendo e que não estou conseguindo localizar no Technet é quanto a listar em ordem alfabética, crescente ou decrescente ítens retornados através do WMI ou ítens retornados do registro.

     

    Podem me ajudar? Obrigado!

    segunda-feira, 9 de julho de 2007 17:40

Respostas

  • Achei esse para AD (com relação a ordenar por ordem alfabetica)...vc só vai ter que dar uma garimpada...para aproveitar ocód que vc quer.
    fonte:
    http://techtasks.com/code/viewbookcode/210


    Code Snippet

    ' From the book "Active Directory, Third Edition"
    ' ISBN: 0-596-10173-2

    '**********************************************************************
    'The ShowUsers.vbs Utility
    '**********************************************************************
    Option Explicit

    On Error Resume Next

    '**********************************************************************
    'Maximizes the Notepad screen when started
    '**********************************************************************
    Const vbMaximizedFocus = 3

    '**********************************************************************
    'Sets the location of the temporary file
    '**********************************************************************
    Const TEMPFILE = "C:\SHOWUSERS-TEMP.TXT"

    '**********************************************************************
    'Opens a file and lets you start writing from the beginning of the
    'file
    '**********************************************************************
    Const ForWriting = 2

    '**********************************************************************
    'Declare all variables. As arrResults will be continually increased
    'in size as more results are fed in, you have to initially declare it
    'as an unbounded array
    '**********************************************************************
    Dim objShell, objFSO, objSession, arrServerResults(1,1), arrResults()
    Dim arrResults2(), fso, ts, intRC, intMaxSessions, intIndex, strItem

    '**********************************************************************
    'Sets up multidimensional array to hold server names and user counts
    '**********************************************************************
    arrServerResults(0,0) = "server1"
    arrServerResults(0,1) = 0
    arrServerResults(1,0) = "server2"
    arrServerResults(1,1) = 0

    '**********************************************************************
    'Redimensions arrResults to one element to start with
    '**********************************************************************
    ReDim arrResults(0)

    '**********************************************************************
    'Iterates through the array, connecting to the server service of
    'each server and looks at each session on that server
    '
    'If the session has an empty user (is an interserver connection) or
    'the user is a computer (the trailing character is a dollar sign), the
    'script ignores that session and proceeds to the next session
    '
    'If the session is valid, the script adds the username to the last
    'element of the arrResults array and expands the array by one element
    'to cope with the next result when it arrives. The script also
    'increments the session count for the corresponding server by one
    '**********************************************************************
    For intIndex = 0 To UBound(arrServerResults)
    Set objFSO = GetObject("WinNT://" & arrServerResults(intIndex,0) _
    & "/LanmanServer")

    For Each objSession In objFSO.Sessions
    If (Not objSession.User = "") And (Not Right(objSession.User,1) = "$") Then
    arrResults(UBound(arrResults)) = objSession.User & vbCrLf
    ReDim Preserve arrResults(UBound(arrResults) + 1)
    ArrServerResults(intIndex,1) = arrServerResults(intIndex,1) + 1
    End If
    Next

    Set objFSO = Nothing
    Next

    '**********************************************************************
    'Sorts the entire arrResults array and then removes duplicates from
    'it, placing the results in arrResults2
    '**********************************************************************
    Quicksort arrResults, 0, UBound(arrResults)
    RemoveDuplicates arrResults, 0, UBound(arrResults), arrResults2

    '**********************************************************************
    'Opens the temporary text file for writing. If the text file already
    'exists, overwrite it.
    '**********************************************************************
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile(TEMPFILE, ForWriting, True)

    '**********************************************************************
    'Counts the max sessions by iterating through each server and adding
    'up the sessions count in the second column of each row of the
    'multidimensional array
    '
    'Writes out the user sessions for each server to the temporary file
    'as the script iterates through the list. When the script finishes
    'counting, it writes out the max sessions to the file as well.
    '**********************************************************************
    intMaxSessions = 0
    For intIndex = 0 To UBound(arrServerResults)
    ts.WriteLine "Total User Sessions on " & arrServerResults(intIndex,0) _
    & ": " & arrServerResults(intIndex,1)
    intMaxSessions = intMaxSessions + arrServerResults(intIndex,1)
    Next
    ts.WriteLine "Total User sessions on CFS: " & intMaxSessions
    ts.WriteLine

    '**********************************************************************
    'Writes out the total number of unique users connected to the domain,
    'followed by each username in alphabetic order
    '**********************************************************************
    ts.WriteLine "Total Users on CFS: " & UBound(arrResults2)
    ts.WriteLine
    For Each strItem in arrResults2
    ts.Write strItem
    Next
    ts.Close

    '**********************************************************************
    'Sets the third parameter of the Shell::Run method to TRUE, which
    'allows the script to open up the file in Notepad and maximize the
    'screen. The script stops executing until you close Notepad, which
    'places a return code into intRC. When Notepad is closed, the script
    'deletes the file.
    '**********************************************************************
    Set objShell = CreateObject("WScript.Shell")
    intRC = objShell.Run ("notepad.exe " & TEMPFILE, vbMaximizedFocus, TRUE)
    fso.DeleteFile(TEMPFILE)

    '**********************************************************************
    'Subroutine Quicksort
    '
    'Sorts the items in the array (between the two values you pass in)
    '**********************************************************************
    Sub Quicksort(strValues(), ByVal min, ByVal max)

    Dim strMediumValue, high, low, i

    '**********************************************************************
    'If the list has only 1 item, it's sorted
    '**********************************************************************
    If min >= max Then Exit Sub

    '**********************************************************************
    'Pick a dividing item randomly
    '**********************************************************************
    we = min + Int(Rnd(max - min + 1))
    strMediumValue = strValues(i)

    '**********************************************************************
    'Swap the dividing item to the front of the list
    '**********************************************************************
    strValues(i) = strValues(min)

    '**********************************************************************
    'Separate the list into sublists
    '**********************************************************************
    low = min
    high = max
    Do
    '**********************************************************************
    'Look down from high for a value < strMediumValue
    '**********************************************************************
    Do While strValues(high) >= strMediumValue
    high = high - 1
    If high <= low Then Exit Do
    Loop

    If high <= low Then
    '**********************************************************************
    'The list is separated
    '**********************************************************************
    strValues(low) = strMediumValue
    Exit Do
    End If

    '**********************************************************************
    'Swap the low and high strValues
    '**********************************************************************
    strValues(low) = strValues(high)

    '**********************************************************************
    'Look up from low for a value >= strMediumValue
    '**********************************************************************
    low = low + 1
    Do While strValues(low) < strMediumValue
    low = low + 1
    If low >= high Then Exit Do
    Loop

    If low >= high Then
    '**********************************************************************
    'The list is separated
    '**********************************************************************
    low = high
    strValues(high) = strMediumValue
    Exit Do
    End If

    '**********************************************************************
    'Swap the low and high strValues
    '**********************************************************************
    strValues(high) = strValues(low)
    Loop 'Loop until the list is separated.

    '**********************************************************************
    'Recursively sort the sublists
    '**********************************************************************
    Quicksort strValues, min, low - 1
    Quicksort strValues, low + 1, max

    End Sub

    '**********************************************************************
    'Subroutine RemoveDuplicates
    '
    'Removes duplicate items in the strValues array (between the two
    'values you pass in) and writes the results to strNewValues()
    '**********************************************************************
    Sub RemoveDuplicates(ByVal strValues(), ByVal min, ByVal max, strNewValues() )

    Dim strValuesIndex, strNewValuesIndex

    ReDim strNewValues(0)
    strNewValuesIndex = 0

    For strValuesIndex = min To max-1
    If Not strValues(strValuesIndex) = strValues(strValuesIndex+1) Then
    strNewValues(strNewValuesIndex) = strValues(strValuesIndex)
    ReDim Preserve strNewValues(strNewValuesIndex + 1)
    strNewValuesIndex = strNewValuesIndex + 1
    End If
    Next
    strNewValues(strNewValuesIndex) = strValues(max)

    End Sub

    abraço
    'Boa sorte
    quarta-feira, 11 de julho de 2007 01:03

Todas as Respostas

  • Arthur ... pq vc não cria essa informação com flags (chave de registro ou arquivos).
    Eu uso arquivos no meu trabalho.
    Veja se te ajuda.'Inventario basico da Estação de Trabalho
    'Romeu Malacarne Avancini - rmavancini@gmail.com

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")

    Set colSettings = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
    Set colBIOS = objWMIService.ExecQuery ("Select * from Win32_BIOS")
    Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
    Set colAdapters = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

    Set oNetwork = CreateObject("WScript.Network")


    Dim fso, sf, StrNew, strDay, strMonth, srtYear,strTime, UNC, strComputer, strUser, strManufac, strModel, strSerial, strBVersion
    Dim strMemo, strMac, strIp, strDiskToal, strDiskFree
    'aqui você coloca o caminho do compartilhamento.
    UNC = "c:\temp\"
    strComputer = oNetwork.ComputerName
    struser = oNetwork.UserName
    systime = Now()
    srtDay = cstr(day(systime))
    strMonth = cstr(month(systime))
    strYear = cstr(year(systime))
    strTime = cstr(Time())
    Const ForAppending = 8

    For Each objComputer in colSettings
        strManufac = objComputer.Manufacturer
        strModel = objComputer.Model
        strMemo = objComputer.TotalPhysicalMemory/1024000
    Next

    For each objBIOS in colBIOS
        strSerial = objBIOS.SerialNumber
        strBVersion = objBIOS.Version
    Next
    n = 1
    For each objNetwork in colAdapters
        strMac = objNetwork.MACAddress
         
     
       If Not IsNull(objNetwork.IPAddress) Then
          For i = 0 To UBound(objNetwork.IPAddress)
          strIp = objNetwork.IPAddress(i)
          Next
       End If
     
       n = n + 1
    Next   

    For each objDisk in colDisks
         strDiskTotal = objDisk.Size/1024
         strDiskFree = objDisk.FreeSpace/1024
    Next

    Set fso = CreateObject("Scripting.FileSystemObject")

    StrNew = UNC & strComputer &".log"

    set sf=fso.OpenTextFile (StrNew, ForAppending, True)
    sf.writeline "Date ="& srtDay &"-"& strMonth &"-"& strYear &"; Time ="& strTime &"; UserID ="& strUser & _
    "; Manufacturer =" & strManufac & "; Model ="& strModel &"; Serial Number ="& strSerial & "; BIOS Version =:"& strBVersion & _
    "; Memoria Fisica Total ="& strMemo &"; MAC ADDRESS ="& strMac & "; IP ADDRESS ="& strIp & _
    "; Disk Size ="& strDiskTotal &"; Free Space ="& strDiskFree

    '-------------------------------------------------------------------------------------

    Com relação a ordenar , talves se vc criar um array com o alfabeto e colocasse ele dentro de um while fazendo a comparação com as primerias letras.
    vou tentar fazer algo aki tbm...
    abraço e boa sorte.
    quarta-feira, 11 de julho de 2007 00:59
  • Achei esse para AD (com relação a ordenar por ordem alfabetica)...vc só vai ter que dar uma garimpada...para aproveitar ocód que vc quer.
    fonte:
    http://techtasks.com/code/viewbookcode/210


    Code Snippet

    ' From the book "Active Directory, Third Edition"
    ' ISBN: 0-596-10173-2

    '**********************************************************************
    'The ShowUsers.vbs Utility
    '**********************************************************************
    Option Explicit

    On Error Resume Next

    '**********************************************************************
    'Maximizes the Notepad screen when started
    '**********************************************************************
    Const vbMaximizedFocus = 3

    '**********************************************************************
    'Sets the location of the temporary file
    '**********************************************************************
    Const TEMPFILE = "C:\SHOWUSERS-TEMP.TXT"

    '**********************************************************************
    'Opens a file and lets you start writing from the beginning of the
    'file
    '**********************************************************************
    Const ForWriting = 2

    '**********************************************************************
    'Declare all variables. As arrResults will be continually increased
    'in size as more results are fed in, you have to initially declare it
    'as an unbounded array
    '**********************************************************************
    Dim objShell, objFSO, objSession, arrServerResults(1,1), arrResults()
    Dim arrResults2(), fso, ts, intRC, intMaxSessions, intIndex, strItem

    '**********************************************************************
    'Sets up multidimensional array to hold server names and user counts
    '**********************************************************************
    arrServerResults(0,0) = "server1"
    arrServerResults(0,1) = 0
    arrServerResults(1,0) = "server2"
    arrServerResults(1,1) = 0

    '**********************************************************************
    'Redimensions arrResults to one element to start with
    '**********************************************************************
    ReDim arrResults(0)

    '**********************************************************************
    'Iterates through the array, connecting to the server service of
    'each server and looks at each session on that server
    '
    'If the session has an empty user (is an interserver connection) or
    'the user is a computer (the trailing character is a dollar sign), the
    'script ignores that session and proceeds to the next session
    '
    'If the session is valid, the script adds the username to the last
    'element of the arrResults array and expands the array by one element
    'to cope with the next result when it arrives. The script also
    'increments the session count for the corresponding server by one
    '**********************************************************************
    For intIndex = 0 To UBound(arrServerResults)
    Set objFSO = GetObject("WinNT://" & arrServerResults(intIndex,0) _
    & "/LanmanServer")

    For Each objSession In objFSO.Sessions
    If (Not objSession.User = "") And (Not Right(objSession.User,1) = "$") Then
    arrResults(UBound(arrResults)) = objSession.User & vbCrLf
    ReDim Preserve arrResults(UBound(arrResults) + 1)
    ArrServerResults(intIndex,1) = arrServerResults(intIndex,1) + 1
    End If
    Next

    Set objFSO = Nothing
    Next

    '**********************************************************************
    'Sorts the entire arrResults array and then removes duplicates from
    'it, placing the results in arrResults2
    '**********************************************************************
    Quicksort arrResults, 0, UBound(arrResults)
    RemoveDuplicates arrResults, 0, UBound(arrResults), arrResults2

    '**********************************************************************
    'Opens the temporary text file for writing. If the text file already
    'exists, overwrite it.
    '**********************************************************************
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile(TEMPFILE, ForWriting, True)

    '**********************************************************************
    'Counts the max sessions by iterating through each server and adding
    'up the sessions count in the second column of each row of the
    'multidimensional array
    '
    'Writes out the user sessions for each server to the temporary file
    'as the script iterates through the list. When the script finishes
    'counting, it writes out the max sessions to the file as well.
    '**********************************************************************
    intMaxSessions = 0
    For intIndex = 0 To UBound(arrServerResults)
    ts.WriteLine "Total User Sessions on " & arrServerResults(intIndex,0) _
    & ": " & arrServerResults(intIndex,1)
    intMaxSessions = intMaxSessions + arrServerResults(intIndex,1)
    Next
    ts.WriteLine "Total User sessions on CFS: " & intMaxSessions
    ts.WriteLine

    '**********************************************************************
    'Writes out the total number of unique users connected to the domain,
    'followed by each username in alphabetic order
    '**********************************************************************
    ts.WriteLine "Total Users on CFS: " & UBound(arrResults2)
    ts.WriteLine
    For Each strItem in arrResults2
    ts.Write strItem
    Next
    ts.Close

    '**********************************************************************
    'Sets the third parameter of the Shell::Run method to TRUE, which
    'allows the script to open up the file in Notepad and maximize the
    'screen. The script stops executing until you close Notepad, which
    'places a return code into intRC. When Notepad is closed, the script
    'deletes the file.
    '**********************************************************************
    Set objShell = CreateObject("WScript.Shell")
    intRC = objShell.Run ("notepad.exe " & TEMPFILE, vbMaximizedFocus, TRUE)
    fso.DeleteFile(TEMPFILE)

    '**********************************************************************
    'Subroutine Quicksort
    '
    'Sorts the items in the array (between the two values you pass in)
    '**********************************************************************
    Sub Quicksort(strValues(), ByVal min, ByVal max)

    Dim strMediumValue, high, low, i

    '**********************************************************************
    'If the list has only 1 item, it's sorted
    '**********************************************************************
    If min >= max Then Exit Sub

    '**********************************************************************
    'Pick a dividing item randomly
    '**********************************************************************
    we = min + Int(Rnd(max - min + 1))
    strMediumValue = strValues(i)

    '**********************************************************************
    'Swap the dividing item to the front of the list
    '**********************************************************************
    strValues(i) = strValues(min)

    '**********************************************************************
    'Separate the list into sublists
    '**********************************************************************
    low = min
    high = max
    Do
    '**********************************************************************
    'Look down from high for a value < strMediumValue
    '**********************************************************************
    Do While strValues(high) >= strMediumValue
    high = high - 1
    If high <= low Then Exit Do
    Loop

    If high <= low Then
    '**********************************************************************
    'The list is separated
    '**********************************************************************
    strValues(low) = strMediumValue
    Exit Do
    End If

    '**********************************************************************
    'Swap the low and high strValues
    '**********************************************************************
    strValues(low) = strValues(high)

    '**********************************************************************
    'Look up from low for a value >= strMediumValue
    '**********************************************************************
    low = low + 1
    Do While strValues(low) < strMediumValue
    low = low + 1
    If low >= high Then Exit Do
    Loop

    If low >= high Then
    '**********************************************************************
    'The list is separated
    '**********************************************************************
    low = high
    strValues(high) = strMediumValue
    Exit Do
    End If

    '**********************************************************************
    'Swap the low and high strValues
    '**********************************************************************
    strValues(high) = strValues(low)
    Loop 'Loop until the list is separated.

    '**********************************************************************
    'Recursively sort the sublists
    '**********************************************************************
    Quicksort strValues, min, low - 1
    Quicksort strValues, low + 1, max

    End Sub

    '**********************************************************************
    'Subroutine RemoveDuplicates
    '
    'Removes duplicate items in the strValues array (between the two
    'values you pass in) and writes the results to strNewValues()
    '**********************************************************************
    Sub RemoveDuplicates(ByVal strValues(), ByVal min, ByVal max, strNewValues() )

    Dim strValuesIndex, strNewValuesIndex

    ReDim strNewValues(0)
    strNewValuesIndex = 0

    For strValuesIndex = min To max-1
    If Not strValues(strValuesIndex) = strValues(strValuesIndex+1) Then
    strNewValues(strNewValuesIndex) = strValues(strValuesIndex)
    ReDim Preserve strNewValues(strNewValuesIndex + 1)
    strNewValuesIndex = strNewValuesIndex + 1
    End If
    Next
    strNewValues(strNewValuesIndex) = strValues(max)

    End Sub

    abraço
    'Boa sorte
    quarta-feira, 11 de julho de 2007 01:03