0

我正在尝试运行 powershell 命令在 AzureVM 上安装 mysql,但我无法成功。

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();

这段代码甚至没有抛出任何错误,当我检查 VM 时,VM 上没有安装任何 mysql 或 choco。

请建议或帮助 Azure Fluent API 在 Azure VM 上执行 powershell 命令。

更新

更新以下能够运行 powershell 脚本的代码。

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();

但是,由于某种原因可能是长时间运行的过程,我收到了这个错误:-

发生一个或多个错误。(长时间运行的操作失败,状态为 'Failed'。附加信息:'VM 在处理扩展 'CustomScriptExtension' 时报告失败。错误消息:“完成执行命令”

有关故障排除的更多信息,请访问https://aka.ms/VMExtensionCSEWindowsTroubleshoot

4

1 回答 1

1

根据我的测试,我们可以使用以下代码来安装带有自定义脚本扩展的 MySQL。

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();

在此处输入图像描述 在此处输入图像描述

于 2019-12-19T02:16:19.870 回答