簡體   English   中英

如何使用PowerShell cmdlet配置32位IIS網站或應用程序

[英]How to configure 32-bit IIS websites or applications using PowerShell cmdlets

我正在嘗試更改ASP.NET臨時文件的位置,以便可以在發布新版本期間清理它們。

因為很難找到特定網站的ASP.NET臨時文件的位置,所以我決定在C:\\Windows\\Microsoft.NET\\Framework C:\\Windows\\Microsoft.NET\\Framework64位置下的虛擬目錄應用程序只需將文件移動到磁盤上的特定位置即可進行清理,這樣會更容易。

您可以通過修改system.web/compilation配置部分中的tempDirectory屬性來執行此操作。

我們使服務器的構建和發布過程自動化,因此看起來很容易將代碼添加到配置和發布腳本中。

但是在測試過程中,我發現32位應用程序的位置沒有改變。

我使用的代碼是:

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT' -location 'MyWebSite' -filter 'system.web/compilation' -name 'tempDirectory' -value 'E:\Temporary ASP.NET Files\MyWebSite' -Clr v2.0

該代碼可以正常工作,並將條目寫入以下根Web.config文件: C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\CONFIG\\web.config

例如

<location path="MyWebSite">
  <system.web>
    <compilation tempDirectory="E:\Temporary ASP.NET Files\MyWebSite" />
  </system.web>
</location>

請注意,如果沒有-Clr v2.0參數,則將值寫入C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\Config\\web.config的CLR 4.0配置文件中。

您還可以在IIS配置編輯器中看到該條目: IIS配置編輯器顯示指定的臨時路徑

問題是應用程序池設置為“啟用32位應用程序”,因此該屬性將被忽略。

如果我將上面顯示的位置元素從C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\CONFIG\\web.config手動移動到C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\CONFIG\\web.config

所做的更改有效,並且ASP.NET臨時文件的確移動到了網站編譯發生時指定的位置。

問題不是關於ASP.NET臨時文件,而是一個更普遍的問題,即您打算在PowerShell中還是在IIS中配置32位應用程序? 我似乎無法做到這一點。 有許多32位應用程序仍需要配置。

還請注意,出於良好的原因,我選擇使用根web.config來存儲此值:

  1. ApplicationHost.config無法存儲system.web配置
  2. 臨時文件的位置在設計時不是已知的,而是由托管服務器配置確定的。 因此,無法在web.config文件中為應用程序本身指定此設置。

感謝您對這個問題的啟發。

有兩種解決方法。

  1. 使用32位appcmd.exe。 這是例子。

%windir%\\ syswow64 \\ cmd.exe%windir%\\ syswow64 \\ inetsrv \\ appcmd.exe設置配置-section:appSettings / +“ [key ='test',value ='test']” / commit:webroot / clr: 4.0

  1. 直接在Powershell上使用MWA。 這是例子。

$ iis =新對象Microsoft.Web.Administration.ServerManager $ wcm =新對象-TypeName Microsoft.Web.Administration.WebConfigurationMap -ArgumentList“ C:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0.30319 \\ CONFIG \\ machine。 config“,” C:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0.30319 \\ CONFIG \\ web.config“ $ iis.GetWebConfiguration($ wcm,$ null).GetSection(” appSettings“)。SetAttributeValue(” file“, “ test3”)$ iis.CommitChanges()

經過實驗之后,使用此處提供的答案中的某些信息以及脫機對話,我可以得出結論,即無法使用Microsoft WebAdministration PowerShell cmdlet編輯32位根web.config文件。

似乎cmdlet進行了硬編碼,只能查看配置文件的64位版本。 IIS管理器的行為方式相同。 為什么是這種情況對我來說是莫名其妙的。

使用某些cmdlet編輯64位Clr 2.0網站和應用程序時,我也發現了很多問題。 Clr參數並非在所有cmdlet上都存在,即使在它所在的cmdlet中也似乎並不總是有效。

因此,我決定放棄WebAdministration cmdlet,直接使用“ Microsoft.Web.Administration.dll”程序集和Microsoft.Web.Administration.ServerManager對象。

