Run PowerShell Script on Azure VM with ComputeManagementClient

I wanted to run a PowerShell script on a VM deployed in Azure trough the Azure Management API.

Theoretically there is a method ComputeManagementClient.VirtualMachines.RunCommandAsync but I didn't know what I should pass as Parameter.

I checked the documentation, but it is just auto generated and didn't help me much.

Thankfully after some extensive use of google and github searching, I found an example in a UnitTest.

This example helped me to understand how to use RunCommandInput, but using echo on VM in azure isn't a great idea to test if it works.

I used this for testing if it really works:

var runCommandInput = new RunCommandInput()
{
	CommandId = "RunPowerShellScript",
	Script = new List<string>() {
				"param(",
				"    [string]$path,",
				"    [string]$name",
				")",
				"New-Item -Path $path -Name $name -ItemType 'directory'"
			},
	Parameters = new List<RunCommandInputParameter>()
			{
				new RunCommandInputParameter("path","D:"),
				new RunCommandInputParameter("name","PowerShellSciptTest"),
			}
};

await vmClient.VirtualMachines.RunCommandAsync(resourceGroupName, vmName, runCommandInput);

It creates a folder named PowerShellSciptTest on the temporary disk (D:) of the Azure VM.

The Script gets executed under the local machine account (MACHINENAME$).