powershell - 使用 Powershell WebAdministration 模块获取网站的 root 应用程序

标签 powershell iis-7.5 azure-web-roles

我正在尝试在 Azure 中部署的网站根目录下配置 Web 应用程序的 serviceAutoStartEnabled 和 serviceAutoStartProvider 属性。如果我了解自动启动过程,可以为单个网站下的特定 Web 应用程序设置这些属性。

我在 Web 角色启动期间执行 powershell 脚本(在启动任务中获得提升的权限后)以执行 Web 管理任务,如下所示:

write-host "Begin RoleStart.ps1"

import-module WebAdministration

Add-WindowsFeature NET-WCF-HTTP-Activation45,NET-WCF-TCP-Activation45,NET-WCF-Pipe-Activation45

$listenerService = Get-WmiObject win32_service -filter "name='NetPipeActivator'"
$listenerService.ChangeStartMode("Automatic")
$listenerService.StartService()

$WebRoleSite = (Get-WebSite "*web*")
$WebRoleSiteName = $WebRoleSite.Name
$WebRoleAppPool = $WebRoleSite.ApplicationPool

New-ItemProperty "IIS:/Sites/$WebRoleSiteName" -name bindings -value @{protocol="net.pipe";bindingInformation="*"}
Set-ItemProperty "IIS:/Sites/$WebRoleSiteName" -Name EnabledProtocols 'http,net.pipe'

Set-ItemProperty -Path "IIS:\AppPools\$WebRoleAppPool" -Name startMode -Value AlwaysRunning

write-host "End RoleStart.ps1"

这会根据需要使用 AlwaysRunning 属性设置应用程序池,但我仍然需要为应用程序特定的 serviceAutoStartEnabled 和 serviceAutoStartProvider 属性提供新值。

我知道我可以使用 Get-WebApplication 来获取应用程序并设置这两个属性,但是当我运行以下 powershell 命令时,我没有看到根(“/”)应用程序的应用程序:

(Get-WebApplication "*") | format-list *

那么如何使用 Web 管理 cmdlet 为根应用程序设置这两个属性?

最佳答案

您可以使用通用 Set-ItemProperty cmdlet 设置这些属性。例如:

Set-ItemProperty "IIS:\Sites\Default Web Site\Test\" -name serviceAutoStartEnabled -Value $true
Set-ItemProperty "IIS:\Sites\Default Web Site\Test\" -name serviceAutoStartProvider -Value ???

其中“Test”是一个 Web 应用程序。抱歉,我不知道 serviceAutoStartProvider 的有效值是什么。有时,即使 Get-ItemProperty 将它们显示为字符串,它们也是整数。

您可以通过以下方式查看值:

Get-ItemProperty "IIS:\Sites\Default Web Site\Test\" -name serviceAutoStartEnabled

您也可以在对象上查看,例如:

Get-WebApplication "*" | % { Write-Output "Path: $($_.Path), AutoStartEnabled: `
  $($_.Attributes["serviceAutoStartEnabled"].Value) AutoStartProvider: `
  $($_.Attributes["serviceAutoStartProvider"].Value)" }

但是,如果您尝试设置Value,您将收到错误,因为它是只读的。

最后,您可以使用 Get-WebConfigurationProperty 和 Set-WebConfigurationProperty 以及一些非常丑陋的 xpath 来获取和设置值。例如:

Get-WebConfigurationProperty "/system.applicationHost/sites/site[@name='Default Web Site' and @id='1']/application[@path='/Test']" -Name serviceAutoStartEnabled
Set-WebConfigurationProperty "/system.applicationHost/sites/site[@name='Default Web Site' and @id='1']/application[@path='/Test']" -Name serviceAutoStartEnabled -Value $true

请注意,这两个 cmdlet 比 Get/Set-ItemProperty 更灵活,因为它们支持配置的所有部分,而 Get/Set-ItemProperty 仅支持公开的部分由 IIS:提供商提供。

关于powershell - 使用 Powershell WebAdministration 模块获取网站的 root 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16381394/

相关文章:

powershell - 更改工作流的 SerializationMethod

c# - DotNetOpenAuth 模板在发布时不起作用

azure - 在 Azure 应用服务中测试 Web 应用的多个实例

powershell - PowerShell函数未按预期运行

powershell - 写主机与写输出 - 换行符

iis-7.5 - HTTP 错误 404.3 - 在 IIS 7.5 中未找到

azure - Azure云服务访问权限和持久存储中的Web角色?

azure - Windows Azure 和 WCF 数据服务 V3

regex - powershell - 匹配字符串,如果它具有某个字符之一。即姓氏_名字与姓氏_名字_中间名不匹配

asp.net - 在Web应用程序之间共享SQL Server session 状态