Dynamically assign to group based on IP address

Putting this here in case anyone else is looking for a similar solution.

Our company is spread across several physical locations, all VLAN trunked with each location or building carrying its own subnets. In order to organize the hosts in the console, we are grouping by location, and the main campus by building. With several hundred hosts to deploy, I needed a way to automatically place into the groups. 

Below is the script Im using in a SCCM task sequence after uninstall of current version, and install of latest. You'll need to change the IP addresses to your range, and the numbers after the IP address represent the group ID. To get your group ID, go into the console, click on the group you need the ID of, then in the browser's address bar, copy the number at the end of the address after teamviewer.com/nav/home/buddies/g/

You'll need a copy of PSExec in the same location this batch file is running from. Using start /wait for the reload has not been reliable in task sequences, so I replaced with PSExec. You can also remove the reload and waitfor, and just exit the script there, then use 2 command line steps with conditions to call the reload, one for 32 bit, and another for 64.

You'll also need to replace XXXXXXX-XXXXXXXXXXXXXXX in two locations with your API key. The TeamViewer_Settings_x64 is a copy of the x86 reg file, with each key [HKEY_LOCAL_MACHINE\SOFTWARE\TeamViewer] changed to [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\TeamViewer]

Ive noticed some odd behaviour if Policies are used with custom hosts, the policy will disappear or show as Not removed (Policy Name). Ive decided to not use TeamViewer policies in this scenario, as the registry import should effectively do the same thing. 

@echo off
call :checkcall 10.0.1. 123456789
call :checkcall 10.0.2. 234567891
call :checkcall 10.0.3. 345678912
call :checkcall 10.0.4. 456789123
call :checkcall 10.0.5. 567891234
goto end

:checkcall
ipconfig | find /c "%1" >NUL 2>NUL
if "%errorlevel%"=="1" goto :EOF
if "%errorlevel%"=="0" set group=g%2

echo Group ID is %group%
:end

echo Checking for TeamViewer ID

REM Check Windows Architecture
IF NOT EXIST "C:\Program Files\TeamViewer\TeamViewer.exe" GOTO loop64

REM Get 32 Bit Client ID
:loop32
reg query HKLM\Software\TeamViewer /v ClientID
IF "%ERRORLEVEL%" == "0" (GOTO assign32) else (GOTO loop32)

REM Get 64 Bit Client ID
:loop64
reg query HKLM\Software\WOW6432Node\TeamViewer /v ClientID
IF "%ERRORLEVEL%" == "0" (GOTO assign64) else (GOTO loop64)

REM 32 Bit Account Assignment
:assign32
echo TeamViewer ID acquired. Importing settings and assigning to group
start /wait reg.exe IMPORT TeamViewer_Settings_x86.reg
IF %ERRORLEVEL% EQU 0 Echo Registry import successful
start /wait TeamViewer_Assignment.exe -apitoken XXXXXXX-XXXXXXXXXXXXXXXXXXXX -datafile "C:\Program Files\TeamViewer\AssignmentData.json" -groupID %group% -allowEasyAccess=true
IF %ERRORLEVEL% EQU 0 Echo Account assignment successful
GOTO reload32

REM 64 Bit Account Assignment
:assign64
echo TeamViewer ID acquired. Importing settings and assigning to group
start /wait reg.exe IMPORT TeamViewer_Settings_x64.reg
IF %ERRORLEVEL% EQU 0 Echo Registry import successful
start /wait TeamViewer_Assignment.exe -apitoken XXXXXXX-XXXXXXXXXXXXXXXXXXXX -datafile "C:\Program Files (x86)\TeamViewer\AssignmentData.json" -groupID %group% -allowEasyAccess=true
IF %ERRORLEVEL% EQU 0 Echo Account assignment successful
GOTO reload64

REM Reload 32 bit
:reload32
psexec -accepteula -s "C:\Program Files\TeamViewer\TeamViewer.exe" --ReloadSettings
IF %ERRORLEVEL% EQU 0 Echo TeamViewer reload successful
waitfor /t 10 SomethingThatIsNeverHappening
IF %ERRORLEVEL% EQU 0 Echo Timeout complete, exiting
EXIT

REM Reload 64 bit
:reload64
psexec -accepteula -s "C:\Program Files (x86)\TeamViewer\TeamViewer.exe" --ReloadSettings
IF %ERRORLEVEL% EQU 0 Echo TeamViewer reload successful
waitfor /t 10 SomethingThatIsNeverHappening
IF %ERRORLEVEL% EQU 0 Echo Timeout complete, exiting
EXIT