Run Ninite Pro with Nice Logs from MAXfocus

20 Jan

Run Ninite Pro with Nice Logs from MAXfocus

I’ve deployed Ninite Pro to update non-Windows applications on computers that I manage. I’m running it in silent mode but I wanted some decent logging so I can review what it did. This is especially useful when run from the MAXfocus dashboard as a Scheduled Task:  the output is visible from the dashboard.

The script takes two parameters:

Param 1 Path to local or network copy of NiniteOne.exe.

Param 2 Optional path to a NiniteDownloads cache.  This can be on a network share, in which case the MAXfocus agent must run as a user with network access (not as the default SYSTEM account).  Defaults to %temp%\NiniteDownloads if not specified.  Fails if not accessible.

Example (type on one line):

NiniteUpdate.cmd "%SystemDrive%\Scripts\Helpers" "\\SRV01\NiniteDownloads"

Behavior

Ninite is run three times:  first in /audit mode, then in /updateonly mode, then again in /audit mode. Output is written to the NiniteUpdateSummary.log file in the same folder as the script. “Not installed” messages are omitted, so you get a nice summary of what is installed. When the script completes, the NiniteUpdateSummary.log file is written to stdout (which is what is displayed in the MAXfocus dashboard). Additionally, the full result of the final audit (including the “Not installed” messages) is saved as NiniteAuditAfterUpdate.log.

There is a hard-coded variable in the script called CommonParams that excludes Microsoft programs (I use Microsoft Update for those), some other programs that were not updating, and TeamViewer (since that update can clobber the MAXfocus Take Control function). I also set it to /disableshortcuts. You may want to adjust these parameters and exclusions; see the Ninite command-line switch reference and their list of apps.

Sample Output

If you run this script from MAXfocus, you’ll want to run it as an automated task—it’s too long to run as a DSC check. I let it run for an hour. After it completes, the Automated Task window will look like this:

NinitePro Automated Task Output

If you scroll in the Details area, you’ll see the full output:

--------------------------------------------------------------
Ninite -
 Audit before updating - Tue 12/16/2014 - 4:00:51.31
Partial
Air : OK -
 15.0.0.356
Firefox : OK - 34.0.5
Flash : Update - 15.0.0.246 ->
 16.0.0.235
Reader : Update - 10.1.7 -> 11.0.10
Skype : Update -
 6.22.0.107 -> 7.0.0.102
--------------------------------------------------------------
 
Ninite - Update Only - Tue 12/16/2014 - 4:00:59.83
OK
Flash :
 OK
Skype : OK
Reader :
 OK
--------------------------------------------------------------
Ninite
 - Audit after updating - Tue 12/16/2014 - 4:07:55.61
OK
Air : OK -
 15.0.0.356
Firefox : OK - 34.0.5
Flash : OK - 16.0.0.235
Reader : OK -
 11.0.10
Skype : OK -
 7.0.0.102
--------------------------------------------------------------
 
Exiting with ExitCode 0

The Script

Save this as NiniteUpdate.cmd. For easier reading and editing, open it in Notepad++. See comments in the script re. various updates since its initial release.

