Reputation: 11074
I am using the IIS Powershell snapin to configure a new web application from scratch. I am new to PS. The following script will not workl as PS is not recognising the ManagedPipelineMode enum. If I change the value to 0 it will work. How can I get PS to understand th enum. I tried the Add-Type cmdlet and also load the Microsoft.Web.Administration assembly without any scuccess, these lines are now commented.
How can I get this PS script working with the enum ?
#Add-Type -AssemblyName Microsoft.Web.Administration
#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
Import-Module WebAdministration
$AppPoolName = 'Test AppPool'
if ((Test-Path IIS:\apppools\$AppPoolName) -eq $false) {
Write-Output 'Creating new app pool ...'
New-WebAppPool -Name $AppPoolName
$AppPool = Get-ChildItem iis:\apppools | where { $_.Name -eq $AppPoolName}
$AppPool.Stop()
$AppPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value "v4.0"
$AppPool | Set-ItemProperty -Name "managedPipelineMode" -Value [Microsoft.Web.Administration.ManagedPipelineMode]::Integrated
$AppPool.Start()
}
The error message is:
Set-ItemProperty : [Microsoft.Web.Administration.ManagedPipelineMode]::Integrated is not a valid value for Int32.
Upvotes: 10
Views: 5579
Reputation: 52450
Instead of using:
$AppPool | Set-ItemProperty -Name "managedPipelineMode" `
-Value [Microsoft.Web.Administration.ManagedPipelineMode]::Integrated
use:
$AppPool | Set-ItemProperty -Name "managedPipelineMode" `
-Value ([Microsoft.Web.Administration.ManagedPipelineMode]::Integrated)
or the even more succinct:
$AppPool | Set-ItemProperty -Name "managedPipelineMode" -Value Integrated
Why? The reason you need brackets in the first answer is because the parameter binder is treating the entire [Microsoft.Web.Administration.ManagedPipelineMode]::Integrated
in your attempt as a string, which cannot be cast to that enumerated type. However, Integrated
can be to that enum. By wrapping it in brackets, it is evaluated again as an expression and is treated as a full type literal.
Upvotes: 0
Reputation: 301387
It is expecting an integer, even though the underlying property is of type ManagaedPipelineMode
. You can do below however:
$AppPool | Set-ItemProperty -Name "managedPipelineMode" -Value ([int] [Microsoft.Web.Administration.ManagedPipelineMode]::Classic)
PS:
Instead of
$AppPool = Get-ChildItem iis:\apppools | where { $_.Name -eq $AppPoolName}
you can do:
$AppPool = Get-Item iis:\apppools\$AppPoolName
Upvotes: 12
Reputation: 201952
Regarding: Add-Type -AssemblyName
- this will only work for a canned set of assemblies that PowwerShell knows about. You have to find the assembly in your file system and use the -Path
parameter. This worked on my system in a 64-bit PowerShell console:
Add-Type -Path C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll
Upvotes: 2