How to convert a .PublishSettings file to .pubxml and .pubxml.user files using Powershell

93 views Asked by At

My team maintain a large number of web sites. Each is a project in a Visual Studio solution, with a publish profile (Properties\PublishProfiles\xxx.pubxml). Goal is to 1) host each web site in an Azure web app and 2) modify the publish profiles so they publish to the respective web apps.

I have written the Powershell script to generate all web apps, and to download the .PublishSettings files for each web app (using Get-AzureRmWebAppPublishingProfile).

My question is, how do I convert the .PublishSettings files to .pubxml and .pubxml.user files using Powershell? I can do this manually for each site (publish | import profile), but I need to do this in a Powershell script because of the large number of sites involved.

1

There are 1 answers

2
Jahnavi On

I tried below sample code in my environment to achieve the conversion of .PublishSettings file to .pubxml and .pubxml.user files using PowerShell.

$resourcegroup = "xxxx"
$webapp = "newapj"
$file = Get-AzureRMWebAppPublishingProfile -ResourceGroupName $resourcegroup -Name $webapp -OutputFile "/home/madugula/webapp.publishsettings"
function Convert-PubSettingsToPubxml {
    param(
        [string]$publishSettingsPath
    )
    $publishSettingsPath = "/home/madugula/webapp.publishsettings"
    $Content = Get-Content -Path $publishSettingsPath 
    $profile = [xml]$Content
    $publishProf = $profile.PublishData.PublishProfile | Where-Object { $_.PublishMethod -eq "MSDeploy" }


    $pubxmlContent = @"
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish>$($publishProf.DestinationPublishUrl)</SiteUrlToLaunchAfterPublish>
    ....#Add it
  </PropertyGroup>
</Project>
"@
    $xmlUserContent = @"
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    #Add other properties as per your requirement
.....
  </PropertyGroup>
</Project>
"@
    $PathpubXml = [System.IO.Path]::ChangeExtension($publishSettingsPath, "pubxml")
    $userpathpubxml = [System.IO.Path]::ChangeExtension($publishSettingsPath, "pubxml.user")

    $pubxmlContent | Out-File -FilePath $PathpubXml
    $xmlUserContent | Out-File -FilePath $userpathpubxml

    Write-Host "Conversion Done. saved at: $PathpubXml, $userpathpubxml"
}

$publishSettingsPath = "/home/madugula/WebApp.PublishSettings"
Convert-PubSettingsToPubxml -publishSettingsPath $publishSettingsPath

Output:

enter image description here