@echo off
REM 2015 Bernhard Linz / Bernhard.Linz@datagroup.de / Bernhard@znil.de
REM Script for Installing Zabbix-Agent on Windows System via GPO,
REM take a look at: http://znil.net/index.php?title=Zabbix_Agent_per_Gruppenrichtlinie_installieren_lassen
@echo off
cls
Echo +---------------------------------------------------------+
Echo ^| Starting Script InstallZabbixAgentGPO                   ^|
Echo +---------------------------------------------------------+
Echo.
REM Globale Variables
Set GPO_INSTALL_AGENT=FALSE
Set GPO_COMPUTERNAME=%COMPUTERNAME%               x
Set GPO_COMPUTERNAME=%GPO_COMPUTERNAME:~0,15%
REM Loading Configuration for GPO-Installer
Set GPO_CURPATH=%~dp0
If EXIST "%GPO_CURPATH%\InstallZabbixAgentGPO\ConfigGPO.bat" (
    CALL "%GPO_CURPATH%\InstallZabbixAgentGPO\ConfigGPO.bat"
) else (
    echo Missing "%GPO_CURPATH%\InstallZabbixAgentGPO\ConfigGPO.bat" !!!!
    GoTo END
)
If EXIST "%GPO_CURPATH%Config.bat" (
    CALL "%GPO_CURPATH%Config.bat"
) else (
    If "%GPO_LOGGING_ENABLE%" == "YES" (
        Echo %GPO_COMPUTERNAME% - ERROR   - %DATE% - %TIME%: %GPO_CURPATH%Config.bat not found >>%GPO_LOGGING_GLOBAL_LOG_FILE%
    )
    echo Missing "%GPO_CURPATH%Config.bat (for InstallZabbixAgent.bat)" !!!!
GoTo END
)

REM                     ######  ##     ## ########  ######  ##    ##  ######                     
REM                    ##    ## ##     ## ##       ##    ## ##   ##  ##    ##                    
REM                    ##       ##     ## ##       ##       ##  ##   ##                          
REM ####### #######    ##       ######### ######   ##       #####     ######     ####### ####### 
REM                    ##       ##     ## ##       ##       ##  ##         ##                    
REM                    ##    ## ##     ## ##       ##    ## ##   ##  ##    ##                    
REM                     ######  ##     ## ########  ######  ##    ##  ######                     

REM Make a test if Zabbix-Agent is already installed
sc query "Zabbix Agent" >nul 2>&1
If NOT ERRORLEVEL 1 (
    Echo Zabbix-Agent already installed - Install Agent DENIED - Exit Script
	GoTo END
) Else (
    Echo Missing Zabbix-Agent Configuration-File - Install Agent ALLOWED
    Set GPO_INSTALL_AGENT=TRUE
)



REM Show used Configuration
Echo.
Echo Using following Settings:
Echo -------------------------
Set GPO_

REM Make the "Server or Workstation Check
Echo.
Echo Check OS for Server or Workstation
For /f "tokens=*" %%i in ('wmic os get caption ^| findstr /I "Windows"') do (
    If "%GPO_LOGGING_ENABLE%" == "YES" (
        If "%GPO_LOG_POSITIVE%" == "YES" (
            Echo %GPO_COMPUTERNAME% - INFO    - %DATE% - %TIME%: OS-Type: %%i >>%GPO_LOGGING_GLOBAL_LOG_FILE%
        ) else (
            If "%GPO_LOG_NEGATIVE%" == "YES" (
                Echo %GPO_COMPUTERNAME% - INFO    - %DATE% - %TIME%: OS-Type: %%i >>%GPO_LOGGING_GLOBAL_LOG_FILE%
            )
        )
    )
)
wmic os get caption | findstr /I "Server"
If ERRORLEVEL 1 (
    Echo %COMPUTERNAME% is a Windows Workstation
    If "%GPO_INSTALL_WORKSTATION%" == "YES" (
        If "%GPO_LOGGING_ENABLE%" == "YES" (
            If "%GPO_LOG_POSITIVE%" == "YES" (
                Echo %GPO_COMPUTERNAME% - SUCCESS - %DATE% - %TIME%: Type WORKSTATION ALLOWED >>%GPO_LOGGING_GLOBAL_LOG_FILE%
            )
        )
        Set GPO_INSTALL_AGENT=TRUE
        Echo GPO_INSTALL_WORKSTATION is YES - Install Agent ALLOWED
    ) else (
        If "%GPO_LOGGING_ENABLE%" == "YES" (
            If "%GPO_LOG_NEGATIVE%" == "YES" (
                Echo %GPO_COMPUTERNAME% - ERROR   - %DATE% - %TIME%: Type WORKSTATION not ALLOWED >>%GPO_LOGGING_GLOBAL_LOG_FILE%
            )
        )
        Echo GPO_INSTALL_WORKSTATION is NO - Install Agent DENIED - Exit Script
        GoTo END
    )
)else (
    Echo %COMPUTERNAME% is a Windows Server
    If "%GPO_INSTALL_SERVER%" == "YES" (
        If "%GPO_LOGGING_ENABLE%" == "YES" (
            If "%GPO_LOG_POSITIVE%" == "YES" (
                Echo %GPO_COMPUTERNAME% - SUCCESS - %DATE% - %TIME%: Type SERVER ALLOWED >>%GPO_LOGGING_GLOBAL_LOG_FILE%
            )
        )
        Set GPO_INSTALL_AGENT=TRUE
        Echo GPO_INSTALL_SERVER is YES - Install Agent ALLOWED
    ) else (
        If "%GPO_LOGGING_ENABLE%" == "YES" (
            If "%GPO_LOG_NEGATIVE%" == "YES" (
                Echo %GPO_COMPUTERNAME% - ERROR   - %DATE% - %TIME%: Type SERVER not ALLOWED >>%GPO_LOGGING_GLOBAL_LOG_FILE%
            )
        )
        Echo GPO_INSTALL_SERVER is NO - Install Agent DENIED - Exit Script
        GoTo END
    )
)

