Automating IIS from the Command-Line/Scripting Webcast

I gave a presentation recently on automating IIS from the command-line (you can check it out at: https://www.livemeeting.com/cc/mseventsbmo/view?id=1032352182&role=attend&pw=D80FDE8D). I focused on how to manage IIS using PowerShell, WMI, and AppCmd. I talked a lot about Server Core as well, and the management options for IIS with Server Core.

Here are the questions from that presentation with answers in case you are interested.

Mika: Hi, I only just joined and might have missed something :( Is the demo script available somewhere?

Mai-lan: I’ve attached a couple of the appcmd batch files that I showed during our presentation.

Salam: Is IIS 7 going to support sFTP, I saw some SSL icons on FTP

Mai-lan: IIS is shipping a new secure FTP server as a downloadable module from IIS.NET at the end of February. If you download the module to your Windows Server 2008 installation, it will integrate into your existing installation, including the management of the server through IIS7 Manager.

salam: 1st Q, how come you were able to create a 2nd web site on the same port and IP where the default one is running without getting an error?

Mai-lan: You can do this by having two different host headers. Host headers provide for an alias so that they can be differentiated and processed by the right pool. You have one IP address per server and thousands of host-header based web-sites. You can copy and paste the following lines into a batch file or just paste them in an elevated command-line:

REM CREATE SITE 1 DIRECTORY
Md %systemdrive%\inetpub\wwwroot\site1

REM CREATE DEFAULT.HTM IN SITE DIRECTORY
echo 'this is site 1' >> %systemdrive%\inetpub\wwwroot\site1\default.htm

REM ADD SITE1 VIA APPCMD
%windir%\system32\inetsrv\appcmd add site -name:site1 -physicalPath:%systemdrive%\inetpub\wwwroot\site1 -bindings:http/*:80:site1

REM ADD HOST HEADER TO HOSTS FILE SO THAT THE NAME 'SITE1" CAN BE RESOLVED BY DNS
echo 127.0.0.1    site1 >> %windir%\system32\drivers\etc\hosts

REM DO THE SAME FOR SITE 2
Md %systemdrive%\inetpub\wwwroot\site2
echo 'this is site 2' >> %systemdrive%\inetpub\wwwroot\site2\default.htm
%windir%\system32\inetsrv\appcmd add site -name:site2 -physicalPath:%systemdrive%\inetpub\wwwroot\site2 -bindings:http/*:80:site2
echo 127.0.0.1    site2 >> %windir%\system32\drivers\etc\hosts

REM REQUEST SITE1 AND SITE 2 VIA IE
start
http://site1
start http://site2

Chris: would you please show again how to load the assembly, and get the iis object?

Mai-lan: Sure. Here it is:

# Load assembly explicitly

$mwaPath = join-path $env:systemroot system32\inetsrv\microsoft.web.administration.dll

$mwa = [System.Reflection.Assembly]::LoadFrom($mwaPath)

# create configuration object

$cfg = new-object Microsoft.Web.Administration.ServerManager

salam: Did you create a virtual directory or a new web site? if it is a VD, then I have no problem, but if it was a new web site, then please explain

Mai-lan: This is a good point. If you create a new site using MWA, you have to create a new virtual directory since that is a required parameter. For example, here’s the syntax: Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite"). Here is a good article about using MWA with examples: http://www.iis.net/articles/view.aspx/IIS7/Extending-IIS7/Using-Microsoft-Web-Administration/How-to-Use-Microsoft-Web-Administration?Page=2. I was using AppCmd in the demo. I was creating a site, but I was NOT actually creating a site that would really work because in the demo I didn’t add the physical path and the bindings. When you create a site using AppCmd, you have to use the syntax of appcmd add site /name:string /id:uint /physicalPath:string /bindings:string to really add a site that works. Note that AppCmd will not throw you an error if you create a site without physical path and bindings. You will create a site (like I did in the demo) but it doesn’t have the required information to be fully functional. I’ll demo it with all parameters next time, I don’t want it to be misleading.

tom: Can you describe what are SCRegEdit.wsf, Netsh/Netdom?

Mai-lan: They are Server Core tools for management (not specific to IIS). SCRegEdit.wsf is included in Server Core to more easily enable automatic updates, enable Terminal Server, Remote Admin Mode, enable remote IPSec Monitor management and configure DNS SRV record weight and priority. Netdom is there to let you join a domain. You can learn more at: http://technet2.microsoft.com/windowsserver/longhorn/en/library/bab0f1a1-54aa-4cef-9164-139e8bcc44751033.mspx?mfr=true. There is also a newsgroup on Server Core: http://forums.microsoft.com/TechNet/ShowForum.aspx?ForumID=582&SiteID=17.

Mika: Thanks for a concise summary on IIS admin automation! I'm looking forward on http://blogs.iis.net/mailant

Arthur: not sure if this is the right forum, but related to automated install, from what I have seen IIS will only install on the C drive with IIS 7 and we cannot point the install to another drive, do you know if there is a reason for that?

Mai-lan: INETPUB goes to C drive and there is no way to override that. Sorry...

chris: Excelent Job! Thank You

Mai-lan: No problem, thanks for coming.

 chris: When I try to run appcmd on my server the program is not found. Could it that I need a later version of 2008? (Server Longhorn)Mai-lan: AppCmd is located in the %systemroot%\system32\inetsrv\ directory. Because it is not path of the PATH automatically, you need to use the full path to the executable. Alternatively, you canto simplify all the dir switching, you can add the inetsrv directory to the path on your machine so that you can access AppCmd.exe directly from any location.

 

 

5 Comments

  • Mai-Ian,
    Thanks for the great demo and session!
    I have a quick question: You mentioned several times during the session that one can use Microsoft.Web.Administration to manage remote IIS servers and especially IIS on server core. Can you give an example of creating a connection to a remote server using Web.Administration with PowerShell?
    Thanks!
    /Dung

  • I wanted to use AppCmd Utility but it is not available on Windows 2003 OS at C:\WINDOWS\system32\inetsrv location. Do I need to install some additional packages for this to get installed.

  • Hi Mai-lan,

    I watched your webcast last night looking for tips and tricks on scripting IIS7 configuration on a Server Core installation. Very helpful for providing more background information that I needed. Now here's my situation...

    I've been working to setup an IIS7 shared configuration demonstration for the Demo Showcase that goes out to the field and partners alike. In this scenario I used System Center Essentials management pack to monitor a site running on IIS7. I am now attempting to scale that site out to multiple servers using RR DNS and IIS7 Shared Configuration. I would like the primary server to be hosted on a Server Core instance of Windows 2008.

    Can I setup Shared Configuration of IIS on a Server Core installation? I thought I had it setup and working but I'm getting 503 errors on the second Windows Server 2008 VM when I inherit the shared configuration files stored on a network share.

  • Apparently, we ran into a similar problem a few months back as part of a demo. Shared config works on Server Core. The biggest block is not having the same modules on both machines.

    From the PM who was knee-deep in the troubleshooting then:

    First guess is the second server is full install of WS08, not Core. In which case all those managed code modules in the globalModules list will fail to load because they don’t exist on the Core server. Which would cause 503.

    Look in the event viewer. If it fails to load a module, there’s an event saying exactly which one.

    Also, verify HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Components list is the same on all machines that will be part of the farm.

  • I am using Windows 2008 Web core and I haven't found a way to enable IIS Shared config, is there a way to do this through the command prompt since IIS remote management doesn't give me the option to change shared configurations?

Comments have been disabled for this content.