Search code examples
windowspowershellazurebootstrappingazure-xplat-cli

Azure xplat to run a CustomScriptExtension in a Windows VM


I am creating Windows VMs from the azure xplat cli, using the following command:

azure network vnet create --location "East US" testnet
azure vm create --vm-name xplattest3 --location "East US" --virtual-network-name testnet --rdp 3389 xplattest3 ad072bd3082149369c449ba5832401ae__Windows-Server-Remote-Desktop-Session-Host-on-Windows-Server-2012-R2-20150828-0350 username SAFEpassword!

After the Windows VM is created I would like to execute a powershell script to configure the server. As far I understand, this is done by executing a CustomScriptExtension.

I found several examples for PowerShell but no examples for Xplat cli.

I would like, for example, to run the following HelloWorld PowerShell script:

New-Item -ItemType directory -Path C:\HelloWorld

After reading documentation I should be able to run a CustomExtensionScript by executing something like this (the following command does not work):

azure vm extension set xplattest3 CustomScriptExtension Microsoft.Compute 1.4 -i '{"URI":["https://gist.githubusercontent.com/tk421/8b7dd37145eaa8f82e2f/raw/36c11aafd3f5d6b4af97aab9ef5303d80e8ab29b/azureCustomScriptExtensionTest"] }'

I think that the problem is the parameter -i. I have not been able to find an example on Internet. There are some references and documentation such as MSDN and Github, but no examples.

Therefore, my question: How to execute a PowerShell script after creating a Windows VM in Azure using the xplat cli ?

Please note that the my current approach is a CustomScriptExtension, but anything that allows to bootstrap a configuration script will be considered!

EDIT How do I know it is failing ?

After I run the command azure vm extension ...:

  1. xplat cli confirms that the command has been executed properly.
  2. As per MSDN documentation, the folder C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\ is created, but there is no script downloaded to C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\{version-number}\Downloads\{iteration}
  3. The folder C:\HelloWorld is not created, which means that the contents of the script has not been executed.
  4. I cannot find any sort of logs or a trace to know what happened. Does anyone knows where can I find this information ?

Solution

  • The parameters (The Json) that I used after reading the MSDN documentation were not correct. However, you can get clues of the correct parameters by reading the C# code.

    And the final command is:

    azure vm extension set xplattest3 CustomScriptExtension Microsoft.Compute 1.4 -i '{"fileUris":["https://macstoragetest.blob.core.windows.net/testcontainername/createFolder.ps1"], "commandToExecute": "powershell -ExecutionPolicy Unrestricted -file createFolder.ps1" }'
    

    This command successfully creates the C:\HelloWorld directory.

    NOTE: I decided to upload the script to Azure as I read in a post and in the documentation that is mandatory. However I just made a test to download the original script from Github and it is working fine, so I guess that the documentation is a bit outdated.

    EDIT: I created an detailed article that explains how to provision windows servers with xplat-cli in Azure.