Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 254
softveda Avatar asked Nov 02 '11 22:11

softveda


2 Answers

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
like image 200
manojlds Avatar answered Nov 09 '22 16:11

manojlds


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
like image 45
Keith Hill Avatar answered Nov 09 '22 16:11

Keith Hill