简体   繁体   中英

How to use an enum type in powershell when configuring IIS using the powershell snapin

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM