Created
February 28, 2013 21:37
-
-
Save steve-jansen/5060329 to your computer and use it in GitHub Desktop.
Windows batch script to modify the TCP/IP port binding for the Microsoft Web Deployment Agent Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Name: MsDepSvc.Port.cmd | |
:: Purpose: Modifies the TCP/IP port that the Web Deployment Agent Service | |
:: (MsDepSvc) listens on. Tested on Win7 Enterprise 32-bit. | |
:: Author: [email protected] | |
:: Revision: January 2013 | |
@ECHO OFF | |
SETLOCAL ENABLEEXTENSIONS | |
SETLOCAL ENABLEDELAYEDEXPANSION | |
:: variables | |
SET me=%~n0 | |
SET url= | |
SET port= | |
IF NOT "%~1"=="" ( | |
SET /A port=%~1 | |
) | |
ECHO %me%: Web Deployment Agent Service (MsDepSvc) port change script | |
:: default argument values | |
IF "%port%"=="" ( | |
SET /A port=8172 | |
ECHO %me%: INFO - using default port value of 8172 | |
) | |
SC.EXE query msdepsvc >NUL 2>NUL | |
IF NOT "%ERRORLEVEL%"=="0" ( | |
ECHO %me%: ERROR - MsDepSvc not installed | |
ECHO %me%: exiting | |
EXIT /B 1 | |
) | |
ECHO %me%: stopping MsDepSvc | |
NET STOP msdepsvc >NUL 2>NUL | |
:: check if the default port is set | |
REG.EXE QUERY HKLM\SYSTEM\CurrentControlSet\Services\MsDepSvc\Parameters /v ListenUrl >NUL | |
IF NOT "%ERRORLEVEL%"=="0" ( | |
ECHO %me%: ERROR - MsDepSvc ListenUrl registry key not found | |
REG.EXE QUERY HKLM\SYSTEM\CurrentControlSet\Services\MsDepSvc\Parameters | |
ECHO %me%: exiting | |
EXIT /B 2 | |
) | |
FOR /F "tokens=3" %%I IN ('REG.EXE QUERY HKLM\SYSTEM\CurrentControlSet\Services\MsDepSvc\Parameters /v ListenUrl ^| FINDSTR ListenUrl') DO ( | |
SET url=%%I | |
) | |
ECHO %me%: INFO - MsDepSvc current reservation is "%url%" | |
NETSH.EXE http show urlacl "%url%" >NUL | |
IF NOT "%ERRORLEVEL%"=="0" ( | |
ECHO %me%: ERROR - reservation for "%url%" not found | |
EXIT /B 4 | |
) | |
:: save the existing urlacl properties for User, Listen, Delegate, and SDDL | |
FOR /F "tokens=1,* delims=: " %%A IN ('NETSH.exe http show urlacl %url% ^| FINDSTR "User Listen Delegate SDDL"') DO ( | |
SET URLACL.%%A=%%B | |
) | |
IF NOT DEFINED URLACL.User ECHO %me%: Failed to read the exising URLACL setting for User &&GOTO :ERROR | |
IF NOT DEFINED URLACL.Listen ECHO %me%: Failed to read the exising URLACL setting for Listen &&GOTO :ERROR | |
IF NOT DEFINED URLACL.Delegate ECHO %me%: Failed to read the exising URLACL setting for Delegate &&GOTO :ERROR | |
IF NOT DEFINED URLACL.SDDL ECHO %me%: Failed to read the exising URLACL setting for SDDL &&GOTO :ERROR | |
ECHO %me%: updating MsDepSvc to listen on port %port% | |
REG.EXE ADD HKLM\SYSTEM\CurrentControlSet\Services\MsDepSvc\Parameters /v ListenUrl /t REG_SZ /f /d "http://+:%port%/MSDEPLOYAGENTSERVICE/" | |
ECHO %me%: deleting the existing reservation for MsDepSvc | |
NETSH.EXE http delete urlacl "%url%" || GOTO :ERROR | |
ECHO %me%: adding the port %port% reservation for MsDepSvc | |
NETSH.EXE http add urlacl url=http://+:%port%/MsDeployAgentService/ user="%URLACL.User%" listen="%URLACL.Listen%" delegate="%URLACL.Delegate%" SDDL="%URLACL.SDDL%" || GOTO :ERROR | |
ECHO %me%: starting MsDepSvc | |
NET START msdepsvc >NUL 2>NUL | |
ECHO %me%: process info for MsDepSvc | |
QUERY.EXE PROCESS MSDEPSVC.EXE | |
ECHO. | |
ECHO %me%: port bindings for MsDepSvc | |
NETSTAT.EXE -a -n -o | FINDSTR /R "TCP.*:%port%.*LISTENING Proto" | |
ECHO. | |
ECHO %me%: finished | |
:END | |
ENDLOCAL | |
ECHO ON | |
@EXIT /B 0 | |
:ERROR | |
ECHO %me%: ERROR - exiting with errorlevel %ERRORLEVEL% | |
ECHO ON | |
@EXIT/B %ERRORLEVEL% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment