Skip to content

Instantly share code, notes, and snippets.

@ProNotion
Last active November 30, 2016 19:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProNotion/6a269396b56bdfc108bd to your computer and use it in GitHub Desktop.
Save ProNotion/6a269396b56bdfc108bd to your computer and use it in GitHub Desktop.
Setup and start IISExpress site then open an ngrok tunnel to it and launch in default browser
rem *********************************************************************
rem This bat file will SETup a site in IIS Express, open a tunnel with ngrok and launch
rem the site in the users default web browser
rem
rem tunnel.bat Path SiteName
rem - Path The path to the root folder for the site
rem - SiteName The name of the site to use for the tunnel and in IIS Express site
rem ***********************************************************************
@echo off
:: Set the appcmd.exe path to a variable for later use
SET APPCMD="%ProgramFiles%\IIS Express\appcmd.exe"
:: Get the site name from the second argument passed to the batch file
SET SITENAME=%2
SET IPADDRESS = 127.0.0.1
SET ip_address_string="IPv4 Address"
:: Get the LAN IP address so we can use it to create the tunnel
FOR /f "usebackq tokens=2 delims=:" %%f IN (`ipconfig ^| findstr /c:%ip_address_string%`) DO (SET ipaddress=%%f)
:::: normalize folder
SET folder=%~dp0Umbraco
IF %folder:~-1%==\ SET folder=%folder:~0,-1%
:: random number from 8000 to 9999
SET /a PORT=(%random%)*1999/32767+8000
:: Check if there is already a site in the IIS Express config with the same name
%APPCMD% list sites |findstr "SITE ""%SITENAME%""" 2>&1 && (SET SITE_EXISTS="1") || (SET SITE_EXISTS="0")
:: If there is already a site in the IIS Express Config with the same name
:: we need to delete it so we can add it back with the new port number
IF %SITE_EXISTS% =="1" (
%APPCMD% DELETE site /site.name:%SITENAME%
)
:: Allow users to access the new url
netsh http add urlacl url=http://%siteName%.ngrok.io:%port%/ user=everyone
:: Add a new site definition in the IIS Express config that maps to the specified folder using the new random port
%APPCMD% ADD site /site.name:%SITENAME% /+bindings.[protocol='http',bindingInformation='*:%PORT%:%SITENAME%.ngrok.io'] /physicalPath:"%folder%"
:: Start the IIS Express web server using the random port
start "IIS Express :%PORT%" cmd /C " "%ProgramFiles%\IIS Express\iisexpress.exe" /site:%SITENAME% /systray:true "
:: Open the site in your browser after a 3 second delay to give IIS Express time to start and the ngrok tunnel to open
start /B "" cmd /c timeout /t 3 ^& start /B "" http://%siteName%.ngrok.io
:: Open the ngrok tunnel with our specified subdomain
ngrok http -subdomain=%SITENAME% %ipaddress%:%PORT%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment