iis-7 - 使用 powershell 管理单元配置 IIS 时如何在 powershell 中使用枚举类型

标签 iis-7 powershell

我正在使用 IIS Powershell 管理单元从头开始配置新的 Web 应用程序。我是PS新手。以下脚本将不起作用,因为 PS 无法识别 ManagedPipelineMode 枚举。如果我将值更改为 0,它将起作用。我怎样才能让 PS 理解 enum.我尝试了 Add-Type cmdlet 并加载了 Microsoft.Web.Administration 程序集,但没有成功,这些行现在已被注释。

如何让这个 PS 脚本与枚举一起使用?

#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()

}

错误信息是:

Set-ItemProperty : [Microsoft.Web.Administration.ManagedPipelineMode]::Integrated is not a valid value for Int32.

最佳答案

它需要一个整数,即使底层属性的类型是ManagaedPipelineMode。不过,您可以执行以下操作:

$AppPool | Set-ItemProperty -Name "managedPipelineMode" -Value ([int] [Microsoft.Web.Administration.ManagedPipelineMode]::Classic)

PS:

而不是

$AppPool = Get-ChildItem iis:\apppools | where { $_.Name -eq $AppPoolName}

你可以这样做:

$AppPool = Get-Item iis:\apppools\$AppPoolName

关于iis-7 - 使用 powershell 管理单元配置 IIS 时如何在 powershell 中使用枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7988306/

相关文章:

powershell - 使用 PowerShell 枚举 Linux SMB 共享

wordpress - 使用资源组库模板在 azure 中创建 WordPress 站点时,为什么 msdeploy 失败?

asp.net - 为什么我的 aspx 页面没有缓存在客户端上?

windows - 使用 powershell 遍历在 IIS 中配置的所有绑定(bind)

powershell - Powershell:将列表中的对象与同一列表中的其他对象进行比较

powershell - 如何使用 WMI 获取计算机的当前 OU 并列出该 OU 中的所有其他计算机?

json - 在powershell中将成员之一作为数组传递给JSON

asp.net - 在 IIS 7 上浏览新部署的 MVC 应用程序时出错 : Http 500 and custom errors not set

asp.net - 如何在 ASP.NET 页面生命周期中从 HTTP 重定向到 HTTPS?

asp.net-mvc - 在 ASP.NET MVC 中混合使用 Windows 和 Forms 身份验证