Mr. Script

The Season of Giving (and Taking)

This useful script will make your life easier, but may make you a grinch to your users.

Happy Holidays! Because this is the season of giving, I’ve decided to give you a very useful script—one that will make your administrative lives much simpler. That’s the good news. The bad news is that your users might start calling you things like "grinch" once you start using it. Last month I demonstrated how to enumerate installed software on remote machines. This month I’ll show how to remove any unauthorized software discovered as a result of running that script—and how to do it remotely.

<?xml version="1.0" ?>
<package>
<comment>
UninstallSoftware.wsf This script uninstalls an .MSI package from a remote computer
</comment>

      <job>

      <runtime>

      <description>
      This script uninstalls the .MSI package specified by
      the "Package" argument on the computer specified by
     the "Target" argument.
      </description>

      <example>
      C:\cscript InstallSoftware.wsf /Target:value
         /Package:value
      </example>

      <named
      name=
"Target"
      helpstring="The Name of the target computer"
      required="true"
      type="string"
      />

      <named
      name=
"SWName"
      helpstring="The path to the MSI package to install"
      type="string"
      required="true"
      />

      </runtime>

      <script language="VBScript">

      <![CDATA[
      Option Explicit
      Dim strComputer, strSWName, objWMI, colSoftware,
         objSoftware
      strComputer=WScript.Arguments.Named.Item("Target")
      strSWName=WScript.Arguments.Named.Item("SWName")
      Set objWMI=GetObject_       ("winmgmts:{impersonationLevel=impersonate}!\\"_
      & strComputer & "\root\cimv2")
      Set colSoftware = objWMI.ExecQuery_
      ("Select * from Win32_Product Where Name = "_
      & strSWName)

     For
      Each objSoftware in colSoftware
      objSoftware.Uninstall()
      Next
      ]]>
      </script>
   </job>
</package>

This script is fairly basic. I use the standard XML elements for named arguments, pass those arguments to the script, and remove that software package. The only caveat about this script is that it’ll be much easier to use if you run last month’s enumeration script first, as this script relies upon your knowing the MSI name of the product you wish to uninstall. To run this script, simply go to a command line and type:

Cscript uninstallsoftware.wsf /Target:computer /SWName:software

Just as the script last month could only list software installed using the Windows Installer, this script will only uninstall Windows Installer .MSI files. Still, it should be effective for removing most of the unauthorized software in your organization, thereby contributing to a marked lack of holiday spirit and general year-end malaise. Bah Humbug!

Will a bonus script help? Perhaps a bonus script that will allow you to install software?

<?xml version="1.0" ?>
<package>
<comment>
InstallLocal.wsf
This script installs an .MSI package on the local computer
</comment>

   <job>

      <runtime>

      <named
      name="Package"
      helpstring="The path to the MSI package to install"
      type="string"
      required="true"
      />
      </runtime>

      <script language="VBScript">
      <![CDATA[
      strPackage=WScript.Arguments.Named.Item("Package")
      Set objService = GetObject("winmgmts:")
      Set objSoftware = objService.Get("Win32_Product")
      errReturn = objSoftware.Install(strPackage, ,True)
      ]]>
      </script>
   </job>
</package>

This script uses WMI to install an MSI package on the local computer. Using WMI to install software remotely is a bit more complex. Perhaps I’ll cover that next month. You can run this script by going to a command line and typing:

Cscript installlocal.wsf /Package:filename

When you write a monthly column, it’s easy to slip into a routine and make assumptions about the skill level of those to whom you are writing. I just assume that, every month, the same five people are reading this column. (You know who you are—you’re the ones who keep sending me e-mails that begin "Dear scripting god…")

Well, apparently, there are new readers joining every month. I’ve been getting e-mails asking me to explain how to run the scripts. The three rules here are simple: I accept all accolades but no money. You must at least try to write the script yourself before you ask me how to do it. Finally, you must pretend to have a complete disdain for "users" (even if you, as I do, secretly like them).

Because many of these scripts are part of a related series (as this month’s is), I may not always be able to bring you fully up to speed by way of a recap. I always recommend reviewing back issues, when possible. They’re available online. However, I will try to always show you—at a minimum—how to run the script from the command line. Whenever possible, I will do my best to cover the highlights of previous relevant columns so you may better understand the current script. Consider it my holiday gift to you.

About the Author

Chris Brooke, MCSE, is a contributing editor for Redmond magazine and director of enterprise technology for ComponentSource. He specializes in development, integration services and network/Internet administration. Send questions or your favorite scripts to [email protected].

comments powered by Disqus
Most   Popular