Web.Config transforms outside of Microsoft MSBuild?

后端 未结 8 2120
情书的邮戳
情书的邮戳 2020-11-29 18:13

Is it possible to use Microsoft\'s XML document transform, for preparing web.configs, outside of MSBuild? I would like to use PowerShell to do these transform without having

8条回答
  •  伪装坚强ぢ
    2020-11-29 18:54

    I updated the script a bit to make it work with the latest version of powershell and make it a bit easier.

    function XmlDocTransform($xml, $xdt)
    {
          $scriptpath = $PSScriptRoot + "\"
          $xmlpath = $scriptpath + $xml
          $xdtpath = $scriptpath + $xdt
    
          if (!($xmlpath) -or !(Test-Path -path ($xmlpath) -PathType Leaf)) {
             throw "Base file not found. $xmlpath";
          }
    
          if (!($xdtpath) -or !(Test-Path -path ($xdtpath) -PathType Leaf)) {
             throw "Transform file not found. $xdtpath";
          }
    
          Add-Type -LiteralPath "$PSScriptRoot\Microsoft.Web.XmlTransform.dll"
    
          $xmldoc = New-Object   Microsoft.Web.XmlTransform.XmlTransformableDocument;
          $xmldoc.PreserveWhitespace = $true
          $xmldoc.Load($xmlpath);
    
          $transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdtpath);
          if ($transf.Apply($xmldoc) -eq $false)
          {
              throw "Transformation failed."
          }
          $xmldoc.Save($xmlpath);
    
          Write-Host "Transformation succeeded" -ForegroundColor Green
      }
    

    And to invoke the function use

     XmlDocTransform "App.config" "App.acc.config"
    

提交回复
热议问题