William Humphreys
William Humphreys

Reputation: 1404

Powershell 7+ Cannot Administer IIS 10 with the IISAdministration Module

The following code will work (only sometimes though ??) with older versions of PowerShell but not at all with version 7+.

All I want to do is create a new Application Pool in IIS 10+.

The documentation is very poor and most examples don't work anymore. Is there something I am missing here?

   # PowerShell 7.2.6

   # Windows Server 2019 (With the GUI).

   # IIS 10 etc is already installed.

   # Executed as Administrator.

   # NOTE: This imports the IISAdministration module not the old WebAdministration module.
   Import-Module -Name IISAdministration -UseWindowsPowerShell -WarningAction SilentlyContinue

   Reset-IISServerManager -Confirm:$false

   Start-IISCommitDelay

   # Create the foo.co.uk application pool.
   #
   # From this thread:
   #
   # https://github.com/MicrosoftDocs/windows-powershell-docs/issues/421
   #
   # And This one:
   #
   # https://blogs.iis.net/jeonghwan/how-to-use-iisadministration-powershell-cmdlets-to-configure-iis-configuration-settings

   Write-Host "Creating the foo.co.uk application pool.`n" -ForegroundColor Green

   $ServerManager = Get-IISServerManager
   $ServerManager.ApplicationPools.Add("foo.co.uk.apppool")

   # $ServerManager.CommitChanges() (this doesn't work though I keep seeing it in examples.)

   Stop-IISCommitDelay

I have now tried many examples of code that are similar to the above from the few online examples I can find and none of them work.

Upvotes: 1

Views: 1231

Answers (2)

William Humphreys
William Humphreys

Reputation: 1404

I have added this in extra to the post I have marked as the answer to help anybody in the future as the documentation and examples to do this are all over the place.

Do note the date of this post as some *stuff may have been fixed in the future.

I like to use PowerShell (modules) only, where I can but in this case the PowerShell Modules fail in a lot of cases, even when you follow the documentation (links I have included in the source).

I have noticed that the new cross platform .NET PowerShell (the one I want to use) also acts quite differently than the older non cross platform version. But even the older version of PowerShell was failing on some cases, again even following the documentation.

This is simply how I (using IIS and Windows Server 2019) created an application pool, created a website, and then changed the logging using a combination of the PowerShell modules that mostly don’t work and the older appcmd.exe (as to the answer in this question.)

Note I have removed any error checking and functions.

As an example, I find this is easier to read without all the extra code. You will want to refactor this code.

Both the following were installed (and a reboot) before attempting to administer IIS using the main setup code.

Install-WindowsFeature -name Web-Server -IncludeManagementTools

Install-WindowsFeature web-mgmt-console

After a reboot...


# Both the new (ish) IISAdministration and the old WebAdministration modules have been used where they
# work but mostly they don't work as per the documentation (as of the date of this file) noting both 
# IncludeManagementTools and web-mgmt-console have been pre-installed. Where the above don't work the 
# old command line program C:\Windows\System32\inetsrv\appcmd.exe has been used.

# https://learn.microsoft.com/en-us/powershell/module/iisadministration/
Import-Module -Name IISAdministration -UseWindowsPowerShell -WarningAction SilentlyContinue

# https://learn.microsoft.com/en-us/powershell/module/webadministration/
Import-Module -Name WebAdministration -UseWindowsPowerShell -WarningAction SilentlyContinue

# Create the foo.co.uk website directory.
New-Item -ItemType Directory -Path "C:\inetpub\foo.co.uk"

# Create the foo.co.uk logfile directory.
New-Item -ItemType Directory -Path "C:\inetpub\logs\LogFiles\foo.co.uk"

# Copy the foo.co.uk website files from the C:\Setup directory (I'm sure you have somewhere.)
Copy-Item -Path "C:\Setup\foo.co.uk\*" -Destination "C:\inetpub\foo.co.uk" -recurse -Force

# Create the foo.co.uk.app.pool application pool.
C:\Windows\System32\inetsrv\appcmd add apppool /name:"foo.co.uk.app.pool" /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated

# Create the foo.co.uk website. (do note the ID here in relation to your own setup)
C:\Windows\System32\inetsrv\appcmd add site /name:"foo.co.uk" /id:1 /bindings:"http/192.168.0.1:80:foo.co.uk" /physicalPath:"C:\inetpub\foo.co.uk"

# Add the foo.co.uk.app.pool application pool to the foo.co.uk website. (Note the use of double quotes to deal with the single quotes as needed by PowerShell.)
C:\Windows\System32\inetsrv\appcmd set site /site.name:"foo.co.uk" /"[path='/'].applicationPool:`"foo.co.uk.app.pool`""

# Setup the foo.co.uk logfile directory and period.
C:\Windows\System32\inetsrv\appcmd set site /site.name:"foo.co.uk" /logFile.directory:"C:\inetpub\logs\LogFiles\foo.co.uk" /logFile.period:"Monthly"

There are some hardcoded paths in this code, you may want to use various environment settings in your own code.

This code also hasn't setup any HTTPS, SSL / TLS certificates that you should in a live site.

Use this simple code as a base.

Do feel free to post working code in the future if some of these issues have been fixed.

Upvotes: 1

postanote
postanote

Reputation: 16096

I am not real sure why you are encountering this (and not in any position to experiment), but, when push comes to shove, you can always call ...

appcmd add apppool /name:myAppPool

or with other settings

appcmd add apppool /name:myAppPool /managedRuntimeVersion:v2.0 /managedPipelineMode:Integrated
appcmd add apppool /name:myAppPool /managedRuntimeVersion:v2.0 /managedPipelineMode:Classic

... directly from your script, regardless of the Powershell version, but I get you are trying to stay in pure PowerShell.

Upvotes: 1

Related Questions