如何使用PowerShell cmdlet配置32位IIS网站或应用程序

v440hwme  于 2022-11-12  发布在  Shell
关注(0)|答案(2)|浏览(148)

我正在尝试更改ASP.NET临时文件的位置,以便在发布新版本时清理这些文件。
因为很难找到特定网站的ASP.NET临时文件的位置,所以我决定将文件移动到磁盘上的特定位置,然后清理虚拟目录下的应用程序。
您可以通过修改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参数,该值将写入CLR 4.0配置文件中的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config位置。
您也可以在IIS配置编辑器中看到该条目:x1c 0d1x
问题是应用程序池设置为“启用32位应用程序”,因此忽略了此属性。
如果我手动将上面显示的location元素从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配置
    1.临时文件的位置在设计时是未知的,由宿主服务器配置确定。因此,不能在应用程序本身的web.config文件中指定此设置。
vwkv1x7d

vwkv1x7d1#

有两种解决方法。
1.请使用32比特的appcmd.exe。范例如下。
设置配置-节:应用程序设置/+"[键=“测试”,值=“测试”]”/commit:webroot /clr:4.0
1.直接在powershell上使用MWA。下面是一个示例。
在此基础上,本文提出了一种新的Web服务器配置方法,该方法包括以下步骤:

v6ylcynt

v6ylcynt2#

经过实验,使用此处提供的答案中的一些信息和脱机对话,我可以得出结论:使用Microsoft WebAdministration PowerShell cmdlet编辑32位根web.config文件 -是不可能的*。
看起来cmdlet被硬编码为只查看64位版本的配置文件。IIS管理器的行为也是如此。我无法解释为什么会出现这种情况。
我还发现使用一些cmdlet编辑64位Clr 2.0网站和应用程序时存在很多问题。Clr参数并不存在于所有cmdlet上,即使在有Clr参数的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"""
        }
    }

相关问题