Friday, March 30, 2007

VBScript: Execute process remotely with WMI

For our terminal/citrix servers we have to regularly run delprof command for deleting inactive user profiles to minimize disk space consumption. The annoying part is to login into each of the servers just to run this command. Even though delprof supports deleting profiles on remote servers with the switch /C:\\<computername>, but that runs terribly slow over WAN links.

A workaround is to remotely execute delprof (or for that matter any other command) on the remote server by using tools like PsExec which installs a temporary service on the remote machine to be able to execute process remotely, and unintalls the same service after the process finishes. Because of its dependency on installing a service, PsExec might not always be a viable option in production environment. So, what's an alternative now?

Enter WMI.

It offers the capability to execute process remotely with the limitation of not allowing any user interaction - the process will run in the background without showing any interface on user's session, which is a perfect feature for running silent installs or non-interactive processes, for example, delprof /q/i

Below is the code snippet which does the job.

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
intReturn = objWMIService.Create(
"delprof /q/i", Null, Null, intProcessID)

This script also uses Popup function of WScript.Shell instance to display message box which disappears after specified seconds - great for showing quick status messages without waiting for any user interaction.

Here is the complete script which executes delprof command on the target server and shows before and after free space information at the end, on the client side.

Assumptions:

  • Delprof executable should already be installed on the target server.
  • Because the script also checks before and after free space on C: drive, it assumes that \Documents and Settings folder is located on server's C: drive.

Reference: http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec06/hey1208.mspx

NOTE: For some weird reason, delprof command is case sensitive. Therefore, DELPROF /q/i does not recognize the "quiet" and "ignore" switches and still prompts: Delete inactive profiles on \\SERVERNAME? (Yes/No)

10 comments:

Webs said...
This comment has been removed by the author.
Webs said...

First of all, thanks so much for your work on the script. Once I get passed my own stupidity I'm sure it will work great!

Every time I try to run the script, I put in the computer name and I get "Error :The remote server machine does not exist or is unavailable: 'GetObject' Code: 800A01CE". I know the name I am using is correct, both systems have full network and Internet connectivity, and both have delprof installed and functional.

For some reason it just won't work. Any help would be greatly appreciated!

Raj said...

yes, I have seen that this script does not work for all the systems. Not sure why, but I would assume it to be related to some permissioning issue.
Please refer to http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec06/hey1208.mspx to see if that helps.
Also try running this script for different target systems to see if all or only few show the errors.

Webs said...

Not much of anything we have done seems to get the delprof utility to work correctly. Just running it by itself or using a simple batch script has the outcome of only deleting a few accounts. We still have at least 50 other accounts that delprof will not delete. I wasn't able to find any information that helps and it appears plenty of other people on the net have the same issue with the answer being to buy a third party app.

Our solution here seems to be to not worry about it too much since we will be starting our Fall build very shortly and tend to rebuild every semester. One other item the admin and I are looking into is doing away with local profiles that essentially cache user account info...

Anyways, so without the needed functionality of delprof it wouldn't be efficient use of my time to see if your script will correctly run delprof. Sorry I can't be of much help.

If we do get into a situation forcing us to look deeper into delprof, I will let you know and may give your script another try.

Thanks for your time and help!

xpyder said...
This comment has been removed by the author.
xpyder said...

Error: The Remote Server machine does not exist or is unavailable: 'GetObject'
Code: 800A01CE

this error happens when the target system is XP Home

I don't have a work around yet, if someone finds a way to get it to work on XP Home please post. I know an Administrator login and password on the target system.

NOTE: you DON'T get the error when running against the local system, whether you use 127.0.0.1 or it's local ip address or even it's netbios name. Makes testing it a little troublesome.

(I wanted to fix a spelling error and couldn't find an edit option"

xpyder said...

I think the failure has something to do with an inability to authenticate as an administrator on XP home
for example
I know you can enable the administrative shares on XP Home by adding the following registry key and restarting
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters]
"AutoShareWks"=dword:00000001

but even after doing that you can't do the following from another computer
net use \\XPHomeName\admin$

if you try you get an access denied message.


NOTE: for some reason it DOES work if it's from the same system (i.e. net use \\127.0.0.1\admin$ works) somehow you ARE able to authenticate as an administrator for loopback connections.

Umesh said...

I have vb script which I want to run on a remote computer through WMI.

That script forcefully install approved windows updates(from WSUS server).

But I have zero knowledge abut scripting.

Could you tell me how I can achieve my goal.

Anonymous said...

Thanks a lot you fucking douche bag!

RRave said...

Hi,
I have seen your blog, its great, if you are interested to publish your great articles, one of the popular site, please reply to me.