AJ.
AJ.

Reputation: 16719

Powershell scripting creation of a "sub" website

I have two folders:

c:\inetpub\site1
c:\inetpub\site1Sub

I want "site1" to be the "parent" website and "site1Sub" to be a "sub" website. Both sites should run under the same application pool, which is a custom pool created solely for these sites, call it "site1." In IIS Manager (7.5), I simply create the application pool, then the parent site, then right-click on the parent site and "Add Application," pointing it at the physical path "c:\inetpub\site1Sub." This all works fine.

When I try to script this in Powershell, however, things get difficult. I can create "site1" and the application pool with no problems. It's the sub-site that's being a pain. I've tried two approaches:

Approach 1: Use New-Item and set the app pool after.

$subSite = New-Item "IIS:\Sites\site1\site1Sub" -physicalPath "C:\inetpub\site1Sub" -type "Application"
$subSite | Set-ItemProperty -Name "applicationPool" -Value "site1"

With this approach, I receive an error after the Set-ItemProperty command:

Set-ItemProperty : Cannot find path 'C:\site1Sub' because it does not exist.
At line:1 char:127
+ $subSite | Set-ItemProperty -Name "applicationPool" -Value "site1"  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\site1Sub:String) [Set-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand

Approach 2: Create a virtual directory and use ConvertTo-WebApplication (as in this answer).

New-WebVirtualDirectory -Site "site1" -Name "site1Sub" -PhysicalPath "c:\inetpub\site1Sub"
ConvertTo-WebApplication -ApplicationPool "site1" "IIS:\Sites\site1\site1Sub"

This runs fine, and further, it looks fine in IIS Manager, but when I attempt to navigate to the site I receive an error stating that the web.config failed to parse:

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.

I'm totally stumped. How can I script this scenario in Powershell?

Upvotes: 3

Views: 2296

Answers (3)

JamesQMurphy
JamesQMurphy

Reputation: 4429

I've found that when creating an application under a website, I've needed to specify the -Force switch in the call to New-Item. I've left out the checking to see if the site already exists (Sneijder's answer shows how to do that with Test-Path), but this should do the trick:

Import-Module WebAdministration

$sitePath = 'IIS:\Sites\site1'
$subSitePath = "$sitePath\site1Sub"
$appPoolName = 'site1'

# Create Site and Subsite
New-Item $sitePath -PhysicalPath 'C:\Inetpub\site1' -Bindings  @{protocol="http";bindingInformation=":80:"}                        }
New-Item $subSitePath -Type Application -PhysicalPath 'C:\Inetpub\site1Sub' -Force

# Create App Pool
New-Item "IIS:\AppPools\$appPoolName"

# Associate sites to App Pool
Set-ItemProperty $sitePath -Name applicationPool -Value $appPoolName
Set-ItemProperty $subSitePath -Name applicationPool -Value $appPoolName

Upvotes: 4

Sneijder
Sneijder

Reputation: 319

As i understand you correctly, you would like to have following result in your local IIS Manager:

IIS-Manager Sites

This script creates an application pool. Then it creates the root site and afterwards the subsite.

Import-Module WebAdministration

$iisAppPoolName = "applicationPool"
$iisAppPoolDotNetVersion = "v4.0"

$iisAppName = "site1"
$directoryPath = "C:\inetpub\site1"
$iisSubAppName = "site1sub"
$directorySubPath = "C:\inetpub\site1Sub"

#navigate to the app pools root
cd IIS:\AppPools\

#check if the app pool exists
if (!(Test-Path $iisAppPoolName -pathType container))
{
    #create the app pool
    $appPool = New-Item $iisAppPoolName
    $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $iisAppPoolDotNetVersion
}

#navigate to the sites root
cd IIS:\Sites\

#check if the site exists
if (Test-Path $iisAppName -pathType container)
{
    return
}

#create the site
$iisApp = New-Item $iisAppName -bindings @{protocol="http";bindingInformation=":80:"} -physicalPath $directoryPath
$iisApp | Set-ItemProperty -Name $iisAppPoolName -Value $iisAppPoolName

#create the subSite
$iisSubApp = New-WebApplication -Name $iisSubAppName -Site $iisAppName -PhysicalPath $directorySubPath -ApplicationPool $iisAppPoolName

Result:

Result

Hope this helps.

Upvotes: 2

Chris Kuperstein
Chris Kuperstein

Reputation: 678

Have you tried mapping your IIS directory tree as a PSDrive, then re-attempt approach 1?

https://technet.microsoft.com/en-us/library/ee176915.aspx

Do a Get-PSDrive to see what PS Providers are available to the host, then attempt to map it like a filesystem.

# For psprovider, I'm not sure if the provider name is 'IIS' or some other.
# For mapping remote drives, usually FileSystem works,
# but try Get-PSDrive to see whats available to your host for this switch.
New-PSDrive -name X -psprovider IIS -root 'IIS:\'

I don't have IIS config'd on my testbox to confirm for you, but just putting this up here to try.

Upvotes: -1

Related Questions