SunQuest
 
       Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Working with System Processes in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Working with System Processes in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-01-15

    Table of Contents:
  • Working with System Processes in WSH
  • Taking a look under the hood
  • More fun with processes
  • Modifying processes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Working with System Processes in WSH - Modifying processes


    (Page 4 of 4 )

    The final set of code samples I have for you is designed for special needs when working with processes that you just may develop a need to script.  We'll begin by modifying a specific process and then we'll look at some cool things we can do with our own.

    Const IDLE = 64

    Const BELOW_NORMAL = 16384

    Const NORMAL = 32

    Const ABOVE_NORMAL = 32768

    Const HIGH_PRIORITY = 128

    Const REAL_TIME = 256

     

    strProcess = "iexplore.exe"

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Set colProcesses = objWMIService.ExecQuery _

       ("Select * from Win32_Process Where Name = '" & strProcess & "'")

     

    For Each objProcess In colProcesses

       result = objProcess.SetPriority(ABOVE_NORMAL)

    Next

    The setPriority method allows you to control the priority of a running process.  It accepts predefined constant values to indicate the priority level and issues a return code from the table below.

    Return Code

    Description

    0

    Successful completion

    2

    Access denied

    3

    Insufficient Privilege

    8

    Unknown Failure

    9

    Path Not Found

    21

    Invalid Parameter

    strProcess = "iexplore.exe"

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Const ABOVE_NORMAL = 32768

     

    Set objStartup = objWMIService.Get("Win32_ProcessStartup")

    Set objConfig = objStartup.SpawnInstance_

    objConfig.PriorityClass = ABOVE_NORMAL

      

    Set objProcess = GetObject("winmgmts:rootcimv2:Win32_Process")

    objProcess.Create strProcess, Null, objConfig

    In the same way, it might also be useful to start a process at a specific priority level.  This code does just that for you.  It's a slightly modified combination of two examples you've already seen.  The PriorityClass property accepts the same values as the setPriority method that we just examined.

    But settings a process' priority isn't all there is to do, is it?  It might also be nice to control how the process starts and runs.  After all, most processes run hidden, don't they?

    strProcess = "iexplore.exe"

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Const HIDDEN_WINDOW = 12

      

    Set objStartup = objWMIService.Get("Win32_ProcessStartup")

    Set objConfig = objStartup.SpawnInstance_

    objConfig.ShowWindow = HIDDEN_WINDOW

      

    Set objProcess = GetObject("winmgmts:rootcimv2:Win32_Process")

    objProcess.Create strProcess, Null, objConfig

    This time we rely on the ShowWindow property of the Win32_ProcessStartup class to set our process to run hidden.  This additional class allows us to control how processes start.  There are many properties and methods available with this class to control how a process starts and operates.  For more information, visit the link below.

    MSDN: Win32_ProcessStartup Class

    The final example I would like to provide for you is one that I sometimes find very useful.  It gives you the ability to prevent a process from running.  Essentially, it monitors for a process to start and then ends it.

    strProcess = "iexplore.exe"

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Set colProcess = objWMIService.ExecNotificationQuery _

       ("Select * from __InstanceCreationEvent" _

           & " WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'")

     

    Do While True

       Set objLatestProcess = colProcess.NextEvent

       If objLatestProcess.TargetInstance.Name = strProcess Then

           objLatestProcess.TargetInstance.Terminate

       End If

    Loop

    This code basically combines our examples for monitoring a process start-up and ending a process.  Again, this code can be further modified to suit your needs.  This example demonstrates how you can combine the techniques you've seen to produce a usable script.

    Take the time to explore the MSDN documentation for the Win32_Process and Win32_ProcessStartup classes.  They provide many other properties and methods that I was unable to include for lack of space.  By reading the documentation and getting to know these WMI classes, you can add a lot of power to your scripts and more weapons to your scripting arsenal.  Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Learn how to capture the power of WMI in your WSH scripts to manipulate, create, and...
     

    WINDOWS SCRIPTING ARTICLES

    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions
    - VBScript: Functioning with Strings
    - Working with the Windows Registry in C++
    - Understanding Objects
    - HTML Applications: Giving WSH a User Interfa...
    - Modifying Computer Objects with Active Direc...
    - Logon Script to Send Email Notifications
    - Securing Computers and Active Directory
    - Moving and Renaming Computers with Active Di...
    - Working with System Processes in WSH





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway