The following forum(s) have migrated to Microsoft Q&A (Preview): Azure Virtual Machines!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
How to deploy ASP.NET Web App to Azure VM using ARM Template and CustomScriptExtension RRS feed

  • Question

  • I am trying to create an ARM template to deploy resources for VM plus auto-deploy ASP.Net web app to VM using CustomScriptExtension. I have created a CustomScriptExtension child resource under VM with code snippet as follow:

               "resources": [
                    {
                        "type": "extensions",
                        "name": "CustomScriptExtension",
                        "apiVersion": "2015-06-15",
                        "location": "[resourceGroup().location]",
                        "dependsOn": [
                            "[concat('Microsoft.Compute/virtualMachines/', parameters('virtualMachineName'))]"
                        ],
                        "properties": {
                            "publisher": "Microsoft.Compute",
                            "type": "CustomScriptExtension",
                            "typeHandlerVersion": "1.8",
                            "autoUpgradeMinorVersion": true,
                            "settings": {
                                "fileUris": [
                                    "<my_storage_file_path>/webdeploy.ps1"
                                ],
                                "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File webdeploy.ps1"
                            }
                        }
                    }
                ]

    Here's the powershell script for webdeploy.ps1:

        # Install IIS (with Management Console)
        Install-WindowsFeature -name Web-Server -IncludeManagementTools
    
        # Install ASP.NET 4.6
        Install-WindowsFeature Web-Asp-Net45
    
        # Delete contents of wwwroot
        Remove-Item -Recurse C:\inetpub\wwwroot\*
    
        # Install Web Management Service (enable and start service)
        Install-WindowsFeature -Name Web-Mgmt-Service
        Set-ItemProperty -Path  HKLM:\SOFTWARE\Microsoft\WebManagement\Server -Name EnableRemoteManagement -Value 1
        Set-Service -name WMSVC -StartupType Automatic
        if ((Get-Service WMSVC).Status -ne "Running") {
            net start wmsvc
        }
    
        # Install Web Deploy 3.6
        # Download file from Microsoft Downloads and save to local temp file (%LocalAppData%/Temp/2)
        $msiFile = [System.IO.Path]::GetTempFileName() | Rename-Item -NewName { $_ -replace 'tmp$', 
                     'msi' } -PassThru
       Invoke-WebRequest -Uri http://download.microsoft.com/download/0/1/D/01DC28EA-638C-4A22-A57B-4CEF97755C6C/WebDeploy_amd64_en-US.msi -OutFile $msiFile
       # Prepare a log file name
       $logFile = [System.IO.Path]::GetTempFileName()
       # Prepare the arguments to execute the MSI
       $arguments= '/i ' + $msiFile + ' ADDLOCAL=ALL /qn /norestart LicenseAccepted="0" /lv ' + $logFile
       # Sample = msiexec /i C:\Users\{user}\AppData\Local\Temp\2\tmp9267.msi ADDLOCAL=ALL /qn/norestart LicenseAccepted="0" /lv $logFile
       # Execute the MSI and wait for it to complete
       $proc = (Start-Process -file msiexec -arg $arguments -Passthru)
       $proc | Wait-Process
       Get-Content $logFile
    
       #Deploy Web App package
       $WebClient = New-Object -TypeName System.Net.WebClient
       $Destination= "C:\WindowsAzure\WebApplication.zip" 
       #$WebClient.DownloadFile($using:WebDeployPackagePath,$destination)
       $WebClient.DownloadFile("<my_personal_storage_account_web_application_path>.zip",$destination)
       $Argument = '-source:package="C:\WindowsAzure\WebApplication.zip" -dest:auto,ComputerName="localhost", -verb:sync -allowUntrusted'
       $MSDeployPath = (Get-ChildItem "HKLM:\SOFTWARE\Microsoft\IIS Extensions\MSDeploy" | Select -Last 1).GetValue("InstallPath")
       Start-Process "$MSDeployPath\msdeploy.exe" $Argument -Verb runas 

    After running the ARM template, all the resources are deployed correctly, but the web app is not deployed to IIS. I checked that the IIS and Web deploy are installed as according to the script. I also see that my web application zip file is downloaded from my storage account to C:\WindowsAzure\WebApplication.zip. Appreciate if anyone can advise me on the correct coding for the #Deploy Web App package part.

    I can manually publish my app to the VM from Visual Studio after running the ARM template. But still I want to know how do that in automated way via CustomScriptExtension/powershell

    Many thanks in advance.

    Monday, November 25, 2019 9:16 AM

