Author Topic: [BAT] Use WMI to list, (un)install software on remote systems  (Read 60 times)

0 Members and 1 Guest are viewing this topic.

Offline HardcoreHacker

  • TechKnow Owner
  • Administrator
  • Uber Member
  • *****
  • Thank You
  • -Given: 3697
  • -Received: 1128
  • Posts: 9632
  • Karma: +2278/-106
Hey sysadmins, it has come to my attention that many sysadmins don't use the power of WMI, when I learnt it, it made my life easier, so let's share here what made it so easy:

If you are a little bit of a scripter you can easily make a script to run on all the computers on your network:

Basics:

index software packages:
wmic /node:%CNAME% product get name,version,vendor

index software based on vendor:
wmic /node:%CNAME% product where "vendor like 'Micro%'" get name,vendor

index software based on version:
wmic /node:127.0.0.1 product where "version like '7%'"

uninstall a certain software package:
wmic /node:%CNAME% product where "name='Adobe AIR'" call uninstall /nointeractive

uninstall all Acrobat on a LIST of computers (divided by newlines):
wmic /failfast:on /node:@"c:\computers.txt" product where name="Acrobat.com" call uninstall /nointeractive

uninstall all HP bloatware of a system (mutiple reboots required)
wmic /node:%CNAME% product where "vendor like 'Hewlett%'" call uninstall /nointeractive

install a msi:
wmic /node:%CNAME% product call install true,"" , "C:\%msiname%"
(please be warned as you first need to upload your MSI file to the remote station before you can deploy it there, I am sure you can make some easy script that does all this.
I have my simple networkscanner script setup to run together with wmic so each found system will receive the commands I define. With minor tweaking I can also patch according to OS version or presence of certain software for example. This made life a whole lot easier)


Usage:

You can easily combine this with something like: "SET /P CNAME=Enter target computername: " , if you put that in front of the command, and the command seeks %CNAME% for node, you can quickly remote command a system. Or combine it with a scanner: the code is a little iffy, but here's some shit-simple-ping-based-network-scanner-script that looks neat: http://www.techknow.one/forum/index.php?topic=9380.0 and since it's just batch code you can modify it easily.

Checks: ALWAYS check AND log if software is installed succesfully, use %ERRORLEVEL% to determine if a installation failed or was a succes.


Simple example of a script listing all MSI files in it's directory, mapping them to a number for easy selection, and deploying to a remote system:
Code: [Select]
@echo off > TMP1
:BOF
set #=0
setLocal EnableDelayedExpansion
cls
echo.
echo Remote App MSI Installer
echo.
for /f "tokens=*" %%D in ('dir /b *.msi') do (
set /a #+=1
echo [!#!] %%D >> TMP1 )
echo. >>TMP1
type TMP1|more
set /p dirNUM=Enter MSI number:
find "[!dirNUM!]" < TMP1 > TMP2
for /f "tokens=2" %%D in (TMP2) do set msiname=%%D
del /q TMP1 && del /q TMP2
set /p cname=Input computer name:
xcopy "%msiname%" "\\%cname%\c$" /y
if not exist "\\%cname%\c$\%msiname%" color 0c && rem cls && echo ERROR: FILE NOT FOUND! && pause && goto EOF
wmic /node:%CNAME% product call install true,"" , "C:\%msiname%"
echo.
echo Removing installation files from remote. . .
echo.
del /f "\\%cname%\c$\%MSINAME%"
echo Done && echo. && pause
goto :BOF
:EOF

Advanced example, including remote registry to deploy application settings:
Code: [Select]
@echo off > TMP1
:BOF
set #=0
setLocal EnableDelayedExpansion
cls
echo.
echo Remote App MSI Installer
echo.
for /f "tokens=*" %%D in ('dir /b *.msi') do (
set /a #+=1
echo [!#!] %%D >> TMP1 )
echo. >>TMP1
type TMP1|more
set /p dirNUM=Enter MSI number:
find "[!dirNUM!]" < TMP1 > TMP2
for /f "tokens=2" %%D in (TMP2) do set msiname=%%D
del /q TMP1 && del /q TMP2
set /p cname=Input computer name:
xcopy "%msiname%" "\\%cname%\c$" /y
if not exist "\\%cname%\c$\%msiname%" color 0c && rem cls && echo ERROR: FILE NOT FOUND! && pause && goto EOF
wmic /node:%CNAME% product call install true,"" , "C:\%msiname%"
sc \\%CNAME% start "remoteregistry"
echo Delaying for service to startup properly ...
ping %CNAME% -n 3
REG ADD \\%CNAME%\HKLM\Software\Wow6432Node\TightVNC\Server /v Password /t REG_BINARY /d "31bb662aace625aa" /f
REG ADD \\%CNAME%\HKLM\Software\Wow6432Node\TightVNC\Server /v ControlPassword /t REG_BINARY /d "31bb662aace625aa" /f
REG ADD \\%CNAME%\HKLM\Software\Wow6432Node\TightVNC\Server /t REG_DWORD /v SaveLogToAllUsersPath /d "0" /f
ping %CNAME% -n 5
echo.
echo Removing installation files from remote. . .
echo.
del /f "\\%cname%\c$\%MSINAME%"
echo Done && echo. && pause
goto :BOF
:EOF

Enjoy being more creative in your solutions.

Enjoy not installing third party software on servers and domain controllers (feeding it domain admin credentials and hope that it doesn't call home)

Enjoy not needing to pay for simple solutions.

Have a nice day
« Last Edit: July 31, 2017, 11:19:41 PM by HardcoreHacker »

 

Powered by EzPortal