The Bald Avenger
The Bald Avenger

Reputation: 119

Powershell - ConvertTo-WebApplication on IIS

I'm trying to use powershell to deploy a .NET 4 application to a virtual directory. I use the following code;

Import-Module webadministration
New-Item IIS:\AppPools\MainAppPool
Set-ItemProperty IIS:\AppPools\MainAppPool managedRuntimeVersion v4.0

New-Item IIS:\AppPools\Site1AppPool
Set-ItemProperty IIS:\AppPools\Site1AppPool managedRuntimeVersion v4.0

New-Item IIS:\Sites\DemoSite -physicalPath C:\DemoSite -bindings @{protocol="http";bindingInformation=":82:"}
Set-ItemProperty IIS:\Sites\DemoSite -name applicationPool -value DemoAppPool

New-Item IIS:\Sites\DemoSite\Site1 -physicalPath C:\Deployment\application -type VirtualDirectory
ConvertTo-WebApplication IIS:\Sites\DemoSite\Site1
Set-ItemProperty IIS:\sites\DemoSite\Site1 -name applicationPool -value Site1AppPool

When I run this, it appears to work correctly, but on running the site, I get the following error;

Configuration Error

**Description**: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

**Parser Error Message**: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.  This error can be caused by a virtual directory not being configured as an application in IIS.

Source Error: 

Line 13:       ASP.NET to identify an incoming user. 
Line 14:     -->
Line 15:     <authentication mode="Windows" />
Line 16:     <!--
Line 17:        The <customErrors> section enables configuration 

If I then remove the application in IIS and manually re-create it, the site works fine. Can you tell me what I am missing?

Upvotes: 6

Views: 7019

Answers (2)

Derorrist
Derorrist

Reputation: 2993

I had this same issue while deploying a Web Site to an IIS using the Web Setup Project. After the setup completes the web site is a virtual directory instead of an application, so I was unable to use CarlR's answer.

What I ended up doing is running a vbs script and then a powershell script converted into an exe on the Commit stage of the installation.

Dim objShell, strPath, strVdir
Set objShell = CreateObject ("WScript.Shell")

strPath = """%systemroot%\system32\inetsrv\APPCMD"""
strVdir = "delete vdir /vdir.name:""Default Web Site/PathToVirtualDirectory"""

objShell.Run strPath & strVdir

You can find the vdir.name attribute using APPCMD list vdir.

After that ConvertTo-WebApplication works and applicationhost.config does not contain the virtualdirectory tag that causes this error.

Upvotes: 0

CarlR
CarlR

Reputation: 1768

When you run the commandlet Convert-ToWebApplication the previous virtual directory entry remains in the applicationhost.config. I ran into the same error.

Change your command to create the application directly rather than create a virtual directory then convert it.

New-Item IIS:\Sites\DemoSite\Site1 -physicalPath C:\Deployment\application -type Application

Upvotes: 7

Related Questions