2014-10-29 70 views
4

我正在爲我們的Web應用程序和Web服務編寫PowerShell 3.0安裝程序,並且在嘗試設置物理路徑憑據時遇到了問題。PowerShell IIS Set-WebConfigurationProperty - 鎖定的ApplicationHost.config部分

我的代碼如下所示:

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
# >>>>>> Path credentials 
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 

# Set the physical path credentials of the web application (on Basic Settings screen) to Connect As... 
$filter="/system.applicationHost/sites/site[@name='{0}' and @id='1']/application[@path='/{1}']/VirtualDirectory[@path='/']" -f $script:WebSiteName,$appName 
Set-WebConfiguration $filter -Value @{userName="$physicalPathCredentialUserID";password="$physicalPathCredentialPassword"} 

執行的時候,我得到在PowerShell中指出的錯誤「這部分配置不能在這個路徑中使用時,部分在父級別上鎖定了出現這種情況。」 。我嘗試了在認證部分被鎖定時工作的PSPath和位置標籤,但那些似乎沒有任何作用。我想也許-Force選項會起作用,但儘管沒有發生錯誤,但物理路徑憑證似乎並未採用。

如果沒有-Force選項,會拋出錯誤,但PowerShell會切斷郵件,因此我無法確切地說出它抱怨的是哪個部分,或者哪個父級別被鎖定。我必須假設它是網站部分,因爲我正在嘗試配置:/configuration/system.applicationHost/sites/application/virtualDirectory

我對解鎖和允許覆蓋之間的差異有點困惑,以獲取值堅持。 PowerShell Web管理在這方面很混亂。我不知道爲什麼它必須如此混淆才能將值設置爲可以在IIS管理界面中設置的值。有些值使用Set-WebConfiguration,如上所示,其他值使用Set-WebConfigurationProperty。如果鎖定是一個已知問題,爲什麼不解鎖更好的記錄?

我不想解鎖所有網站或所有應用程序。我只想解鎖我必須爲了在默認Web站點下安裝的每個Web應用程序上設置配置值。

截至2014年和PowerShell 3.0的解鎖或覆蓋配置節的權威解決方案是什麼?哪些設置接受PSPath和位置?下面的

順便說一句,我曾嘗試變種:

$filter="/system.applicationHost/sites/site[@name='{0}' and @id='1']/application[@path='/{1}']/VirtualDirectory[@path='/']" -f $script:WebSiteName,$appName 
Set-WebConfiguration $filter machine/webroot/appHost -metadata overrideMode -value Allow 

,但繼續得到鎖定部分消息,直到過濾器支持關閉的網站水平。

我也嘗試設置virtualDirectoryDe​​faults.userName和virtualDirectoryDe​​faults.password,但似乎並沒有採取最初,但IISReset後,我注意到他們確實添加在applicationHost.config文件的底部。我真的不希望他們設置爲默認值,因爲我們的應用程序不應該影響服務器上的其他應用程序。

我很感激您可以提供的任何幫助。我必須錯過一些東西,因爲設置這些和其他Web應用程序配置值應該不那麼困難。

Regards

回答

1

您嘗試更改的部分在IIS機器配置中設置。您必須解鎖這些部分才能按照站點進行設置。

請參見:Programmatically unlocking IIS configuration sections in Powershell

+0

這似乎是一個非常粗糙的方法。我真的不得不退出Web管理模塊並加載ServerManager嗎?我無法相信爲PowerShell編寫Web管理的人員並不需要解鎖應用程序設置。 – Scott 2014-10-29 21:43:04

+0

這不是ServerManager中處理角色和功能的模塊,它是.net Web管理控件的一部分。儘管如此,這個帖子還是挺老的。也許有更好的辦法,但我本人並不知道。如果你想使用第三方產品,Carbon似乎已經把它放到了一個漂亮的整潔的cmdlet中(我從來沒有使用它):http://get-carbon.org/help/Unlock-IisConfigurationSection.html – briantist 2014-10-29 21:50:20

0

你的過濾器看起來不正確的。您可以將過濾器視爲XPath查詢。因此,如果您使用//authentication/*的過濾器,那麼它將獲得認證節點下的所有配置。它與XPath不完全相同,但非常接近。請記住,您無法僅使用Filter參數來選擇元數據部分,如sectionGrouplocation標籤。

我遇到了一個問題,我需要在服務器級別解鎖Windows身份驗證,這樣才能在應用程序級別將Windows身份驗證設置爲不同的值。因此,我不得不做這樣的事情:

Set-WebConfiguration -Metadata OverrideMode -Value Allow -Filter //windowsAuthentication 
Set-WebConfigurationProperty -PSPath IIS:\Sites\$WebsiteName\$AppName -Filter //windowsAuthentication -Name Enabled -Value $true 

這是什麼做的是創造applicationHost.config文件中的一個部分,其看起來像這樣:

<location path="" overrideMode="Allow"> 
     <system.webServer> 
      <security> 
       <authentication> 
        <windowsAuthentication> 
        </windowsAuthentication> 
       </authentication> 
      </security> 
     </system.webServer> 
</location> 

無論配置你與該位置標籤地方會根據我認爲的IIS被視爲解鎖。

而這正是被添加到Web.config文件的Web應用程序本身:

<authentication> 
    <windowsAuthentication enabled="true" /> 
</authentication> 

希望這有助於。