Script IIS7 & 7.5


I have the following code:

@echo off 
setlocal EnableDelayedExpansion 

REM Get parameters from user if they're not specified 
if [%1]==[] set /P directory="Enter name of directory/site: " 
if [%2]==[] set /P sitenum="Enter site number: " 

mkdir C:\websites\%directory%

REM Create site in IIS 
%systemroot%\system32\inetsrv\appcmd add site /name:"%directory%" /id:%sitenum% /physicalPath:"C:\websites\%directory%" /bindings:http/* 

REM Attempt to migrate config to IIS7 stylee 
%SystemRoot%\system32\inetsrv\appcmd migrate config "%directory%/" 

REM Start new site 
%SystemRoot%\system32\inetsrv\appcmd start site "%directory%" 

echo site "%directory%" now running at 

REM interactive mode 
if [%1]==[] (if [%2]==[] ( 

Open in new window

Problem I face:

It does not create the folder directory
I want the site ID to be added automatically rather than a user entering the ID
I also get an error:
Enter name of directory/site: test10
Enter site number: 23
SITE object "test10" added
APP object "test10/" added
VDIR object "test10/" added
Successfully migrated section "system.web/httpModules".
Successfully migrated section "system.web/httpHandlers".
ERROR ( message:The W3SVC service is not available - try starting the service first. )
site "ari10" now running at
Press any key to continue . . .

Any help will be appreciated. Trying to get this process automated.


Who is Participating?
arnoldConnect With a Mentor Commented:
There are many examples of scripts that perform these tasks.  You would need to combine the various available scripts into a single one that does what you need.
i.e script that creates folders and sets appropriate permissions
script that ..........
First, the site number is a unique identifier.  Your script lacks testing to make sure a site number a user might enter is not already in use.
Similarly for any input you request.

Is IIS running?
W3svc refers to the IIS not running when you are trying to start a site.
introluxAuthor Commented:
So there is no way to automate the site number apart from having a check that the ID number is in use?
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

If you do not specify the ID, the system will randomly assign one.
Any scripting should use testing and validation to avoid data corruption.

vbscript/powershell might be a more robust than the batch file using appcmd.

If I am not mistaken, vbscript and possible powershell, have methods/tie into such that you can configure/manage/ the IIS parameters.

Instead of prompting for information, you could use a CSV/excel spreadsheet/database from which your script will read data, and then add/activate, deactivate, delete sites.
It seems you are in need of further assistance.
If you would provide detail.........
introluxAuthor Commented:
Basically looking to generate a script that we would possibly run  to create the following:

Create folder
Create sites and set correct binding
Create Application Pool
Set Application Pool .NET version (If possible)
Start web site

Let me know if possible.


It is possible, using vbscript, powershell or c# might be a better approach as they have hooks to interact and verify response as compared to using batch processing when you have to call different commands to do the different things.
introluxAuthor Commented:
Powershell script
introluxAuthor Commented:
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.