2013-05-05 33 views
2

我試圖在部署在Azure中的網站的根目錄下配置web應用程序的serviceAutoStartEnabled和serviceAutoStartProvider屬性。如果我瞭解自動啓動過程,可以在單個網站下爲特定Web應用程序設置這些屬性。使用Powershell Web管理模塊獲取網站的根應用程序

我web角色啓動時執行PowerShell腳本(後提升的權限已經啓動任務獲得)來執行網絡管理任務如下所示:

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爲根應用程序設置這兩個屬性?

回答

0

您可以使用通用的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 ??? 

其中「測試」是一個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-WebConfigurationPropertySet-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暴露:供應商。

0

我知道這是很老了,但是,對於那些像我這樣在搜索過程中遇到這個,這是我怎麼會做它:

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; 

我的Set-WebConfigurationProperty位來自Swoogan的但是,因爲我自己想出serviceAutoStartProviders部分,所以我想我會發佈一個完整的解決方案:)

相關問題