<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; VBS/WMI: Многопоточный WshController с ограничением длины очереди]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=5682</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=5682&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «VBS/WMI: Многопоточный WshController с ограничением длины очереди».]]></description>
		<lastBuildDate>Fri, 01 Apr 2011 11:05:03 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[VBS/WMI: Многопоточный WshController с ограничением длины очереди]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=47266#p47266</link>
			<description><![CDATA[<p>Данный скрипт позволяет одновременно выполнять скрипты на удалённых хостах, скрипт модифицирован и количество потоков ограничено очередью, что придаёт стабильность работе скрипта. Прошу любить и жаловать...</p><div class="codebox"><pre><code>SN=&quot;SomeScript.vbs&quot;

Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
SNParentFolderName = fso.GetParentFolderName(Wscript.ScriptFullName) 
SNDestFilePath = fso.BuildPath(SNParentFolderName,SN) 
LF=&quot;result.log&quot;


If Wscript.Arguments.Count=0 Then  &#039; признак основного потока

arrComputers = Array(&quot;host02&quot;,&quot;host03&quot;,&quot;host04&quot;,&quot;host06&quot;,&quot;host07&quot;,&quot;host08&quot;,&quot;host10&quot;,&quot;host13&quot;,&quot;host14&quot;,&quot;host15&quot;,&quot;host16&quot;,&quot;host17&quot;,&quot;host18&quot;,&quot;host20&quot;,&quot;host21&quot;,&quot;host22&quot;,&quot;host23&quot;,&quot;host24&quot;,&quot;host25&quot;,&quot;host26&quot;,&quot;host27&quot;,&quot;host29&quot;)


Set objService     = GetObject(&quot;winmgmts:\\.\Root\CIMV2&quot;)
Set objSinkProcess = WScript.CreateObject(&quot;WbemScripting.SWbemSink&quot;, &quot;SinkProcess_&quot;)
objService.ExecNotificationQueryAsync objSinkProcess, &quot;SELECT * FROM __InstanceDeletionEvent WITHIN 0.2 WHERE TargetInstance ISA &#039;Win32_Process&#039;&quot;
Set objSinkPing    = WScript.CreateObject(&quot;WbemScripting.SWbemSink&quot;, &quot;SinkPing_&quot;)
Set objProcess     = objService.Get(&quot;Win32_Process&quot;)
Set objDictionary  = CreateObject(&quot;Scripting.Dictionary&quot;)



ret=LogEvent(String(28,&quot;-&quot;) &amp; &quot; &quot; &amp; Now &amp; String(28,&quot;-&quot;))

bdoneprocess = False
strCount=0    &#039; подсчёт отработавших хостов
intThread=0   &#039; подсчёт количества потоков
QueueLength=0 &#039; длина очереди
intElement=0  &#039; порядковый номер обрабатываемого элемента

Do While intElement&lt;ubound(arrComputers)+1

If QueueLength&lt;8 Then   &#039; ограничение очереди потоков
  Set objContext = CreateObject(&quot;WbemScripting.SWbemNamedValueSet&quot;)
  objContext.Add &quot;hostname&quot;, arrComputers(intElement)
  objService.ExecQueryAsync objSinkPing, &quot;select * from Win32_PingStatus where address =&#039;&quot; &amp; arrComputers(intElement) &amp; &quot;&#039;&quot;, , , , objContext
  intElement=intElement+1
  QueueLength=QueueLength+1
End If
  Wscript.Sleep 100
Loop

objSinkPing.Cancel


While Not bdoneprocess

If intThread=0 Then
bdoneprocess=True
End If

WScript.Sleep 100
Wend

objSinkProcess.Cancel

MsgBox &quot;Готово !!!&quot;


Else           &#039; признак потока с аргументом
strComputer=WScript.Arguments(0)

Set Controller = WScript.CreateObject(&quot;WSHController&quot;)
On Error Resume Next
Set RemoteScript = Controller.CreateScript(SNDestFilePath,strComputer)
WScript.ConnectObject RemoteScript, &quot;remote_&quot;
RemoteScript.Execute
If Err.Number&lt;&gt;0 Then  &#039;2
ret=LogEvent(Now &amp; vbtab &amp; strComputer &amp; &quot; - Connection Error !!!&quot;)
Err.Clear
On Error Goto 0
Else

Do While RemoteScript.Status &lt;&gt; 2 
    WScript.Sleep 100
Loop
ret=LogEvent(Now &amp; vbtab &amp; strComputer &amp; &quot; - Ok !!!&quot;)
End If

WScript.DisconnectObject RemoteScript
Set Controller = Nothing


End If        &#039; конец условия по признакам потоков


Function LogEvent(strInput)
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
LogParentFolderName = fso.GetParentFolderName(Wscript.ScriptFullName) 
LogDestFilePath = fso.BuildPath(LogParentFolderName,LF) 
Set objFile = fso.OpenTextFile(LogDestFilePath, 8, True)
objFile.WriteLine strInput
objFile.Close
End Function



Sub SinkPing_OnObjectReady(objWbemObject, objWbemAsyncContext)
strCount=strCount+1
Set strComputer = objWbemAsyncContext.Item(&quot;hostname&quot;)
res=&quot;&quot;
  Select Case objWbemObject.StatusCode
    Case 0        &#039; хост пингуется
	objProcess.Create &quot;wscript.exe &quot; &amp; Wscript.ScriptFullName &amp; &quot; &quot; &amp; strComputer, null, null, intProcessID
	Wscript.Sleep 10  &#039; профилактическая пауза, добавляет стабильности в работе механизма
	objDictionary.Add intProcessID, intProcessID
	intThread=intThread+1
    Case Else     &#039; хост не пингуется
	ret=Now &amp; vbtab &amp; LogEvent(Now &amp; vbtab &amp; strComputer &amp; &quot; - not respond to ping&quot;)
  End Select
End Sub

Sub SinkProcess_OnObjectReady(objLatestEvent, objAsyncContext)  
   If objDictionary.Exists(objLatestEvent.TargetInstance.ProcessID) Then
	objDictionary.Remove(objLatestEvent.TargetInstance.ProcessID)
	intThread=intThread-1
	QueueLength=QueueLength-1
   End If

End Sub

Sub remote_Error
    Set theError = RemoteScript.Error
    strError=&quot;Error &quot; &amp; theError.Number &amp; &quot; - Line: &quot; &amp; theError.Line &amp; &quot;, Char: &quot; &amp; theError.Character &amp; vbCrLf &amp; &quot;Description: &quot; &amp; theError.Description
    ret=LogEvent(Now &amp; vbtab &amp; strComputer &amp; &quot; - &quot; &amp; strError)
End Sub</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Евген)]]></author>
			<pubDate>Fri, 01 Apr 2011 11:05:03 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=47266#p47266</guid>
		</item>
	</channel>
</rss>
