How to Deploy a Batch Script for Updating Configuration Files via GPO

How to Deploy a Batch Script for Updating Configuration Files via GPO

KB ID: 115918

Overview

This article provides instructions for creating a batch script to update a configuration file with a new IP address, deploying it via Group Policy Object (GPO), and ensuring successful execution on target machines.

Batch Script: Update IP Address in Configuration File

Script File Content

NotesNote: Save the following script as a .bat file (e.g., update_ip.bat)

@echo off
REM Define the file path and new IP address set "filePath=C:\Program Files\Vembu\VembuIntegrationService\conf\ephost.conf" set "newIP=192.168.1.100" REM Generate a timestamp in the format DayMMDD_YYYY_HHMMSS for /f "tokens=1-4 delims=/-:., " %%a in ('echo %date% %time%') do ( REM Get the day abbreviation for /f "tokens=1 delims= " %%x in ('echo %date%') do set "dayAbbreviation=%%x" REM Extract the month, day, year, and time components set "monthDay=%%b%%c" set "year=%%a" set "timePart=%%d" ) REM Extract seconds from %time% for /f "tokens=1-2 delims=:" %%a in ("%time%") do set "seconds=%%b" REM Combine components to create the timestamp set "timestamp=%dayAbbreviation%%monthDay%_%year%_%timePart%%seconds%" REM Create a backup file name with the timestamp set "backupFile=%filePath%.%timestamp%.bak" REM Check if the original file exists if not exist "%filePath%" ( echo File not found: "%filePath%" pause exit /b 1 ) REM Create a backup of the original file copy "%filePath%" "%backupFile%" >nul if errorlevel 1 ( echo Failed to create a backup of the file. pause exit /b 1 ) echo Backup of the original file created: "%backupFile%" REM Replace the content of the file with the new IP ( echo %newIP% ) > "%filePath%" if errorlevel 1 ( echo Failed to update the file with the new IP address. pause exit /b 1 ) REM Success message echo Successfully replaced the IP in %filePath% with %newIP%. pause

Input Parameters

  • filePath: Path to the configuration file to be updated (e.g., C:\Program Files\Vembu\VembuIntegrationService\conf\ephost.conf).
  • newIP: The new IP address to replace the existing content (e.g., 192.168.1.100).

Deploying the Batch Script via GPO

1. Place the Script on a Shared Network Location

  1. Save the batch file to a shared folder accessible by target machines (e.g., \\ServerName\SharedFolder\update_ip.bat).
  1. Ensure the shared folder has Read/Execute permissions for the required machines or users.

2. Create a Group Policy Object (GPO)

  1. Open the Group Policy Management Console (GPMC):
    Press Win + R, type gpmc.msc, and press Enter.
  2. Right-click the Organizational Unit (OU) containing the target computers.
  3. Select Create a GPO in this domain, and Link it here.
  4. Name the GPO (e.g., Batch Script Deployment).

3. Add the Batch Script to Startup or Logon

  1. Right-click the newly created GPO and select Edit.
  2. Navigate to the relevant section based on your deployment method:
For computers:
Computer Configuration → Policies → Windows Settings → Scripts (Startup/Shutdown) → Startup.
For users:
User Configuration → Policies → Windows Settings → Scripts (Logon/Logoff) → Logon.
  1. Double-click Startup (or Logon) and click Add.
  2. In the Add a Script dialog, browse and select the batch file (e.g., \\ServerName\SharedFolder\update_ip.bat).

4. Apply Permissions

Ensure target machines or users have Read/Execute permissions for the network share where the script is stored.

5. Apply and Test the Policy

  1. On a target machine, run the following command to apply the policy:
gpupdate /force
  1. Reboot the machine (if configured for startup scripts) or log off and log back in (for logon scripts).

Conclusion

By following these steps, you can efficiently deploy the batch script to update configuration files across multiple machines using GPO. This approach ensures a seamless update process while maintaining a backup of the original configuration.