scrip an aditional binding for IIS site to include www.

Hi all, I have 300 sites in IIS they all have a binding set up as a sub-domain  i.e.

benssite.mydomain.com

this works great for each of the 300 sites

I want to resolve these sites using www.benssite.mydomain.com too

So I want to copy the existing binding and add www. to the beginning and then add this as an additional binding.

so each site has 2 binings:

benssite.mydomain.com
www.benssite.mydomain.com

Is this possible using a script in IIS
bcoussensAsked:
Who is Participating?
 
Brad HoweConnect With a Mentor DevOps ManagerCommented:
Hi,

Created a simple CMD script to automate this for you.

Requirements:
1. TXT File (Site_and_binding.txt) In the following format (SITE,NEWBINDING)
         SITE1,www.site1.com
         SITE2,www.site2.com
         SITE3,www.site3.com
         SITE4,www.site4.com

You can use
   %windir%\system32\inetsrv\AppCmd list sites /text:name > sitenames.txt
to get ALL the SITENAMES

BINDINGS....A little harder if you have multiple.

Create the script binding.CMD

@echo off
FOR /F "tokens=1,2 delims=," %%G IN (Site_and_binding.txt) DO echo ^%windir%\system32\inetsrv\AppCmd set SITE /site.name: %%G /+bindings.[protocol='http',bindingInformation='*:80:%%H']

Then Execute the script    
   CMD> binding.CMD > appcmds.txt

APPCMD.txt OUTPUT SAMPLE
C:\Windows\system32\inetsrv\AppCmd set SITE /site.name: SITE1 /+bindings.[protocol='http',bindingInformation='*:80:www.site1.com']
C:\Windows\system32\inetsrv\AppCmd set SITE /site.name: SITE2 /+bindings.[protocol='http',bindingInformation='*:80:www.site2.com']
C:\Windows\system32\inetsrv\AppCmd set SITE /site.name: SITE3 /+bindings.[protocol='http',bindingInformation='*:80:www.site3.com']
C:\Windows\system32\inetsrv\AppCmd set SITE /site.name: SITE4 /+bindings.[protocol='http',bindingInformation='*:80:www.site4.com']

Then simple cut and past those into command line and wait.

hope it helps give you some ideas.
Cheers,
Hades666
0
 
gaurav05Commented:
Hi,

Command Line
To add a binding to a site, use the following syntax:

appcmd set site /site.name: string /+bindings.[protocol=' string ',bindingInformation=' string ']

The variable site.name string is name of the site to which you want to add a binding. The variable protocol string is the protocol that you want to use, and the variable bindingInformation string is the combination of IP address, port, and host header.

For example, to configure a site named contoso to have an HTTPS binding for all IP addresses, on port 443, without a host header, type the following at the command prompt, and then press ENTER:

appcmd set site /site.name: contoso /+bindings.[protocol='https',bindingInformation='*:443:']

For more information about Appcmd.exe, see Appcmd.exe (IIS 7).

Configuration
The procedure in this topic affects the following configuration elements:

<bindings> under <site> element
0
 
gaurav05Commented:
Hi,

To add a host header to a Web site's binding, use the following syntax:

appcmd set site /site.name: string /bindings.[protocol=' string ',bindingInformation=' string '].bindingInformation: string

The variable site.name string is name of the site to which you want to add a host header. The variable [protocol='string',bindingInformation='string'] is the existing binding to which you want to add a host header, and bindingInformation string is the new binding with host header.

For example, to configure a site named contoso with an existing HTTPS binding for all IP addresses, on port 443, without a host header to have a host header named marketing, type the following at the command prompt, and then press ENTER:

appcmd set site /site.name: contoso /bindings.[protocol='https',bindingInformation='*:443:'].bindingInformation:*:443: marketing

For more information about Appcmd.exe, see Appcmd.exe (IIS 7).

Configuration
The procedure in this topic affects the following configuration elements:

<bindings> under <site> element
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 
bcoussensAuthor Commented:
Yes I found this online too but this does not copy the existing binding and create an additional one using the current one adding www to the beginning.

Any other ideas?
0
 
Brad HoweDevOps ManagerCommented:
So each site ONLY has 1 binding at the moment?

if so, you could create a simple Binding.CMD script that essentially grabs the sites current bindings, pipes to a TXT and then reads the TXT and pipes it back into the site settings.

1. Get ALL Sites Bindings and SITE text:NAME into a tmp TXT file.
    eg :  Ben Site        bensite.domain.com
2. Take the first site name and assign to a string.
3. Take the first binding and assign to a string.
4. Concat the binding_string with www.
5. Execute appcmd set site /+bindings with the sitename_string and binding_string.
6. Loop until TXT is complete.

Just a thought. sounds like a fun script IF all your sites are only with 1 binding currently. Have more will cause more work.

-Hades666
0
 
bcoussensAuthor Commented:
ok I think i follow, so first I use    

%windir%\system32\inetsrv\AppCmd list sites /text:name > sitenames.txt from the command line to get an output text file with all site names

this outputs a text file with all the sitenames, I then edit this and add next to each sitename the new www.sitename.domain.com binding (with a comma between site name and new binding)

I save this edited file as Site_and_binding.txt

then i create a script called binding.CMD  inserting:
@echo off
FOR /F "tokens=1,2 delims=," %%G IN (Site_and_binding.txt) DO echo ^%windir%\system32\inetsrv\AppCmd set SITE /site.name: %%G /+bindings.[protocol='http',bindingInformation='*:80:%%H']

Then Execute the script    
   CMD> binding.CMD > appcmds.txt

(i'm not sure about this bit,  where does appcmds.txt com from??  is the outputted from running the script?  or do i need to create this first?  )

Many thanks for your help.

Ben
0
 
Brad HoweDevOps ManagerCommented:
Yes,

When you execute binding.cmd > appcmd.txt

The FOR LOOP essentially reads the TXT and generates the proper appcmd syntax to submit via CMD. So after you execute the CMD script, open the file appcmd.txt (which is in the folder you execute the binding.cmd from) and then simple select ALL and paste in commandline.

IIS will then add the bindings.

Cheers,
Hades666
0
 
bcoussensAuthor Commented:
Many thanks, this works a treat I now have 300 sites with updated host headers!!

Ben
0
 
bcoussensAuthor Commented:
great solution and also attention to detail and communications
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.