segunda-feira, 4 de abril de 2011

Registrando data e hora logon e logoff do usuário com script Vbscript

Option Explicit

Dim objFSO, objLogFile, objNetwork, objShell, strText, intAns, strComputer, objWMIService
Dim intConstants, intTimeout, strTitle, intCount, blnLog, IPconfigSet, Ipconfig, IP
Dim strUserName, strComputerName, strIP, strShare, strLogFile
Dim fso, sf, StrNew
Const ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")

strComputer = "."
strShare = "\\SeuServidor\SuaPastaCompartilhada\"
strLogFile = "logonoff.txt"
intTimeout = 20

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")
Set objShell = CreateObject("Wscript.Shell")

Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
strUserName = objNetwork.UserName
strComputerName = objNetwork.ComputerName

'For Each IPConfig in IPConfigSet
' If Not IsNull(IPConfig.IPAddress) Then
' For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
' IP = IPConfig.IPAddress(i)
' Next
' End If
'Next

If objFSO.FolderExists(strShare) Then
On Error Resume Next
Set objLogFile = objFSO.OpenTextFile(strShare & "\" & strLogFile, 8, True, 0)
If Err.Number = 0 Then
intCount = 1
blnLog = False
Do Until intCount = 3
objLogFile.WriteLine "Logon " & Now & " " & strComputerName & " " & strUserName '& " com o ip " & IP'
If Err.Number = 0 Then
intCount = 3
blnLog = True
Else
Err.Clear
intCount = intCount + 1
If Wscript.Version > 5 Then
Wscript.Sleep 200
End If
End If
Loop
' On Error GoTo 0
End If
End If

Dim fa, fb, f, s, OpSysSet, i

Set objFSO = Nothing
Set objNetwork = Nothing
Set objShell = Nothing

Wscript.Quit


Para capturar o logon salve o código acima com o nome logon.vbs e para logoff salve como logoff.vbs

Na máquina local clica no menu: iniciar > executar: Em seguida digite:
gpedit.msc
Logo após vá em configurações de usuário e selecione Script de Logon/Logoff
Ao lado direito irá aparecer dois campos Logon/Logoff clica com o botao direito vá em propriedades, em seguida selecione o script na pasta de Logon e Logon.
Onde SeuServidor\SuaPastaCompartilhada deve ser substituido por uma pasta na rede compartilhada ou local.
Obs: Altere a linha 44 referente A mensagem de gravação
para Logoff.

Um comentário:

  1. Bom dia Amigo!
    Gostaria de saber como poderia acrescentar uma variável para buscar a OU a qual pertence o usuário ue fez logon e logoff

    ResponderExcluir

Já aconteceu de você clicar no meu (Iniciar/Desligar) e o computador não ser desligado, pelo fato de o Windows ficar perguntando se você des...