@echo off
REM Run Ninite /updateonly on the local computer, optionally caching to local or network path.
REM
REM Copyright 2015 by Mark Berry, MCB Systems, www.mcbsys.com. 
REM Free for personal or commercial use.  May not be sold.
REM No warranties.  Use at your own risk.
REM
REM Param 1:  Path to local or network copy of NiniteOne.exe. 
REM           Path only; assumes "NiniteOne.exe" for program name.
REM
REM Param 2:  Path to NiniteDownloads cache.  Can be on a network share.  Defaults
REM           to %temp%\NiniteDownloads if not specified.  Fails if not accessible.
REM
REM Assumes you have administrative permissions on the local PC.
REM
REM Creates/replaces NiniteUpdateSummary.log in %ProgramPath%.
REM
REM 12/11/2014 Initial script.
REM
REM 12/13/2014 Modify to create and save NiniteUpdateSummary.log as combined log file.
REM            Individual Ninite runs write to NiniteTemp.log, deleted after use.
REM            Final audit saved to NiniteAuditAfterUpdate.log.
REM
REM 12/16/2014 Add exclusions for Avast and AVG (can't update security software), CCleaner
REM            (vendor doesn't allow update) and Messenger (discontinued).
REM
REM 01/31/2015 Exclude ".NET 3.5.2", a new Microsoft program.
REM
REM 03/16/2015 Print program and cache path at beginning of NiniteUpdateSummary.log.
REM
REM 04/02/2015 Show parameters in log. 
REM            Don't try to write to log if log path (%ProgramPath%) not found.
REM
REM 04/27/2015 - Fix bug that was putting cache in C:\Program Files (x86)\Advanced Monitoring Agent\scripts\-logfile
REM              folder when no cache location was specified and script was run from Max RM.
REM            - Set default cache location to NiniteOne.exe folder (param 1) + \NiniteDownloads.
REM
REM 11/04/2015 - Max RM agent 9.12.3 introduced a bug that encloses parameters in single quotation marks if they
REM              were passed in double quotation marks.  Strip out single quotation marks around parameters.
REM ===========================================================================================
REM Set up variables
REM ===========================================================================================
REM This script can be adapted to run another program by modifying the next two lines
set ProgramExe=NiniteOne.exe
REM Exit with 0; exceptions below
set /A exitcode=0
REM ===========================================================================================
REM Check for parameters
REM ===========================================================================================
if ###%1###==###### goto NoParam1
goto Param1Found
:NoParam1
echo.
echo Missing parameter(s)
echo.
echo Usage:  NiniteUpdate.cmd PathToNinite [NiniteDownloadsCache]
echo.
echo         Example:
echo.
echo         NiniteUpdate.cmd "C:\Scripts\Helpers" "\\SRV01\NiniteDownloads"
echo.
set /A exitcode=1001
goto End
:Param1Found
REM Strip double quotation marks, if any, from parameter
set ProgramPath=%~1
REM Now strip single quotation marks, if any, added by Max RM caller bug.
REM See http://ss64.com/nt/syntax-dequote.html.
set ProgramPath=###%ProgramPath%###
set ProgramPath=%ProgramPath:'###=%
set ProgramPath=%ProgramPath:###'=%
set ProgramPath=%ProgramPath:###=%
if ###%2###==###### goto NoParam2
REM Max RM adds a -logfile parameter as the last parameter.  Ignore that.
if ###%2###==###-logfile### goto NoParam2
goto Param2Found
:NoParam2
set CachePath=%ProgramPath%\NiniteDownloads
goto EchoParams
:Param2Found
REM Strip double quotation marks, if any, from parameter
set CachePath=%~2
REM Now strip single quotation marks, if any, added by Max RM caller bug.
REM See http://ss64.com/nt/syntax-dequote.html.
set CachePath=###%CachePath%###
set CachePath=%CachePath:'###=%
set CachePath=%CachePath:###'=%
set CachePath=%CachePath:###=%
:EchoParams
REM ===========================================================================================
REM Echo parsed parameters
REM ===========================================================================================
REM echo   ProgramPath: %ProgramPath%
REM echo    ProgramExe: %ProgramExe%
REM echo     CachePath: %CachePath%
REM echo.
REM ===========================================================================================
REM Check for program path
REM ===========================================================================================
if exist "%ProgramPath%" goto ProgramPathExists
echo Program path "%ProgramPath%" does not exist. Exiting.
set /A exitcode=1002
goto End
:ProgramPathExists
REM ===========================================================================================
REM Check for program executable
REM ===========================================================================================
if exist "%ProgramPath%\%ProgramExe%" goto ProgramExists
echo Program executable "%ProgramPath%\%ProgramExe%" does not exist. Exiting.
set /A exitcode=1003
goto End
:ProgramExists
REM ===========================================================================================
REM Check for cache path
REM ===========================================================================================
REM First try to create it if it doesn't exist
if not exist "%CachePath%" md "%CachePath%"
REM If it still doesn't exist, e.g. if it's on an inaccessible network share, fail
if exist "%CachePath%" goto CachePathExists
echo Downloads cache path "%CachePath%" does not exist and could not create it. Exiting.
set /A exitcode=1004
goto End
:CachePathExists
REM ===========================================================================================
REM Run Ninite three times with various parameters
REM ===========================================================================================
set CommonParams=/silent "%ProgramPath%\NiniteTemp.log" /disableshortcuts /exclude ".NET" ".NET 3.5" ".NET 4" ".NET 4.5" ".NET 4.5.1" ".NET 4.5.2" Avast AVG CCleaner Essentials Messenger Office OneDrive OpenOffice Silverlight SkyDrive TeamViewer
set CacheParam=/cachepath "%CachePath%"
REM Send all output to NiniteUpdateSummary.log file.  First redirect overwrites previous log; the rest append.
set LogFullPath=%ProgramPath%\NiniteUpdateSummary.log
echo NiniteUpdate.cmd > "%LogFullPath%"
echo. >> "%LogFullPath%"
echo       Program: %ProgramPath%\%ProgramExe% >> "%LogFullPath%"
echo   Cache param: %CacheParam% >> "%LogFullPath%"
echo Common params: %CommonParams% >> "%LogFullPath%"
echo      Log file: %LogFullPath% >> "%LogFullPath%"
echo. >> "%LogFullPath%"
echo -------------------------------------------------------------- >> "%LogFullPath%"
echo Ninite - /audit before updating - %date% - %time% >> "%LogFullPath%"
echo. >> "%LogFullPath%"
"%ProgramPath%\%ProgramExe%" /audit %CommonParams% %CacheParam%
set /a errorcode=%errorlevel%
if %errorcode% EQU 0 goto Continue1
echo Running first %ProgramExe% /audit failed with ErrorLevel %errorcode%.  Exiting.
set /A exitcode=1100+%errorcode%
goto PrintLogAndEnd
:Continue1
REM Print lines that do NOT contain the string ": Not installed"
type "%ProgramPath%\NiniteTemp.log" | find /V ": Not installed" >> "%LogFullPath%"
del "%ProgramPath%\NiniteTemp.log"
echo. >> "%LogFullPath%"
echo -------------------------------------------------------------- >> "%LogFullPath%"
echo Ninite - /updateonly - %date% - %time% >> "%LogFullPath%"
echo. >> "%LogFullPath%"
"%ProgramPath%\%ProgramExe%" /updateonly %CommonParams% %CacheParam%
set /a errorcode=%errorlevel%
if %errorcode% EQU 0 goto Continue2
echo Running %ProgramExe% /updateonly failed with ErrorLevel %errorcode%.  Exiting.
set /A exitcode=1100+%errorcode%
goto PrintLogAndEnd
:Continue2
type "%ProgramPath%\NiniteTemp.log" >> "%LogFullPath%"
del "%ProgramPath%\NiniteTemp.log"
echo. >> "%LogFullPath%"
echo -------------------------------------------------------------- >> "%LogFullPath%"
echo Ninite - /audit after updating - %date% - %time% >> "%LogFullPath%"
echo. >> "%LogFullPath%"
"%ProgramPath%\%ProgramExe%" /audit %CommonParams% %CacheParam%
set /a errorcode=%errorlevel%
if %errorcode% EQU 0 goto Continue3
echo Running second %ProgramExe% /audit failed with ErrorLevel %errorcode%.  Exiting.
set /A exitcode=1100+%errorcode%
goto PrintLogAndEnd
:Continue3
type "%ProgramPath%\NiniteTemp.log" | find /V ": Not installed" >> "%LogFullPath%"
REM Save final full log as NiniteAuditAfterUpdate.log (delete old audit first)
del "%ProgramPath%\NiniteAuditAfterUpdate.log"
ren "%ProgramPath%\NiniteTemp.log" "NiniteAuditAfterUpdate.log"
:PrintLogAndEnd
echo. >> "%LogFullPath%"
echo -------------------------------------------------------------- >> "%LogFullPath%"
echo. >> "%LogFullPath%"
REM Type log to stdout so it will appear in Max RM dashboard
type "%LogFullPath%"
echo Exiting with ExitCode %exitcode% >> "%LogFullPath%"
:End
REM Display ExitCode in stdout even if log file not available
echo Exiting with ExitCode %exitcode%
exit /b %exitcode%