运行 使用 Azure Fluent 的 Azure VM 上的 powershell 脚本 API

Run powershell script on Azure VM with Azure Fluent API

我正在尝试 运行 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();

但是,由于某些原因可能需要很长时间 运行ning 过程,我收到此错误 :-

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

根据我的测试,我们可以使用下面的代码来安装带有自定义脚本扩展的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();