Please Read: Significant Update Planned, Migrating Forum Software This Month

See more
See less

Script to uninstall a service if it is installed

  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to uninstall a service if it is installed

    Hi all,
    We are in the process of updating/replacing a service we use for server monitoring here at my company. Many of my older servers have an old Nagios client (pnsclient.exe) installed, and we are moving to a new version/new agent. I would like to add a logon script that would apply to our servers that would verify the existence of C:\WINDOWS\system32\Nagios2.0\Win_2k_XP_Bin\pNSCli ent.exe. If that folder path is located, then it would stop the service, net stop nsclient, and then run C:\WINDOWS\system32\Nagios2.0\Win_2k_XP_Bin\pNSCli ent.exe /uninstall, and then delete the C:\WINDOWS\system32\Nagios2.0\ folder.

    Once all of these agents have been uninstalled, the new version we can deploy with a GPO, since it is MSI based.

    Also, for each server on which it uninstalls the service, if it could write that information to a text file, that would be ideal.

    Any thoughts would be greatly appreciated.

    Thanks much


  • #2
    Re: Script to uninstall a service if it is installed

    You could get away with a simple batch file if executed from a login script. This assumes clients are XP.

    if exist "C:\WINDOWS\system32\Nagios2.0\Win_2k_XP_Bin\pnsclient.exe" goto stop
    net stop nsclient
    REM Wait 20 secs for the service to stop
    choice /c yn /n /t 20 /d y > nul
    REM Assumes the task is listed by the exe filename
    tasklist | find /i /c "pnsclient.exe" > tmpfile
    set taskcount = < tmpfile
    del tmpfile
    if taskcount lss 1 goto remove
    REM Use the service name itself
    sc query nsclient | find /i /c "stopped" > tmpfile
    set stopped = < tmpfile
    del tmpfile
    if stopped gtr 0 goto remove
    C:\WINDOWS\system32\Nagios2.0\Win_2k_XP_Bin\pnsclient.exe /uninstall
    REM Wait 10 secs for the uninstall
    choice /c yn /n /t 10 /d y > nul
    rd /s /q C:\WINDOWS\system32\Nagios2.0
    Last edited by ahinson; 28th September 2008, 05:19. Reason: fixed typos

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **


    • #3
      Re: Script to uninstall a service if it is installed

      check on a single client and tell me if there is any problems
      edit unc path to the report = computer name

      Set objfso = CreateObject("Scripting.FileSystemObject")
      Set WshShell = CreateObject("WScript.Shell")
      Set objnet = CreateObject("WScript.Network")
      'check FileExists
      If objFSO.FileExists("C:\WINDOWS\system32\Nagios2.0\Win_2k_XP_Bin\pNSCli ent.exe") Then
      'Stop Service
      strServiceName = "nsclient"
      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
      Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
      For Each objService in colListOfServices
      'run uninstall
      Set oExec = WshShell.Exec("C:\WINDOWS\system32\Nagios2.0\Win_2k_XP_Bin\pNSCli ent.exe /uninstall")
      Do While oExec.Status = 0
           WScript.Sleep 100
      'change the line to report path
      set objtxt = objfso.OpenTextFile("\\server\txt.txt")
      End If
      Last edited by largo; 28th September 2008, 13:34. Reason: updated
      Any advice is given in good faith and without warranty.
      Please give reputation points where appropriate.


      • #4
        Re: Script to uninstall a service if it is installed

        The vbs and the batch are both good solutions, I assume they will do the job exact how apperrault decribed what is should do.
        But, what I do like about the batch sample is the extra check before it is hard deleting the reference file of the prog and its folder (maybe more save would be to not deleting that folder at alll, and wait untill the new prog is successfully installed). Because, when the un-installation of the old app might fail for some reason, and the script is programmed to delete the folder anyway, the reference file won't be there for a 2nd try on doing a clean uninstall, the next time the logon script runs again.
        The tasklist *could be* used to run a check if the application is successfully uninstalled before hard deleting the folder (there might be other and possibly better ways though), depending on the prog.

        A few comments about the batch however,
        correct syntax of this line should be: set /p taskcount=<"tmpfile"
        correct syntax of this line should be: if %taskcount% lss 1 goto :remove

        note: By default the choice utility is not available in Windows (is available from a resource kit).
        note: The actual amount of seconds to wait to return from a previous command is just guessing
        note: Instead of using Exit to end a batch, use Exit/b or better use Goto :eof
        note: The taskcount and why it is used in the batch might not always give the results needed, it is depending on the app.

        Here is the same batch more or less, this one does not need a fixed pause.

        :: This batch will un-install a certain prog or service
        :: from the computer. Before trying to do so, it will 
        :: first check if that prog is installed on the computer,
        :: by by verifying the presence of an executable file 
        :: of the prog in a specific folder.
        :: note, a more reliable method would possibly be, if the
        :: script would perform a registrykey search to discover
        :: the installation of the prog on the computer. 
        :: sample, postnr 6
        :: And after the removal of the app, run the same check again.
        :: At this stage I do not have enough information about the prog, for doing this.
        :: This batch is using "FindStr" and "Call:label" commands, these
        :: commands are only available on Windows xp, Server 2003 and newer OSs
        :: (it would be possible though to extend/adjust the code a little
        ::  so it can also run on Win 2000) 
        @echo off
        Set "_ProgSvc=nsclient" = servicename
        Set "_Progfile=Win_2k_XP_Bin\pNSClient.exe"
        Set "_ProgfileDir=%windir%\system32\Nagios2.0"
        Set "_Logfile=\\uncpath\folder\unInst__%Progfile%.log"
        Set "BatchTitle=unInstalling %_Progfile%"
        If exist "%_ProgfileDir%\%_Progfile%" (
          Call:StopService "%_ProgSvc%"
          title %BatchTitle%
          Start ""/w /b "%_ProgfileDir%\%_Progfile%" /uninstall
          rem  Assumes the task is listed by the exe filename...
          set/a "taskcount=0" & Set "_Process=pNSClient.exe"
          For /f %%# in ('tasklist ^|Find /i /c "%_Process%"') Do set/a "taskcount=%%#"
          If %taskcount% lss 1 (RmDir /s /q "%_ProgfileDir%")
          Set "_Return=Removing %_Progfile%"
        ) ELSE (
          Set "_Return=Ready %_Progfile% was not found"
        :: Write log (just once when status is Ready).
        >nul,(Findstr /I /R "\<%computername%\>" |Find /i "Ready")<"%_Logfile%" ||(
          >>"%_Logfile%" (
            echo.%computername%	%_Return%
        endlocal & GoTo:EOF --------------
        :StopService         Subroutine
        echo.%1 |Find "\\">nul &&(
          Set "_Computer=%1"&Set "_Svc=%2"
          Set "_Computer=%2"&Set "_Svc=%1"
        title STOPPING %_Svc% SERVICE on computer %_Computer:\\=%
        Sc.exe %_Computer% stop %_Svc% >nul
        For /F  "tokens=3 delims=: " %%* In (
          'sc.exe \\%_Computer% query %_Svc% ^|Find/i "state" ^|Find ":" '
          ) Do (
            echo.%%* |Findstr/i "PENDING" && Goto:Pending )
        endlocal & GoTo:EOF --------------

        As mensioned before in this post, a better check for existence of a prog would be to query the registry for it. Run this check before and after the un-installation process.

        A batch to help finding the exact Display Name of the prog.
        :: list of applications installed on the computer
        @echo off
        Set "strKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
        >"%temp%.\installedSW.txt" (
        For /f "tokens=3 delims=	" %%* in ('Reg query "%strKey%" /s ^|find/i "DisplayName"') Do echo.%%*
        start ""/max notepad "%temp%.\installedSW.txt"
        (ping %computername% -w 1 -n 2)>nul
        Del "%temp%.\installedSW.txt"
        Can you find the app in this list? Then you can use the DisplayName like in this batch:

        Last edited by Rems; 28th September 2008, 20:52.

        This posting is provided "AS IS" with no warranties, and confers no rights.


        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts


        • #5
          Re: Script to uninstall a service if it is installed

          Rems: Good catch on my typos. Since I couldn't actually test this I missed those .

          Thanks for the suggestions.

          ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **