Friday, January 9, 2015

Code to kill all the other wscripts running on the system Except current wscript || Vb Script || shell

    'Create windows management object
    Dim objWinMgmts
    Dim process_List
    Set objWinMgmts = GetObject("WinMgmts:Root\Cimv2")
    'Get the list of all the wscripts running in the system
    Set process_List = objWinMgmts.ExecQuery("Select * From Win32_Process where name='wscript.exe'")
    'Get the process id of the current executing wscript ('this' object)
    Dim cpid, cpst
    cpid = 0
    cpst = FormatDateTime("01/01/1900")
    For Each objProcess In process_List
        If objProcess.CreationDate > cpst Then
            cpid = objProcess.ProcessId
            cpst = objProcess.CreationDate
        End If
    Next
    'Except current wscript, kill all the other wscripts running on the system
    For Each objProcess In colProcess
        If cpid <> objProcess.ProcessId Then
            objProcess.Terminate
        End If
    Next
    Set process_List = Nothing
    Set objWinMgmts = Nothing

Create and Delete Windows User-defined Environment Variables +Vb Scrpt/Shell


'# Code to set a System/user environment variable and Variable Value:

strVarName = "varWorkingDir"
strVarValue = "D:\"

Function Create_System_Environment_Variable(strVarName ,strVarValue )
             Dim wshshell
             Set wshshell = CreateObject("WScript.Shell")

             Dim WshSysUserEnv

             Set WshSysUserEnv= wshshell.Environment("User")

             WshSysUserEnv("varAutomation_WorkingDir")= strVarValue

             Set WshSysUserEnv= Nothing

End Function

'# Code to remove/delete the environment variable:

Function Remove_System_Environment_Variable(strVarName)


               Dim wshshell

               Set wshshell = CreateObject("WScript.Shell")

               Dim WshSysUserEnv

               Set WshSysUserEnv= wshshell.Environment("User")

               WshSysUserEnv.Remove(strVarName)

               Set WshSysUserEnv= Nothing

End Function