Тема: VBS: Утилита Cconnect для ограничения одновременной регистрации
Добрый день.
Пожалуйста помагите разобратся со скриптом.
Принцип работы скрипа следуешее. На заранее устанвленем сервере SQL сервере должен зарегистрироваться имя компютера и име текушего пользователя кто залогенулся.
В принципе все работает нормально, но если компютер по каким то причинам отключился не успев выполнить скрипт, то в SQL –е остается запись, и не пускает узеру логинутся, приходится самому удалять запись из SQL в ручную.
Как можно дописать скрипт чтобы не было проблем после Dirty Shutdown –на.
Set objNet = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PROCESS")
Set locator = CreateObject("WbemScripting.SWbemLocator")
CONST CONST_FORCE_LOGOFF = 4
' Gather ConCurrent Connection Information
On error resume next
ConConnection = WshShell.RegRead("HKCU\Software\Microsoft\CConnect\CConnection")
if err then
ConConnection = 99999
End if
' Gather EventServer Name
On error resume next
evntserver = WshShell.RegRead("HKCU\Software\Microsoft\CConnect\EvntServer")
if err then
evntServer = ""
End if
' Gather SQL Credentials from the reginstry
On error resume next
strSQLServerName = WshShell.RegRead("HKCU\Software\Microsoft\CConnect\SQLSERVER") 'If this fails, they will be prompted for the SQL connection info
strUserName = WshShell.RegRead("HKCU\Software\Microsoft\CConnect\SQLUSER")
strPassword = WshShell.RegRead("HKCU\Software\Microsoft\CConnect\SQLPASSWORD")
if err then
WshShell.LogEvent 1, "CConnect Failed to Start for user " & objNet.UserName & " due to " & Err.Description, EvntServer
wscript.quit
End if
'Start SQL Connection
On error resume next
Set objDBConnection = WScript.CreateObject("ADODB.Connection")
Set RS = WScript.CreateObject("ADODB.Recordset")
objDBConnection.open "DRIVER=SQL Server;UID=" + strUserName + ";Password=" + strPassword + ";SERVER=" +strSQLServerName
if err then
WshShell.LogEvent 1, "CConnect Failed to Start for user " & objNet.UserName & " due to " & Err.Description, EvntServer
wscript.quit
End if
' Get Current User Count
NumberOfConnections = GetUserCount()
' Determine if the current Computer is matches the one currently in the database
If CurrentComputer() = True Then
WshShell.LogEvent 2, "User " & objNet.UserName & " has logged into a computer whose previous information existed in the SQL Server. This can only happen if the UserName and ComputerName matched the objects in the SQL Server", EvntServer
End If
' Check if the User Should be logged off
If NumberOfConnections >= ConConnection Then
WshShell.LogEvent 0, "User " & objNet.UserName & " has reached thier concurrent connection limitation. The ConCurrent Connection number is set to " & NumberOfConnections, EvntServer
WshShell.LogEvent 0, "User " & objNet.UserName & " has been successfully logged off.", evntserver
' Log the User Off
For Each os In GetObject("winmgmts:{impersonationLevel=impersonate,(shutdown,remoteshutdown)}!//" + objNet.ComputerName).InstancesOf("Win32_OperatingSystem")
retCode = os.Win32Shutdown(4, 0)
Next
Wscript.quit
End If
call AddUsers()
WshShell.LogEvent 0, "CConnect.vbs successfully ran for user " & objNet.UserName & " on computer " & objNet.computername, EvntServer
'******************************************************************
'
' This is The Function to Add The User information to the Database
'
'*******************************************************************
Public Function AddUsers()
on error Resume Next
SQLQuery1 = ""
SQLQuery1 = " if not exists (select * from SYSIAD where"
SQLQuery1 = SQLQuery1 + " username='" & objNet.UserName & "' and computername='" & objNet.Computername & "')"
SQLQuery1 = SQLQuery1 + " Insert SYSIAD values('" & objNet.UserName & "',"
SQLQuery1 = SQLQuery1 + " '" & objNet.Computername & "', '" & WshSysEnv("LOGONSERVER") & "')"
Set RSSYSIAD = objDBConnection.Execute(SQLQuery1)
if err then
WshShell.LogEvent 1, "CConnect Failed to Start for user " & objNet.UserName & " due to " & Err.Description, EvntServer
wscript.quit
End if
End Function
'*****************************************************************************************
'
' This is The Function to Get the Current Number of Computers that the user is logged into
'
'*******************************************************************************************
Public Function GetUserCount()
On error Resume Next
SQLQuery1 = ""
SQLQuery1 = SQLQuery1 + " select * from SYSIAD"
SQLQuery1 = SQLQuery1 + " where username='" & objNet.UserName & "'"
Set RSSYSIAD = objDBConnection.Execute(SQLQuery1)
if err then
WshShell.LogEvent 1, "CConnect Failed to Start for user " & objNet.UserName & " due to " & Err.Description, EvntServer
wscript.quit
End if
recAffected = 0
Do While Not RSSYSIAD.EOF
RSSYSIAD.MoveNext
recAffected = recAffected + 1
Loop
GetUserCount = recAffected
End Function
'*****************************************************************************************
'
' This is The Function to Get the Current Computer of the user and see if it matches
' The one in the database
'
'*******************************************************************************************
Public Function CurrentComputer()
On Error Resume Next
SQLQuery1 = ""
SQLQuery1 = SQLQuery1 & " select * from SYSIAD"
SQLQuery1 = SQLQuery1 & " where ComputerName='" & objNet.ComputerName & "'"
Set RSSYSIAD = objDBConnection.Execute(SQLQuery1)
if err then
WshShell.LogEvent 1, "CConnect Failed to Start for user " & objNet.UserName & " due to " & Err.Description, EvntServer
wscript.quit
End if
Do While Not RSSYSIAD.EOF
CComp = RSSYSIAD.Fields(1).Value
RSSYSIAD.MoveNext
Loop
If CComp = "" Then
CurrentComputer = False
Exit Function
End If
If objNet.ComputerName = Trim(CComp) Then
CurrentComputer = True
Else
CurrentComputer = False
End If
End Function
