【问题标题】:How to configure 32-bit IIS websites or applications using PowerShell cmdlets如何使用 PowerShell cmdlet 配置 32 位 IIS 网站或应用程序
【发布时间】:2016-01-05 14:07:10
【问题描述】:

我正在尝试更改 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 配置编辑器中看到该条目:

问题是应用程序池设置为“启用 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 文件中指定此设置。

感谢您对此问题的任何启发。

【问题讨论】:

    标签: windows powershell iis web-administration window-server


    【解决方案1】:

    有两种解决方法。

    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 = New-Object -TypeName Microsoft.Web.Administration.WebConfigurationMap -ArgumentList "C:\Windows\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine.config","C:\Windows\Microsoft.NET\框架\v4.0.30319\CONFIG\web.config" $iis.GetWebConfiguration($wcm, $null).GetSection("appSettings").SetAttributeValue("file", "test3") $iis.CommitChanges()

    【讨论】:

      【解决方案2】:

      经过实验,使用此处提供的答案中的一些信息和离线对话,我可以肯定地说,不可能使用 Microsoft WebAdministration PowerShell cmdlet 编辑 32 位根 web.config 文件.

      似乎 cmdlet 是硬编码的,只能查看 64 位版本的配置文件。 IIS 管理器的行为方式相同。为什么会这样,我无法解释。

      我还发现使用某些 cmdlet 来编辑 64 位 Clr 2.0 网站和应用程序时存在很多问题。 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"""
              }
          }
      

      我希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 2015-04-12
        • 2012-12-23
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 2012-05-05
        • 1970-01-01
        相关资源
        最近更新 更多