修改Web.Config Azure PowerShell

时间:2018-09-10 18:24:19

标签: azure powershell azure-web-sites

我在脚本中有一个函数,该脚本可以运行以对其中一个订阅的Azure中的web.config文件进行一些更改。它基于搜索关键字来修改端点地址。工作正常。我需要添加到其中,还需要修改web.config中的另一个键,但不能100%确定如何将其添加到此功能中。这是功能代码:

function Update-ServiceConfigFile() {

    Write-Verbose "Updating Service Web Config"
    $webConfig = 'directoryToConfigInAzure/web.config'

    Write-Verbose -Message "Downloading $webConfig"
    $tempFile = [IO.Path]::GetTempFileName()
    Write-Verbose "Write $tempFile"
    Download-File $accessToken $webappName $webConfig $tempFile
    $doc = [xml](Get-Content $tempFile)

    $customerId = $WebAppName.Substring(0, 6)
    $nodes = $doc.configuration.'system.serviceModel'.client.endpoint

    foreach($node in $nodes) {
        $address = $node.Attributes['address'].Value;


        $address = $address -replace ':ServicePortNumber', ''
        $ub = [System.UriBuilder]::new($address);

        if($Insecure) {
            Write-Verbose "Setting HTTP (insecure) API endpoint"
            $ub.Scheme = 'http';
        } else {
            Write-Verbose "Setting HTTPS (secure) API endpoint"
            $ub.Scheme = 'https';
        }

        if($address.contains("SomeAddress"))
        {
           $ub.Host = "service-prod-$($customerId).ourdomainname.cloud"

        }else{
        $ub.Host = "service-$($customerId).ourdomnaineame.cloud";
        }

        if($webAppName -like '*-test') {
            $ub.Host = "service-test-$($customerId).ourdomnaineame.cloud";
        }

        $ub.Port = -1;
        $node.Attributes['address'].Value = $ub.Uri.AbsoluteUri;
    }

    $doc.Save($tempFile)

    Upload-File $accessToken $webappName $webConfig $tempFile
    Remove-Item $tempFile -Force
}

我需要添加的内容是将system.serviceModel安全模式中的另一个值从“无”更改为“传输”

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码更新所有绑定节点的安全模式。即使您有多个绑定节点和不同种类的绑定节点(例如wsHttpBinding,basicHttpBinding等),它也将起作用。

$bindingNodes = $doc.configuration.'system.serviceModel'.bindings

$securityNodes = $bindingNodes.SelectNodes("//security")

foreach($securityNode in $securityNodes)
{
  $mode = $securityNode.Attributes['mode'].Value;
  Write-Host("Mode = ", $mode)

  # I am checking for none in a case insensitive way here, so only none will be changed to transport. You can remove the if condition, in case you want any value to change to Transport.
  if($mode -ieq 'none') {
    $securityNode.Attributes['mode'].Value = 'Transport'
  }
}

为了您的理解,我只给出了以上重要部分。

总体功能代码将变成这样。

function Update-ServiceConfigFile() {

    Write-Verbose "Updating Service Web Config"
    $webConfig = 'directoryToConfigInAzure/web.config'

    Write-Verbose -Message "Downloading $webConfig"
    $tempFile = [IO.Path]::GetTempFileName()
    Write-Verbose "Write $tempFile"
    Download-File $accessToken $webappName $webConfig $tempFile
    $doc = [xml](Get-Content $tempFile)

    $customerId = $WebAppName.Substring(0, 6)
    $nodes = $doc.configuration.'system.serviceModel'.client.endpoint

    foreach($node in $nodes) {
        $address = $node.Attributes['address'].Value;


        $address = $address -replace ':ServicePortNumber', ''
        $ub = [System.UriBuilder]::new($address);

        if($Insecure) {
            Write-Verbose "Setting HTTP (insecure) API endpoint"
            $ub.Scheme = 'http';
        } else {
            Write-Verbose "Setting HTTPS (secure) API endpoint"
            $ub.Scheme = 'https';
        }

        if($address.contains("SomeAddress"))
        {
           $ub.Host = "service-prod-$($customerId).ourdomainname.cloud"

        }else{
        $ub.Host = "service-$($customerId).ourdomnaineame.cloud";
        }

        if($webAppName -like '*-test') {
            $ub.Host = "service-test-$($customerId).ourdomnaineame.cloud";
        }

        $ub.Port = -1;
        $node.Attributes['address'].Value = $ub.Uri.AbsoluteUri;
    }

    # NEW CODE STARTS HERE

    $bindingNodes = $doc.configuration.'system.serviceModel'.bindings

    $securityNodes = $bindingNodes.SelectNodes("//security")

    foreach($securityNode in $securityNodes)
    {
      $mode = $securityNode.Attributes['mode'].Value;
      Write-Host("Mode = ", $mode)

      # I am checking for none in a case insensitive way here, so only none will be changed to transport. You can remove the if condition, in case you want any value to change to Transport.
      if($mode -ieq 'none') {
        $securityNode.Attributes['mode'].Value = 'Transport'
      }
    }

    # NEW CODE ENDS HERE

    $doc.Save($tempFile)

    Upload-File $accessToken $webappName $webConfig $tempFile
    Remove-Item $tempFile -Force
}