REM Start the Whitelist Check
If "%GPO_USE_WHITELIST%" == "YES" (
    Echo.
    Echo Check if %COMPUTERNAME% is whitelisted ...
    If EXIST "%GPO_CURPATH%InstallZabbixAgentGPO\Whitelist.txt" (
        findstr /I /B /E "%COMPUTERNAME%" "%GPO_CURPATH%InstallZabbixAgentGPO\Whitelist.txt"
        If ERRORLEVEL 1 (
            If "%GPO_LOGGING_ENABLE%" == "YES" (
                If "%GPO_LOG_NEGATIVE%" == "YES" (
                    Echo %GPO_COMPUTERNAME% - ERROR   - %DATE% - %TIME%: Name not in Whitelist.txt >>%GPO_LOGGING_GLOBAL_LOG_FILE%
                )
            )
            Echo %COMPUTERNAME% is NOT listet at "%GPO_CURPATH%InstallZabbixAgentGPO\Whitelist.txt"
            Echo Install Agent DENIED - Exit Script
            GoTo END
        ) else (
            If "%GPO_LOGGING_ENABLE%" == "YES" (
                If "%GPO_LOG_POSITIVE%" == "YES" (
                    Echo %GPO_COMPUTERNAME% - SUCCESS - %DATE% - %TIME%: Name is in Whitelist.txt >>%GPO_LOGGING_GLOBAL_LOG_FILE%
                )
            )
            Set GPO_INSTALL_AGENT=TRUE
            Echo %COMPUTERNAME% is listet at "%GPO_CURPATH%InstallZabbixAgentGPO\Whitelist.txt"
            Echo Install Agent ALLOWED
        )
    ) else (
        Echo Missing "%GPO_CURPATH%InstallZabbixAgentGPO\Whitelist.txt"
        GoTo END
    )
)

REM Start the Blacklist Check
If "%GPO_USE_BLACKLIST%" == "YES" (
    Echo.
    Echo Check if %COMPUTERNAME% is blacklisted ...
    If EXIST "%GPO_CURPATH%InstallZabbixAgentGPO\Blacklist.txt" (
        findstr /I /B /E "%COMPUTERNAME%" "%GPO_CURPATH%InstallZabbixAgentGPO\Blacklist.txt"
        If ERRORLEVEL 1 (
            If "%GPO_LOGGING_ENABLE%" == "YES" (
                If "%GPO_LOG_POSITIVE%" == "YES" (
                    Echo %GPO_COMPUTERNAME% - SUCCESS - %DATE% - %TIME%: Name is NOT in Blacklist.txt >>%GPO_LOGGING_GLOBAL_LOG_FILE%
                )
            )
            Set GPO_INSTALL_AGENT=TRUE
            Echo %COMPUTERNAME% is NOT listet at "%GPO_CURPATH%InstallZabbixAgentGPO\Blacklist.txt"
            Echo Install Agent ALLOWED
        ) else (
            If "%GPO_LOGGING_ENABLE%" == "YES" (
                If "%GPO_LOG_NEGATIVE%" == "YES" (
                    Echo %GPO_COMPUTERNAME% - ERROR   - %DATE% - %TIME%: Name is in Blacklist.txt >>%GPO_LOGGING_GLOBAL_LOG_FILE%
                )
            )
            Echo %COMPUTERNAME% is listet at "%GPO_CURPATH%InstallZabbixAgentGPO\Blacklist.txt"
            Echo Install Agent DENIED - Exit Script
            GoTo END
        )
    ) else (
        Echo Missing "%GPO_CURPATH%InstallZabbixAgentGPO\Blacklist.txt"
        GoTo END
    )
)

REM                    #### ##    ##  ######  ########    ###    ##       ##                          
REM                     ##  ###   ## ##    ##    ##      ## ##   ##       ##                          
REM                     ##  ####  ## ##          ##     ##   ##  ##       ##                          
REM ####### #######     ##  ## ## ##  ######     ##    ##     ## ##       ##          ####### ####### 
REM                     ##  ##  ####       ##    ##    ######### ##       ##                          
REM                     ##  ##   ### ##    ##    ##    ##     ## ##       ##                          
REM                    #### ##    ##  ######     ##    ##     ## ######## ########                    

REM Call the Installation Script
Echo.
Echo Start Agent-Installation ...
If "%GPO_LOGGING_ENABLE%" == "YES" (
    Echo %GPO_COMPUTERNAME% - INSTALL - %DATE% - %TIME%: Start Installation: %GPO_INSTALLER% >>%GPO_LOGGING_GLOBAL_LOG_FILE%
)
If "%GPO_LOGGING_PER_HOST%" == "YES" (
    %GPO_INSTALLER% > %GPO_LOGGING_PATH%\%COMPUTERNAME%.LOG
) else (
    %GPO_INSTALLER%
)
GoTo END

REM                    ######## ##    ## ########                     
REM                    ##       ###   ## ##     ##                    
REM                    ##       ####  ## ##     ##                    
REM ####### #######    ######   ## ## ## ##     ##    ####### ####### 
REM                    ##       ##  #### ##     ##                    
REM                    ##       ##   ### ##     ##                    
REM                    ######## ##    ## ########                     
:END
Echo.
Echo +---------------------------------------------------------+
Echo ^| Stopping Script InstallZabbixAgentGPO                   ^|
Echo +---------------------------------------------------------+
REM exit 0
REM PAUSE