January 27, 2014

VBScript to terminate HP OMW console of the users disconnected from more than 1 hour

Below is the vbscript code to terminate HP OMW console of the users disconnected from more than 1 hour -


'----------------------------------------------------
' Script: Terminate_HPOM_Console.vbs
'
' Author: Amit Kumar
'
' Usage: 
'  Terminate_HPOM_Console.vbs
'
' This script will Terminate HP OMW console of the users who are disconnected from more than one hour
'----------------------------------------------------


' Set the time period to retrieve the event logs of last one hour 

Const CONVERT_TO_LOCAL_TIME = True

Set dtmStartDate = CreateObject("WbemScripting.SWbemDateTime")
Set dtmEndDate = CreateObject("WbemScripting.SWbemDateTime")
dtmStartDate.SetVarDate dateadd("n", -60, now)' Set the start time to 1 hour ago of current time
dtmEndDate.SetVarDate dateadd("n",0, now) ' End time is set to current time


computerName = WScript.Arguments(0) 'Name of the server to look for the disconnected sessions

Dim strCmd
    strCmd = "cmd /q /c ""quser| find /i ""Disc"" "" " 'Command that checks for the disconnected users


Set objShell = WScript.CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strCmd)


Do
     line = objExec.StdOut.ReadLine()
     strOutput = line & vbcrlf
c = InStr(strOutput,"Disc")
userName = Trim(Mid(strOutput,2,7)) 'Name of the users who are found disconnected




If (userName <> "") Then 'This condition checks if any user is found disconnected


'List all the session disconnect event logs of the disconnected users that came in last one hour

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & computerName & "\root\cimv2")

Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'Security' and "&" EventCode = '683' and "&" Message like '%"& userName &"%' and "&" TimeWritten >= '"& dtmStartDate & "' and TimeWritten < '" & dtmEndDate & "'")

'Checks whether the user's disconnected time duration is more than 1 hour. 
'This is checked by counting the event logs and if count is found to be 0 
'it indicates that the specific user is disconnected from more than 1 hour

If (colLoggedEvents.Count = 0) Then

'WScript.echo "Terminate HPOMW console " & userName
Call KillProcess (userName,computerName) 'Call kill process function to terminate HPOM console

End If
End If

Loop While Not objExec.Stdout.AtEndOfStream


'Function to terminate HPOM Remote console of the disconnected users

Function KillProcess (userName,computerName)


Dim objWMIService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=Impersonate,(debug)}!\\" & computerName & "\root\cimv2")

Dim colProcesses : Set colProcesses = objWMIService.ExecQuery("Select * From Win32_Process Where Name = 'mmc.exe'")
Dim objProcess, strUserName, strDomain, strCommandLine
For Each objProcess In colProcesses
    On Error Resume Next
    strCommandLine = objProcess.CommandLine
    objProcess.GetOwner strUserName

'Kills only the mmc process of the users disconnected from more than one hour

If (strUserName=userName) Then

'WScript.Echo strUserName
objProcess.Terminate()


    End If
     
    On Error GoTo 0
Next

End Function

No comments:

Post a Comment