All replies

  • HI mh_86,

    Go to this directory on the machine where the extension didnt work.

    C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension

    This is the place where the extension logs the outputs.  Look at the log files and share the errors with us,

    If you are unable to find any errors there,

    Then execute the "#Deploy Web App package" code alone and check for errors.

    Monday, November 25, 2019 12:11 PM
  • Hi jakaruna,

    Firstly, thank you for your reply. This is the log file for CustomScriptHandler.txt:

    [4012+00000001] [11/26/2019 12:07:15.20] [INFO] Starting IaaS ScriptHandler Extension v1
    [4012+00000001] [11/26/2019 12:07:15.21] [INFO] HandlerEnvironment = Version: 1, HandlerEnvironment: [LogFolder: "C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3", ConfigFolder: "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\RuntimeSettings", StatusFolder: "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\Status", HeartbeatFile: "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\Status\HeartBeat.Json"]
    [4012+00000001] [11/26/2019 12:07:15.21] [INFO] Installing Handler
    [4012+00000001] [11/26/2019 12:07:15.21] [INFO] Handler successfully installed
    [4012+00000001] [11/26/2019 12:07:15.23] [INFO] Removing user-level access rules for 2 directories
    [4012+00000001] [11/26/2019 12:07:15.23] [INFO] Removing Users access rules from directory 'C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3'
    [4012+00000001] [11/26/2019 12:07:15.29] [INFO] Removing Users access rules from directory 'C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3'
    [4012+00000001] [11/26/2019 12:07:15.31] [INFO] Finished removing user-level access rules
    [3236+00000001] [11/26/2019 12:07:21.81] [INFO] Starting IaaS ScriptHandler Extension v1
    [3236+00000001] [11/26/2019 12:07:21.82] [INFO] HandlerEnvironment = Version: 1, HandlerEnvironment: [LogFolder: "C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3", ConfigFolder: "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\RuntimeSettings", StatusFolder: "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\Status", HeartbeatFile: "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\Status\HeartBeat.Json"]
    [3236+00000001] [11/26/2019 12:07:21.82] [INFO] Enabling Handler
    [3236+00000001] [11/26/2019 12:07:21.82] [INFO] Successfully enabled TLS.
    [3236+00000001] [11/26/2019 12:07:21.84] [INFO] Handler successfully enabled
    [3236+00000001] [11/26/2019 12:07:21.85] [INFO] Loading configuration for sequence number 0
    [3236+00000001] [11/26/2019 12:07:21.89] [INFO] HandlerSettings = ProtectedSettingsCertThumbprint: , ProtectedSettings: {}, PublicSettings: {FileUris: [<my_personal_storage_path>/webdeploy.ps1], CommandToExecute: powershell.exe -ExecutionPolicy Unrestricted -File webdeploy.ps1}
    [3236+00000001] [11/26/2019 12:07:21.90] [INFO] Downloading files specified in configuration...
    [3236+00000001] [11/26/2019 12:07:22.01] [INFO] DownloadFiles: fileDownloadPath = C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\Downloads\0
    [3236+00000001] [11/26/2019 12:07:22.01] [INFO] WebClientDownloader: starting download fileUri = <my_personal_storage_path>/webdeploy.ps1
    [3236+00000001] [11/26/2019 12:07:22.98] [INFO] WebClientDownloader: Downloaded file to fileDownloadLocation = Downloads\0\webdeploy.ps1
    [3236+00000001] [11/26/2019 12:07:22.98] [INFO] Files downloaded. Asynchronously executing command: 'powershell.exe -ExecutionPolicy Unrestricted -File webdeploy.ps1'
    [3236+00000001] [11/26/2019 12:07:22.99] [INFO] Command execution task started. Awaiting completion...
    [3236+00000005] [11/26/2019 12:18:30.73] [INFO] Process has exited. Waiting for pending data from output and error streams.
    [3236+00000005] [11/26/2019 12:18:30.73] [INFO] Output stream pending data received.
    [3236+00000005] [11/26/2019 12:18:30.78] [INFO] Error stream pending data received.
    [3236+00000001] [11/26/2019 12:18:30.79] [INFO] Command execution finished. Command exited with code: 0
    [5584+00000001] [11/26/2019 12:21:34.06] [INFO] Starting IaaS ScriptHandler Extension v1
    [5584+00000001] [11/26/2019 12:21:34.07] [INFO] HandlerEnvironment = Version: 1, HandlerEnvironment: [LogFolder: "C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3", ConfigFolder: "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\RuntimeSettings", StatusFolder: "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\Status", HeartbeatFile: "C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\Status\HeartBeat.Json"]
    [5584+00000001] [11/26/2019 12:21:34.08] [INFO] Enabling Handler
    [5584+00000001] [11/26/2019 12:21:34.09] [INFO] Successfully enabled TLS.
    [5584+00000001] [11/26/2019 12:21:34.15] [INFO] Handler successfully enabled
    [5584+00000001] [11/26/2019 12:21:34.16] [INFO] Loading configuration for sequence number 0
    [5584+00000001] [11/26/2019 12:21:34.19] [INFO] HandlerSettings = ProtectedSettingsCertThumbprint: , ProtectedSettings: {}, PublicSettings: {FileUris: [<my_personal_storage_path>/webdeploy.ps1], CommandToExecute: powershell.exe -ExecutionPolicy Unrestricted -File webdeploy.ps1}
    [5584+00000001] [11/26/2019 12:21:34.19] [WARN] Current sequence number, 0, is not greater than the sequence number of the most recently executed configuration. Exiting...

    Do note that I have replaced the FileUris in the log from my actual personal storage link to <my_personal_storage_path>.

    Here's the CommandExecution log:

    [11/26/2019 12:21:28.13] Executing: C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\enable.cmd 
    [11/26/2019 12:21:33.78] Execution Complete.
    ######
    Execution Output:
    C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3>if not exist RuntimeSettings\*.settings exit /b -2 
    C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3>start cmd /C bin\CustomScriptHandler.exe "enable" 


    Execution Error:

    ######
     Number of Tries:
     1Command C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.3\enable.cmd of Microsoft.Compute.CustomScriptExtension has exited with Exit code: 0
    Plugin (name: Microsoft.Compute.CustomScriptExtension, version: 1.10.3) completed successfully.

    I tried running portion of the Web deploy code from the Webdeploy.ps1 script, as shown below manually in VM but the ExitCode is 0. Actually I just want to know if the code below is correct to deploy a web app to VM. 

    $Argument = '-source:package="C:\WindowsAzure\WebApplication.zip" -dest:auto,ComputerName="localhost", -verb:sync -allowUntrusted'
    $MSDeployPath = (Get-ChildItem "HKLM:\SOFTWARE\Microsoft\IIS Extensions\MSDeploy" | Select -Last 1).GetValue("InstallPath")
    $Process = Start-Process "$MSDeployPath\msdeploy.exe" $Argument -Verb runas -PassThru
    $Process.ExitCode


    • Edited by mh_86 Tuesday, November 26, 2019 12:53 PM
    Tuesday, November 26, 2019 12:51 PM
  • Hi, 

    I dont have experience in deploying a web app. I thought of helping you to solve the problem with custom script extension.

    If your problem is not solved yet, Send your subscription id to this email id AzCommunity@microsoft.com with this thread url.

    I can create a free support ticket to address this problem.

    Wednesday, December 4, 2019 4:53 AM