Изменить Web.Config Azure PowerShell - PullRequest
       4

Изменить Web.Config Azure PowerShell

0 голосов
/ 10 сентября 2018

У меня есть функция в скрипте, которую я запускаю, чтобы внести некоторые изменения в файл web.config в Azure в одной из наших подписок. Он изменяет адрес конечной точки на основе поиска по ключевым словам. Работает нормально. Мне нужно добавить в него также изменить другой ключ в 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 с «none» на «transport»

enter image description here

1 Ответ

0 голосов
/ 11 сентября 2018

Вы можете обновить режим безопасности для всех узлов привязки с помощью такого кода. Это будет работать, даже если у вас есть несколько узлов привязки и разные виды узлов привязки (например, 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
}
...