使用Powershell WebAdministration module获取网站的根应用程序
我正在尝试在Azure中部署的网站根目录下配置web应用程序的ServiceAutoStart Enabled和ServiceAutoStart Provider属性。如果我了解自动启动过程,可以在单个网站下为特定的web应用程序设置这些属性 我在web角色启动期间(在启动任务中获得提升权限后)执行powershell脚本,以执行web管理任务,如下所示:使用Powershell WebAdministration module获取网站的根应用程序,powershell,iis-7.5,azure-web-roles,Powershell,Iis 7.5,Azure Web Roles,我正在尝试在Azure中部署的网站根目录下配置web应用程序的ServiceAutoStart Enabled和ServiceAutoStart Provider属性。如果我了解自动启动过程,可以在单个网站下为特定的web应用程序设置这些属性 我在web角色启动期间(在启动任务中获得提升权限后)执行powershell脚本,以执行web管理任务,如下所示: write-host "Begin RoleStart.ps1" import-module WebAdministration Add
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 *
那么,如何使用webadministration cmdlet为根应用程序设置这两个属性呢?您可以使用generic
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 ???
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
其中“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)" }
但是,如果试图设置值
,则会出现错误,因为它是只读的
最后,您可以使用getwebconfigurationproperty
和setwebconfigurationproperty
以及一些非常难看的xpath来获取和设置值。例如:
Set-ItemProperty "IIS:\Sites\Default Web Site\Test\" -name serviceAutoStartEnabled -Value $true
Set-ItemProperty "IIS:\Sites\Default Web Site\Test\" -name serviceAutoStartProvider -Value ???
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:provider公开的内容。我知道这一点现在已经相当老了,但对于像我这样在搜索过程中遇到这一点的人来说,我就是这样做的:
Import-Module WebAdministration;
$WebSiteName = "My Web Site";
$WebAppName = "MyWebApp";
$ProviderName = "PreWarmMyWebApp";
$ProviderType = "MyNamespace.MyClass, MyAssembly";
Add-WebConfiguration -PSPath 'IIS:\' -Filter '/system.applicationHost/serviceAutoStartProviders' -Value @{ name=$ProviderName; type=$ProviderType };
Set-WebConfigurationProperty -PSPath 'IIS:\' -Filter "/system.applicationHost/sites/site[@name='$WebSiteName']/application[@path='/$WebAppName']" -Name "serviceAutoStartEnabled" -Value "true";
Set-WebConfigurationProperty -PSPath 'IIS:\' -Filter "/system.applicationHost/sites/site[@name='$WebSiteName']/application[@path='/$WebAppName']" -Name "ServiceAutoStartProvider" -Value $ProviderName;
我从Swoogan的帖子中获得了Set-WebConfiguration属性,但是,当我自己找到ServiceAutoStart提供者部分时,我想我应该发布一个完整的解决方案:)