diff --git a/Windows/ZabbixAgent/README.md b/Windows/ZabbixAgent/README.md index f16746dd87ef4aa0bd04a4a2fe4d7eceeecfeea9..d2b11238c5120d0afd32e425935c62cd58613e32 100644 --- a/Windows/ZabbixAgent/README.md +++ b/Windows/ZabbixAgent/README.md @@ -1,16 +1,16 @@ # Some scripts to use with Zabbix Agent ## Overview -This repository contains scripts to +This repository contains scripts to: +- Check if Zabbix Agent 2 is running or not, if not copy the configuration file from the network and replace it in the installation folder and start up the Zabbix Agent 2 service - Remove all remnants of Zabbix Agent and Zabbix Agent 2 -- Check if Zabbix Agent 2 is running or not, if not copy the configurartion file from the network and replace it in the installation folder and start up the Zabbix Agent 2 service ## Files -- `CheckAndStartZabbixAgent2.ps1`: Script to check if Zabbix Agent 2 is running or not, if not then copy the configurartion file from the network and replace it in the installation folder and start the Zabbix Agent 2 service. -- `RemoveZabbix.ps1`: Script to emove all remnants of Zabbix Agent and Zabbix Agent 2. +- `CheckAndStartZabbixAgent2.ps1`: Script to check if Zabbix Agent 2 is running or not, if not then copy the configuration file from the network and replace it in the installation folder and start the Zabbix Agent 2 service. +- `RemoveZabbix.ps1`: Script to remove all remnants of Zabbix Agent and Zabbix Agent 2. ## Usage -1. Download `CheckAndStartZabbixAgent2.ps1` and create a new scheduled task run run this regurarly. +1. Download `CheckAndStartZabbixAgent2.ps1` and create a new scheduled task to run this regularly. 2. Download and run `RemoveZabbix.ps1` to remove all remnants of Zabbix Agent and Zabbix Agent 2. ## Prerequisites @@ -20,49 +20,72 @@ This repository contains scripts to ### Via an automatic One-Liner, that Downloads, installs and clean up This one-liner download and install does the following: - 1. Sets the ExecutionPolicy - 2. Stop the "Zabbix Agent 2" service. - 3. Set the path where to store the script. - 4. Download the script file to the path. - 5. Create a scheduled task. - -1. To run the script, you need to temporarily change the PowerShell execution policy to allow script execution. You can do this by using the following one-liner code: +1. Sets the ExecutionPolicy +2. Stops the "Zabbix Agent 2" service. +3. Sets the path where to store the script. +4. Downloads the script file to the path. +5. Creates a scheduled task. +6. Starts the newly created scheduled task. - ```powershell - Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force; Stop-Service -Name "Zabbix Agent 2"; $scriptPath = "C:\Windows\System32\Tasks\CheckAndStartZabbixAgent2.ps1"; Invoke-WebRequest -Uri "https://gitlab.liu.se/andli86/snippets/-/raw/master/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1" -OutFile $scriptPath; schtasks /create /tn "CheckAndStartZabbixAgent2" /tr "powershell.exe -File $scriptPath" /sc hourly /mo 3 /ru SYSTEM; - ``` +To download, save, and execute the script, you need to temporarily change the PowerShell execution policy to allow script execution. You can do all this by using the following one-liner code in an elevated PowerShell session: + +```powershell +Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force; Stop-Service -Name "Zabbix Agent 2"; $scriptPath = "C:\Windows\System32\Tasks\CheckAndStartZabbixAgent2.ps1"; Invoke-WebRequest -Uri "https://gitlab.liu.se/andli86/snippets/-/raw/master/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1" -OutFile $scriptPath; schtasks /create /tn "CheckAndStartZabbixAgent2" /tr "powershell.exe -File $scriptPath" /sc hourly /mo 3 /ru SYSTEM; schtasks /run /tn "CheckAndStartZabbixAgent2"; +``` -## Manual Installation -1. **Download the script file to the computer, I prefer to the `C:\Windows\System32\Tasks\` folder**: +### Manual Installation +1. **Download the script file to the computer, preferably to the `C:\Windows\System32\Tasks\` folder**: ``` https://gitlab.liu.se/andli86/snippets/-/raw/master/Windows/ZabbixAgent/CheckAndStartZabbixAgent2.ps1 ``` -2. **Configure a Scheduled Task**: Ensure the execution policy allows the script to run. +2. **Configure a Scheduled Task**: + - Open Task Scheduler. + - Create a new task named "CheckAndStartZabbixAgent2" (or any name you prefer). + - Set the task to run the script `CheckAndStartZabbixAgent2.ps1` at the desired interval (e.g., every three hours). + - Ensure the execution policy allows the script to run. + +### Manually Start the Scheduled Task +1. **From the command line in an elevated PowerShell session**: + ```powershell + schtasks /run /tn "CheckAndStartZabbixAgent2" # or whatever you named the task + ``` + +2. **From Task Scheduler**: + - Right-click the task you created and select "Run". -3. **Test run the script** by manually starting the scheduled task, perhaps with the Zabbix Agent 2 service both Started and Stopped. +### Run the Script Manually in PowerShell +1. Open an elevated PowerShell session. +2. Navigate to the directory where the script is saved. +3. Run the script: + ```powershell + .\CheckAndStartZabbixAgent2.ps1 + ``` +### Script Details When you run the script `CheckAndStartZabbixAgent2.ps1`, it will perform the following actions: - 1. **Retrive the Domain name**: ... - 2. **Set the source path for the Zabbix Agent 2 configuration file**: ... - 3. **Set the target path for the Zabbix Agent 2 configuration file**: ... - 4. **Check if Zabbix Agent 2 service is running**: Output the Service status... - - **Running**: Output "Zabbix Agent 2 is already running. No action needed.". And EXIT the script. - - **Stopped**: Call two functions "Copy-ZabbixConfig" and "Start-ZabbixAgent2" - 6. **Copy-ZabbixConfig function**: Copies the zabbix_agent2.conf file from the source path to the target path. - 7. **Start-ZabbixAgent2 function**: Starts the "Zabbix Agent 2" service. - 8. **EXIT**: Exit the script, since we reached the end of the file. - - The script provides informative messages about its actions, such as: - - If the Zabbix Agent 2service is running or is stopped. - - Copies the the zabbix_agent2.conf file from the source path to the target path. - - Starts the "Zabbix Agent 2" service, if it was stopped. - -## Usage -The scheduled task `CheckAndStartZabbixAgent2` runs the following script `C:\Windows\System32\Tasks\CheckAndStartZabbixAgent2.ps1` every three hours. +1. **Retrieve the Domain name**: Dynamically retrieves the domain name of the computer. +2. **Get the computer name**: Retrieves the computer name to determine the appropriate configuration file. +3. **Set the source path for the Zabbix Agent 2 configuration file**: Determines the path to the configuration files on the network. +4. **Set the target path for the Zabbix Agent 2 configuration file**: Specifies the local path where the configuration file should be placed. +5. **Check if Zabbix Agent 2 service is running**: Outputs the service status. + - **Running**: Outputs "Configuration files are identical. No action needed." if the files match, or updates the configuration if they don't match. + - **Stopped**: Copies the configuration file and starts the Zabbix Agent 2 service. +6. **Copy-ZabbixConfig function**: Copies the configuration file from the source path to the target path. +7. **Start-ZabbixAgent2 function**: Starts the "Zabbix Agent 2" service. +8. **Stop-ZabbixAgent2 function**: Stops the "Zabbix Agent 2" service if needed. +9. **EXIT**: Exits the script after completing the necessary actions. + +The script provides informative messages about its actions, such as: +- If the Zabbix Agent 2 service is running or stopped. +- Copies the configuration file from the source path to the target path. +- Starts the "Zabbix Agent 2" service if it was stopped. + +## Scheduled Task Details +The scheduled task `CheckAndStartZabbixAgent2` runs the script `C:\Windows\System32\Tasks\CheckAndStartZabbixAgent2.ps1` every three hours. ## License -This project is licensed under the MIT License. And is provided as-is without any warranty. Use at your own risk. +This project is licensed under the MIT License and is provided as-is without any warranty. Use at your own risk. ## Author Andreas Lindemark, Linköping University