Search code examples
azure-sdk-.netazure-fluent-api

Run powershell script on Azure VM with Azure Fluent API


I am trying to run powershell command to install mysql on AzureVM, but I am not able to succeed.

var windowsVmAccessExtensionName = "VMAccessAgent";
            var windowsVmAccessExtensionPublisherName = "Microsoft.Compute";
            var windowsVmAccessExtensionTypeName = "VMAccessAgent";
            var windowsVmAccessExtensionVersionName = "2.3";
            var mySqlScriptWindowsInstallCommand = "powershell.exe -ExecutionPolicy Unrestricted -File installMySQL.ps1";
            var mySQLWindowsInstallScriptFileUris = new List<string>()
        {
            "https://raw.githubusercontent.com/Azure/azure-libraries-for-net/master/Samples/Asset/installMySQL.ps1"
        };

            //azure.VirtualMachines.RunPowerShellScript(rgName, vmName, mySQLWindowsInstallScriptFileUris, asdf);

            var windowsVM = azure.VirtualMachines.GetByResourceGroup(rgName, vmName);

            windowsVM.Update()
                       .DefineNewExtension(windowsVmAccessExtensionName)
                           .WithPublisher(windowsVmAccessExtensionPublisherName)
                           .WithType(windowsVmAccessExtensionTypeName)
                           .WithVersion(windowsVmAccessExtensionVersionName)
                           .WithPublicSetting("fileUris", mySQLWindowsInstallScriptFileUris)
                           .WithPublicSetting("commandToExecute", mySqlScriptWindowsInstallCommand)
                       .Attach()
                       .Apply();

this code even not throwing any error as well, and when I check the VM there is no any mysql or choco installed on VM.

Please suggest or help on Azure FLuent API to execute command of powershell on Azure VM.

UPDATE

Update the below code able to run powershell script.

var scriptUris = new List<string>()
        {
            "https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-windows/scripts/configure-music-app.ps1"
        };

        var windowsVM = azure.VirtualMachines.GetByResourceGroup(rgName, vmName);
        windowsVM.Update()
                   .UpdateExtension("CustomScriptExtension")
                    //.WithPublisher(windowsVmAccessExtensionPublisherName)
                    //.WithType(windowsVmAccessExtensionTypeName)
                    //.WithVersion(windowsVmAccessExtensionVersionName)
                       .WithPublicSetting("fileUris", scriptUris)
                       .WithPublicSetting("commandToExecute", "powershell -ExecutionPolicy Unrestricted -File configure-music-app.ps1")
                    //.Attach()
                    //.Apply();
                    .Parent()
                    .Apply();

But, for some reason might be for long running process I am getting this error :-

One or more errors occurred. (Long running operation failed with status 'Failed'. Additional Info:'VM has reported a failure when processing extension 'CustomScriptExtension'. Error message: "Finished executing command"

More information on troubleshooting is available at https://aka.ms/VMExtensionCSEWindowsTroubleshoot


Solution

  • According to my test, we can use the following code to install MySQL with custom script extension.

    var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud);
                 var azure = Microsoft.Azure.Management.Fluent.Azure.Configure()
                             .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                             .Authenticate(credentials)
                             .WithSubscription(SubscriptionId);
                var windowsVmAccessExtensionName = "installmysql";
                var windowsVmAccessExtensionPublisherName = "Microsoft.Compute";
                var windowsVmAccessExtensionTypeName = "CustomScriptExtension";
                var windowsVmAccessExtensionVersionName = "1.9";
                var mySqlScriptWindowsInstallCommand = "powershell.exe -ExecutionPolicy Unrestricted -File installMySQL.ps1";
                var mySQLWindowsInstallScriptFileUris = new List<string>()
            {
                "https://raw.githubusercontent.com/Azure/azure-libraries-for-net/master/Samples/Asset/installMySQL.ps1"
            };
    
                //azure.VirtualMachines.RunPowerShellScript(rgName, vmName, mySQLWindowsInstallScriptFileUris, asdf);
    
                var windowsVM = azure.VirtualMachines.GetByResourceGroup("testInstance", "test");
    
                windowsVM.Update()
                           .DefineNewExtension(windowsVmAccessExtensionName)
                               .WithPublisher(windowsVmAccessExtensionPublisherName)
                               .WithType(windowsVmAccessExtensionTypeName)
                               .WithVersion(windowsVmAccessExtensionVersionName)
                               .WithPublicSetting("fileUris", mySQLWindowsInstallScriptFileUris)
                               .WithProtectedSetting("commandToExecute", mySqlScriptWindowsInstallCommand)
                           .Attach()
                           .Apply();
    

    enter image description here enter image description here