以下是我編寫的一些功能可能會有所幫助:

    function Get-MWAConfigObjects
    {
        <#
                .SYNOPSIS
                Returns object to manage IIS configuration in root web.config
                .DESCRIPTION
                The objects returned allow viewing or editing or configuration. Parameters open the appropriate version, either 32 or 64 bit for the appropriate version of the ManagedRunTime. https://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager(v=vs.90).aspx
Ensure that you call CommitChanges to save any changes made.
                .EXAMPLE
                $MWA = Get-MWAConfigObjects -ManagedRunTimeVersion v2.0 -Architecture 32               $MWA.Configuration.GetSection('system.web/compilation','MyWebSite/MyApplication').SetAttributeValue('tempDirectory', 'C:\NewPath')
                $MWA.ServerManager.CommitChanges()             
        #>
        [cmdletbinding(positionalbinding = $false)]
        param(
            [Parameter(Mandatory = $True)][string][ValidateSet('v2.0','v4.0')] $ManagedRunTimeVersion,
            [Parameter(Mandatory = $True)][string][ValidateSet(32,64)] $Architecture
        )

        $assemblyPath = $(Join-Path -Path $([System.Environment]::GetFolderPath('System')) -ChildPath $(Join-Path -Path 'inetsrv' -ChildPath 'Microsoft.Web.Administration.dll'))
        If (Test-Path -Path $assemblyPath -PathType Leaf)
        {
            $null = [System.Reflection.Assembly]::LoadFrom($assemblyPath)
            $iis = New-Object -TypeName Microsoft.Web.Administration.ServerManager
            $wcm = New-Object -TypeName Microsoft.Web.Administration.WebConfigurationMap -ArgumentList $(Get-ConfigFilePath -Type machine -ManagedRunTimeVersion $ManagedRunTimeVersion -Architecture $Architecture), $(Get-ConfigFilePath -Type web -ManagedRunTimeVersion $ManagedRunTimeVersion -Architecture $Architecture)
            $configuration = $iis.GetWebConfiguration($wcm, $null)

            $object = New-Object -TypeName PSObject
            $object | Add-Member -MemberType NoteProperty -Name ServerManager -Value $iis
            $object | Add-Member -MemberType NoteProperty -Name Configuration -Value $configuration
            Write-Output -InputObject  $object
        }
        else
        {
            Throw "Cannot validate existence of required assembly 'Microsoft.Web.Administration.dll' at ""$assemblyPath"""
        }
    }

    function Get-ConfigFilePath
    {
        [CmdletBinding(PositionalBinding = $false)]
        param
        (
            [Parameter(Mandatory = $True)][string][ValidateSet('web','machine')] $Type, 
            [Parameter(Mandatory = $True)][string][ValidateSet('v2.0','v4.0')] $ManagedRunTimeVersion, 
            [Parameter(Mandatory = $True)][string][ValidateSet(32,64)] $Architecture
        )

        $ErrorActionPreference = 'stop'

        switch ($ManagedRunTimeVersion)
        {
            'v2.0'
            {
                switch ($Architecture)
                {
                    32
                    {
                        $path = $(Join-Path -Path $([System.Environment]::GetFolderPath('Windows')) -ChildPath "Microsoft.NET\Framework\v2.0.50727\CONFIG\$Type.config")
                        break
                    }
                    64
                    {
                        $path = $(Join-Path -Path $([System.Environment]::GetFolderPath('Windows')) -ChildPath  "Microsoft.NET\Framework64\v2.0.50727\CONFIG\$Type.config")
                        break
                    }
                }

                break;
            }
            'v4.0'
            {
                switch ($Architecture)
                {
                    32
                    {
                        $path = $(Join-Path -Path $([System.Environment]::GetFolderPath('Windows')) -ChildPath "Microsoft.NET\Framework\v4.0.30319\CONFIG\$Type.config")
                        break
                    }
                    64
                    {
                        $path = $(Join-Path -Path $([System.Environment]::GetFolderPath('Windows')) -ChildPath  "Microsoft.NET\Framework64\v4.0.30319\CONFIG\$Type.config")
                        break
                    }
                }

                break;
            }
        }

        If (Test-Path -Path $path -PathType Leaf)
        {
            Write-Output -InputObject $path
        }
        else
        {
            Throw "Cannot validate configuration file at path ""$path"""
        }
    }

我希望這可以幫助別人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM