Молчание...
Ok, вот новая версия, отсылающая сообщение пользователю, превысившему лимит:
Код:
Set Args = WScript.Arguments
iaFilename = "watcher.cfg"
' *******************************************
' Script, that allow to limit quantity of the
' started applications on a terminal server.
' The names of applications and the maximum
' quantity of simultaneously started spears
' are stored in config file.
'
' @authors: SkyF, hasherfrog
' @site: oszone.net
'
CONST FIELDS = 3
CONST PSNAME = 0
CONST PSNUMB = 1
CONST PSMAXN = 2
Dim thePsArray ( )
Dim nToWatch
nToWatch = 0
Function AddPsToWath( name, number )
ReDim Preserve thePsArray ( FIELDS, nToWatch )
thePsArray(PSNAME, nToWatch) = name
thePsArray(PSNUMB, nToWatch) = number
thePsArray(PSMAXN, nToWatch) = number
addPsToWatch = nToWatch
nToWatch = nToWatch + 1
End Function
' *******************************************
Sub ReadPsWatchFile(filename)
Dim fso, f
' On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename, 1, False)
if Err.Number<>0 then
call Error_(0)
else
while not f.atEndOfStream
s = f.ReadLine
ms = Split(s, " ", -1, vbBinaryComapre)
s1 = ms(0)
n2 = CInt(ms(1))
n = AddPsToWath(s1, n2)
Err.Clear
wend
f.Close
end if
End Sub
' *******************************************
Sub StartUpWatcher()
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
& "Targetinstance ISA 'Win32_Process'")
Do While 1
Set objLatestProcess = colMonitoredEvents.NextEvent
For psn=0 to nToWatch-1
If objLatestProcess.TargetInstance.Name = thePsArray(PSNAME, psn) Then
Select Case objLatestProcess.Path_.Class
Case "__InstanceCreationEvent"
thePsArray(PSNUMB, psn) = thePsArray(PSNUMB, psn) - 1
If thePsArray(PSNUMB, psn) < 0 then
colProperties = objLatestProcess.TargetInstance.GetOwner(strNameOfUser, strUserDomain)
objLatestProcess.TargetInstance.Terminate
msg = "Вы запустили лишнюю копию программы "& objLatestProcess.TargetInstance.Name _
& " - разрешенный лимит только " & thePsArray(PSMAXN, psn)
Set objShell = CreateObject("WScript.Shell")
objShell.Exec("%COMSPEC% /k net send " & strNameOfUser & " " & msg)
End If
Case "__InstanceDeletionEvent"
thePsArray(PSNUMB, psn) = thePsArray(PSNUMB, psn) + 1
End Select
End If
Next
Loop
End Sub
' *******************************************
for i=0 to Args.count-1
If (Args(i) = "-f") AND (i+1 < Args.count) Then
iaFilename = Args(i+1)
End If
Next
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(iaFilename)) Then
Call ReadPsWatchFile(iaFilename)
If nToWatch=0 Then
Wscript.Echo "File is empty."
Else
Call StartUpWatcher()
End If
Else
Wscript.Echo "Config file is not found."
End If
Требования: У пользователя на машине должна работать "Служба сообщений".
Фичи: поддерживается дефолтный конфиг-файл watcher.cfg