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.
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.