I'm writing a short script to push out a new line to everyone in my domains hosts file in a GPO - don't ask why we're not doing this via DNS, the reasons are both fun and extensive :)
For the most part, this is simple, however I'm hitting upon a somewhat obstinate problem with comparing strings in variables, namely, it doesn't seem to do it. The strings don't seem to match, despite the fact if I echo out the strings they are identical. With the strings not matching, it just keeps adding a line to the end of the hosts file ad infinitum.
For reference, this is the process I want the script to follow:
Set compare value -> Search hosts file for existing entries and save output to variable -> Compare variables if match, do nothing, else
@echo off SET compare=10.1.1.1 test.testing.com FOR /F "usebackq delims==`" %%A IN (`findstr /c:"10.1.1.1 test.testing.com" %SystemRoot%\system32\drivers\etc\hosts`) do SET search=%%A if NOT "%search%"=="%compare%" echo Do Something >> output.txt
If I change:
It works fine. If I echo the output of %search% and %compare% I get:
10.1.1.1 test.testing.com 10.1.1.1 test.testing.com