<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; VBS: Утилита Cconnect для ограничения одновременной регистрации]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=5309</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=5309&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «VBS: Утилита Cconnect для ограничения одновременной регистрации».]]></description>
		<lastBuildDate>Mon, 27 Dec 2010 10:52:31 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[VBS: Утилита Cconnect для ограничения одновременной регистрации]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=43088#p43088</link>
			<description><![CDATA[<p>Добрый день.<br />Пожалуйста помагите разобратся со скриптом.<br />Принцип работы скрипа следуешее. На заранее устанвленем сервере SQL&nbsp; сервере должен зарегистрироваться имя компютера и име текушего пользователя кто залогенулся.<br />В принципе все работает нормально, но если компютер по каким то причинам отключился не успев выполнить скрипт, то в SQL –е остается запись, и не пускает узеру логинутся,&nbsp; приходится самому удалять запись из SQL в ручную. <br />Как можно дописать скрипт чтобы не было проблем после Dirty Shutdown –на.</p><div class="codebox"><pre><code>Set objNet = WScript.CreateObject(&quot;WScript.Network&quot;)
Set WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;)
Set WshSysEnv = WshShell.Environment(&quot;PROCESS&quot;)
Set locator = CreateObject(&quot;WbemScripting.SWbemLocator&quot;)
CONST CONST_FORCE_LOGOFF = 4


&#039; Gather ConCurrent Connection Information
 On error resume next
    ConConnection = WshShell.RegRead(&quot;HKCU\Software\Microsoft\CConnect\CConnection&quot;)
        if err then
              ConConnection = 99999
         End if

&#039; Gather EventServer Name
 On error resume next
   evntserver = WshShell.RegRead(&quot;HKCU\Software\Microsoft\CConnect\EvntServer&quot;)
        if err then
              evntServer = &quot;&quot;
       End if

&#039; Gather SQL Credentials from the reginstry
 On error resume next
  strSQLServerName = WshShell.RegRead(&quot;HKCU\Software\Microsoft\CConnect\SQLSERVER&quot;) &#039;If this fails, they will be prompted for the SQL connection info
  strUserName = WshShell.RegRead(&quot;HKCU\Software\Microsoft\CConnect\SQLUSER&quot;)
  strPassword = WshShell.RegRead(&quot;HKCU\Software\Microsoft\CConnect\SQLPASSWORD&quot;)
        if err then
              WshShell.LogEvent 1, &quot;CConnect Failed to Start for user &quot; &amp; objNet.UserName &amp; &quot; due to &quot; &amp; Err.Description, EvntServer
          wscript.quit
       End if

&#039;Start SQL Connection
On error resume next
 Set objDBConnection = WScript.CreateObject(&quot;ADODB.Connection&quot;)
  Set RS = WScript.CreateObject(&quot;ADODB.Recordset&quot;)
  objDBConnection.open &quot;DRIVER=SQL Server;UID=&quot; + strUserName + &quot;;Password=&quot; + strPassword + &quot;;SERVER=&quot; +strSQLServerName

       if err then
              WshShell.LogEvent 1, &quot;CConnect Failed to Start for user &quot; &amp; objNet.UserName &amp; &quot; due to &quot; &amp; Err.Description, EvntServer
          wscript.quit
       End if

&#039; Get Current User Count
 NumberOfConnections = GetUserCount()

&#039; Determine if the current Computer is matches the one currently in the database
  If CurrentComputer() = True Then
       WshShell.LogEvent 2, &quot;User &quot; &amp; objNet.UserName &amp; &quot; 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&quot;, EvntServer

  End If

&#039; Check if the User Should be logged off
      If NumberOfConnections &gt;= ConConnection Then
           WshShell.LogEvent 0, &quot;User &quot; &amp; objNet.UserName &amp; &quot; has reached thier concurrent connection limitation. The ConCurrent Connection number is set to &quot; &amp; NumberOfConnections, EvntServer
           WshShell.LogEvent 0, &quot;User &quot; &amp; objNet.UserName &amp; &quot; has been successfully logged off.&quot;, evntserver

&#039; Log the User Off
       For Each os In GetObject(&quot;winmgmts:{impersonationLevel=impersonate,(shutdown,remoteshutdown)}!//&quot; + objNet.ComputerName).InstancesOf(&quot;Win32_OperatingSystem&quot;)
        retCode = os.Win32Shutdown(4, 0)
       Next
        Wscript.quit
     End If


    call AddUsers()

   WshShell.LogEvent 0, &quot;CConnect.vbs successfully ran for user &quot; &amp; objNet.UserName &amp; &quot; on computer &quot; &amp; objNet.computername, EvntServer

&#039;******************************************************************
&#039;
&#039; This is The Function to Add The User information to the Database
&#039;
&#039;*******************************************************************

Public Function AddUsers()
on error Resume Next
   SQLQuery1 = &quot;&quot;
   SQLQuery1 = &quot; if not exists (select * from SYSIAD where&quot;
   SQLQuery1 = SQLQuery1 + &quot; username=&#039;&quot; &amp; objNet.UserName &amp; &quot;&#039; and computername=&#039;&quot; &amp; objNet.Computername &amp; &quot;&#039;)&quot;
   SQLQuery1 = SQLQuery1 + &quot; Insert SYSIAD values(&#039;&quot; &amp; objNet.UserName &amp; &quot;&#039;,&quot;
   SQLQuery1 = SQLQuery1 + &quot; &#039;&quot; &amp; objNet.Computername &amp; &quot;&#039;, &#039;&quot; &amp; WshSysEnv(&quot;LOGONSERVER&quot;) &amp; &quot;&#039;)&quot;
       Set RSSYSIAD = objDBConnection.Execute(SQLQuery1)

             if err then
                  WshShell.LogEvent 1, &quot;CConnect Failed to Start for user &quot; &amp; objNet.UserName &amp; &quot; due to &quot; &amp; Err.Description, EvntServer
             wscript.quit
              End if
End Function


&#039;*****************************************************************************************
&#039;
&#039; This is The Function to Get the Current Number of Computers that the user is logged into
&#039;
&#039;*******************************************************************************************

 Public Function GetUserCount()
On error Resume Next
      SQLQuery1 = &quot;&quot;
      SQLQuery1 = SQLQuery1 + &quot; select * from SYSIAD&quot;
      SQLQuery1 = SQLQuery1 + &quot; where username=&#039;&quot; &amp; objNet.UserName &amp; &quot;&#039;&quot;

       Set RSSYSIAD = objDBConnection.Execute(SQLQuery1)
             if err then
                  WshShell.LogEvent 1, &quot;CConnect Failed to Start for user &quot; &amp; objNet.UserName &amp; &quot; due to &quot; &amp; Err.Description, EvntServer
            wscript.quit
             End if

        recAffected = 0
        Do While Not RSSYSIAD.EOF
             RSSYSIAD.MoveNext
            recAffected = recAffected + 1
        Loop
      GetUserCount = recAffected

 End Function

&#039;*****************************************************************************************
&#039;
&#039; This is The Function to Get the Current Computer of the user and see if it matches
&#039;        The one in the database
&#039;
&#039;*******************************************************************************************

 Public Function CurrentComputer()
On Error Resume Next
      SQLQuery1 = &quot;&quot;
      SQLQuery1 = SQLQuery1 &amp; &quot; select * from SYSIAD&quot;
      SQLQuery1 = SQLQuery1 &amp; &quot; where ComputerName=&#039;&quot; &amp; objNet.ComputerName &amp; &quot;&#039;&quot;
       Set RSSYSIAD = objDBConnection.Execute(SQLQuery1)
             if err then
                  WshShell.LogEvent 1, &quot;CConnect Failed to Start for user &quot; &amp; objNet.UserName &amp; &quot; due to &quot; &amp; Err.Description, EvntServer
            wscript.quit
             End if

       Do While Not RSSYSIAD.EOF
        CComp = RSSYSIAD.Fields(1).Value
         RSSYSIAD.MoveNext
         Loop
        
    If CComp = &quot;&quot; Then
       CurrentComputer = False
       Exit Function
    End If

   If objNet.ComputerName = Trim(CComp) Then
 
       CurrentComputer = True

     Else

       CurrentComputer = False
 
   End If
 End Function</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Армен)]]></author>
			<pubDate>Mon, 27 Dec 2010 10:52:31 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=43088#p43088</guid>
		</item>
	</channel>
</rss>
