0

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

9
  • Could you please provide your powershell script? Commented Dec 18, 2019 at 1:03
  • Besides, could you tell me if you can install mysql with the script manually? Commented Dec 18, 2019 at 2:19
  • @JimXu here is the .ps1 : raw.githubusercontent.com/Azure/azure-libraries-for-net/master/… Manually its working, Commented Dec 18, 2019 at 6:07
  • Ok. I will check it Commented Dec 18, 2019 at 6:30
  • 1
    According to my understanding, you can install the mysql with the script in the VN by yourself. But you cannot install mysql with the script by CustomScriptExtension. Is that right? Commented Dec 18, 2019 at 8:17

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.