Solved

Batch File [ADVANCED]: Uninstall remote apps [WMIC in a FOR loop]

Posted on 2012-03-23
8
1,399 Views
Last Modified: 2012-03-24
Hi there,

In line 11, the data is extracted in a way that lines 14 and 15 does not work as expected.

Thanks for your help,
Rene

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET LogFile=%~n0.log

ECHO %date% %time% START>>"%LogFile%"
ECHO ----------------------------------->>"%LogFile%"

FOR /F "delims=\ " %%A IN ('NET VIEW ^| FINDSTR -i "\\"') DO (
   ECHO [%%A]
   REM UNINSTALLING "PROGRAMS"
      FOR /F "delims=" %%B in ('wmic /node:"%%A" product get name ^| findstr -i "LibreOffice OpenOffice"') DO (
         SET ProductName=%%B
         SET ProductName=!ProductName: =_!
         ECHO UNINSTALLING [%%B]
         WMIC /failfast:on /node:"%%A" product where name="%%B" call uninstall /nointeractive
         SET Result=%%A=!ProductName!=UNINSTALLED:!errorlevel!
      )
)

REM CREATING LOG FILE
FOR /F "tokens=2-4 delims==" %%A in ('SET Result=') DO (
   ECHO [%%A] [%%B] [%%C] [%%D]
   ECHO %Date% %Time% [%%A] [%%B] [%%C]>>"%LogFile%"
)
ECHO ----------------------------------->>"%LogFile%"
ECHO %date% %time% COMPLETED>>"%LogFile%"
ECHO.>>"%LogFile%"
PAUSE
EXIT

Open in new window

0
Comment
Question by:ReneGe
  • 4
  • 3
8 Comments
 
LVL 51

Expert Comment

by:Bill Prew
ID: 37759644
Rene, what product are you looking for, it seems like you are getting the name attribute, for a product named "LibreOffice OpenOffice", so if you know the name why look for it?  Is it to see if it is installed?  Or is that only a partial substring of the full name?

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 37759677
It is indeed a partial substring of the full name. The problem here is that Open Office and Libre Office includes the version number in the name. So I want to make sure all version are removed through out the network.

Thanks,
Rene
0
 
LVL 15

Accepted Solution

by:
achaldave earned 300 total points
ID: 37759781
Try this for each application

wmic /failfast:on product where "name like 'openoffice%'" call uninstall /nointeractive
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
ID: 37759789
Yes, I was going to suggest using the LIKE operator in the WMIC command.  Take a look at this reference on WQL...

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394606%28v=vs.85%29.aspx

~bp
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Author Comment

by:ReneGe
ID: 37759811
Following achaldave command line and bill's reference URL on WQL, the following works:
wmic /node:"remotepcname" /failfast:on product where "name like 'openoffice%' or name like 'libreoffice%'" call uninstall /nointeractive

Open in new window


If a PC would have both programs, Open Office and Libre Office, would they both be uninstalled?

Thanks
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 37759814
I think so, but I haven't used WMIC in that manner so don't take my word for it :-).

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 37759906
Ok, here is the verdict!

I installed both apps, then ran the following:

wmic /node:"RemotePcName" /failfast:on product where "name like 'OpenOffice%' or name like 'LibreOffice%'" call uninstall /nointeractive

Both apps were uninstalled

Very cool!!

Thanks to you two :)

Cheers,
Rene
0
 
LVL 10

Author Comment

by:ReneGe
ID: 37761003
FYI

WMIC /node:"RemotePcName" /failfast:on product where "name like 'LibreOffice%' or name like 'OpenOffice%'"

Works when I run it in a command prompt.  When I run it from within a batch file, dos thinks that whatever is between the % is a variable name.

To resolve this, I replaced % by %%, as in:
WMIC /node:"RemotePcName" /failfast:on product where "name like 'LibreOffice%%' or name like 'OpenOffice%%'"

Cheers,
Rene
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now