public override void ExecuteCmdlet()
        {
            this._Helper = new AEMHelper((err) => this.WriteError(err), (msg) => this.WriteVerbose(msg), (msg) => this.WriteWarning(msg),
                                         this.CommandRuntime.Host.UI,
                                         AzureSession.Instance.ClientFactory.CreateArmClient <StorageManagementClient>(DefaultProfile.DefaultContext, AzureEnvironment.Endpoint.ResourceManager),
                                         this.DefaultContext.Subscription,
                                         this.DefaultContext.Environment.GetEndpoint(AzureEnvironment.Endpoint.StorageEndpointSuffix));

            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                VirtualMachine virtualMachine = null;
                if (String.IsNullOrEmpty(this.OSType))
                {
                    virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);
                    this.OSType    = virtualMachine.StorageProfile.OsDisk.OsType.ToString();
                }
                if (String.IsNullOrEmpty(this.OSType))
                {
                    this._Helper.WriteError("Could not determine Operating System of the VM. Please provide the Operating System type ({0} or {1}) via parameter OSType",
                                            AEMExtensionConstants.OSTypeWindows, AEMExtensionConstants.OSTypeLinux);
                    return;
                }

                if (string.IsNullOrEmpty(this.Name))
                {
                    if (virtualMachine == null)
                    {
                        virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);
                    }
                    var aemExtension = AEMHelper.GetAEMExtension(virtualMachine, this.OSType);

                    if (aemExtension == null)
                    {
                        WriteWarning(string.Format(CultureInfo.InvariantCulture, Properties.Resources.AEMExtensionNotFound, this.ResourceGroupName, this.VMName));
                        return;
                    }
                    else
                    {
                        this.Name = aemExtension.Name;
                    }
                }

                if (NoWait.IsPresent)
                {
                    var op = this.VirtualMachineExtensionClient.BeginDeleteWithHttpMessagesAsync(this.ResourceGroupName, this.VMName, this.Name).GetAwaiter().GetResult();
                    WriteObject(ComputeAutoMapperProfile.Mapper.Map <PSAzureOperationResponse>(op));
                }
                else
                {
                    var op     = this.VirtualMachineExtensionClient.DeleteWithHttpMessagesAsync(this.ResourceGroupName, this.VMName, this.Name).GetAwaiter().GetResult();
                    var result = ComputeAutoMapperProfile.Mapper.Map <PSAzureOperationResponse>(op);
                    WriteObject(result);
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                if (string.IsNullOrEmpty(this.Name))
                {
                    var virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);

                    var osdisk = virtualMachine.StorageProfile.OsDisk;
                    if (String.IsNullOrEmpty(this.OSType))
                    {
                        this.OSType = osdisk.OsType.ToString();
                    }
                    if (String.IsNullOrEmpty(this.OSType))
                    {
                        WriteError("Could not determine Operating System of the VM. Please provide the Operating System type ({0} or {1}) via parameter OSType",
                                   AEMExtensionConstants.OSTypeWindows, AEMExtensionConstants.OSTypeLinux);
                        return;
                    }

                    var aemExtension = AEMHelper.GetAEMExtension(virtualMachine, this.OSType);

                    if (aemExtension == null)
                    {
                        WriteObject(null);
                        return;
                    }
                    else
                    {
                        this.Name = aemExtension.Name;
                    }
                }

                AzureOperationResponse <VirtualMachineExtension> virtualMachineExtensionGetResponse = null;
                if (Status.IsPresent)
                {
                    virtualMachineExtensionGetResponse =
                        this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName,
                                                                               this.VMName, this.Name);
                }
                else
                {
                    virtualMachineExtensionGetResponse = this.VirtualMachineExtensionClient.GetWithHttpMessagesAsync(
                        this.ResourceGroupName,
                        this.VMName,
                        this.Name).GetAwaiter().GetResult();
                }

                var returnedExtension = virtualMachineExtensionGetResponse.ToPSVirtualMachineExtension(
                    this.ResourceGroupName, this.VMName);

                WriteObject(returnedExtension);
            });
        }
        private VirtualMachine SetAzureVMDiagnosticsExtensionC(VirtualMachine vm, VirtualMachineInstanceView vmStatus, string storageAccountName, string storageAccountKey)
        {
            System.Xml.XmlDocument xpublicConfig = null;

            var extensionName = AEMExtensionConstants.WADExtensionDefaultName[this.OSType];

            var    extTemp    = AEMHelper.GetAEMExtension(vm, this.OSType);
            object publicConf = null;

            if (extTemp != null)
            {
                publicConf    = extTemp.Settings;
                extensionName = extTemp.Name;
            }

            if (publicConf != null)
            {
                var jpublicConf = publicConf as Newtonsoft.Json.Linq.JObject;
                if (jpublicConf == null)
                {
                    throw new ArgumentException();
                }

                var base64 = jpublicConf["xmlCfg"] as Newtonsoft.Json.Linq.JValue;
                if (base64 == null)
                {
                    throw new ArgumentException();
                }

                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.LoadXml(Encoding.UTF8.GetString(System.Convert.FromBase64String(base64.Value.ToString())));

                if (xDoc.SelectSingleNode("/WadCfg/DiagnosticMonitorConfiguration/PerformanceCounters") != null)
                {
                    xDoc.SelectSingleNode("WadCfg/DiagnosticMonitorConfiguration").Attributes["overallQuotaInMB"].Value = "4096";
                    xDoc.SelectSingleNode("WadCfg/DiagnosticMonitorConfiguration/PerformanceCounters").Attributes["scheduledTransferPeriod"].Value = "PT1M";

                    xpublicConfig = xDoc;
                }
            }

            if (xpublicConfig == null)
            {
                xpublicConfig = new System.Xml.XmlDocument();
                xpublicConfig.LoadXml(AEMExtensionConstants.WADConfigXML);
            }

            foreach (var perfCounter in AEMExtensionConstants.PerformanceCounters[OSType])
            {
                var currentCounter = xpublicConfig.
                                     SelectSingleNode("WadCfg/DiagnosticMonitorConfiguration/PerformanceCounters/PerformanceCounterConfiguration[@counterSpecifier = '" + perfCounter.counterSpecifier + "']");
                if (currentCounter == null)
                {
                    var node = xpublicConfig.CreateElement("PerformanceCounterConfiguration");
                    xpublicConfig.SelectSingleNode("WadCfg/DiagnosticMonitorConfiguration/PerformanceCounters").AppendChild(node);
                    node.SetAttribute("counterSpecifier", perfCounter.counterSpecifier);
                    node.SetAttribute("sampleRate", perfCounter.sampleRate);
                }
            }

            var endpoint = this._Helper.GetCoreEndpoint(storageAccountName);

            endpoint = "https://" + endpoint;

            Newtonsoft.Json.Linq.JObject jPublicConfig = new Newtonsoft.Json.Linq.JObject();
            jPublicConfig.Add("xmlCfg", new Newtonsoft.Json.Linq.JValue(System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(xpublicConfig.InnerXml))));

            Newtonsoft.Json.Linq.JObject jPrivateConfig = new Newtonsoft.Json.Linq.JObject();
            jPrivateConfig.Add("storageAccountName", new Newtonsoft.Json.Linq.JValue(storageAccountName));
            jPrivateConfig.Add("storageAccountKey", new Newtonsoft.Json.Linq.JValue(storageAccountKey));
            jPrivateConfig.Add("storageAccountEndPoint", new Newtonsoft.Json.Linq.JValue(endpoint));

            WriteVerbose("Installing WAD extension");

            Version wadVersion = this._Helper.GetExtensionVersion(vm, vmStatus, OSType,
                                                                  AEMExtensionConstants.WADExtensionType[this.OSType], AEMExtensionConstants.WADExtensionPublisher[this.OSType]);

            VirtualMachineExtension vmExtParameters = new VirtualMachineExtension();

            vmExtParameters.Publisher = AEMExtensionConstants.WADExtensionPublisher[this.OSType];
            vmExtParameters.VirtualMachineExtensionType = AEMExtensionConstants.WADExtensionType[this.OSType];
            vmExtParameters.TypeHandlerVersion          = wadVersion.ToString(2);
            vmExtParameters.Settings                = jPublicConfig;
            vmExtParameters.ProtectedSettings       = jPrivateConfig;
            vmExtParameters.Location                = vm.Location;
            vmExtParameters.AutoUpgradeMinorVersion = true;
            vmExtParameters.ForceUpdateTag          = DateTime.Now.Ticks.ToString();

            this.VirtualMachineExtensionClient.CreateOrUpdate(ResourceGroupName, vm.Name, extensionName, vmExtParameters);

            return(this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(ResourceGroupName, vm.Name));
        }
        public override void ExecuteCmdlet()
        {
            this._StorageEndpoint = this.DefaultContext.Environment.GetEndpoint(AzureEnvironment.Endpoint.StorageEndpointSuffix);
            this._Helper          = new AEMHelper((err) => this.WriteError(err), (msg) => this.WriteVerbose(msg), (msg) => this.WriteWarning(msg),
                                                  this.CommandRuntime.Host.UI,
                                                  AzureSession.Instance.ClientFactory.CreateArmClient <StorageManagementClient>(
                                                      DefaultProfile.DefaultContext, AzureEnvironment.Endpoint.ResourceManager),
                                                  this.DefaultContext.Subscription,
                                                  this._StorageEndpoint);

            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                this._Helper.WriteVerbose("Retrieving VM...");

                var selectedVM       = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);
                var selectedVMStatus = ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView(this.ResourceGroupName, this.VMName).Body.InstanceView;

                if (selectedVM == null)
                {
                    var subscriptionId = this.DefaultContext.Subscription.Id;
                    this._Helper.WriteError("No virtual machine with name {0} in resource group {1} in subscription {2} found", this.VMName, this.ResourceGroupName, subscriptionId);
                    return;
                }

                var osdisk = selectedVM.StorageProfile.OsDisk;

                if (String.IsNullOrEmpty(this.OSType))
                {
                    this.OSType = osdisk.OsType.ToString();
                }
                if (String.IsNullOrEmpty(this.OSType))
                {
                    this._Helper.WriteError("Could not determine Operating System of the VM. Please provide the Operating System type ({0} or {1}) via parameter OSType",
                                            AEMExtensionConstants.OSTypeWindows, AEMExtensionConstants.OSTypeLinux);
                    return;
                }

                var aemExtension = AEMHelper.GetAEMExtension(selectedVM, this.OSType);

                /*
                 * no extension + new extension switch => install new extension
                 * new extension + new extension switch => install new extension
                 * new extension + no extension switch => install new extension
                 * no extension + no new extension switch => install old extension
                 * old extension + no new extension switch => install old extension
                 * old extension + new extension switch => error
                 */
                if ((aemExtension == null && InstallNewExtension.IsPresent) ||
                    (AEMHelper.IsNewExtension(aemExtension, this.OSType)))
                {
                    this.SetNewExtension(selectedVM, selectedVMStatus);
                }
                else if ((aemExtension == null && !InstallNewExtension.IsPresent) ||
                         (AEMHelper.IsOldExtension(aemExtension, this.OSType) && !InstallNewExtension.IsPresent))
                {
                    this.SetOldExtension(selectedVM, selectedVMStatus);
                }
                else
                {
                    this._Helper.WriteVerbose($"Migration from the old extension to the new one is not supported. " +
                                              $"Please remove the old extension first. (" +
                                              $"Extension installed={aemExtension != null} " +
                                              $"IsNewExtension={AEMHelper.IsNewExtension(aemExtension, this.OSType)} " +
                                              $"IsOldExtension={AEMHelper.IsOldExtension(aemExtension, this.OSType)}" +
                                              $"InstallNewExtension={InstallNewExtension.IsPresent}");
                    this._Helper.WriteError("Migration from the old extension to the new one is not supported. Please remove the old extension first.");
                    return;
                }
            });
        }
示例#5
0
        public override void ExecuteCmdlet()
        {
            this._Helper = new AEMHelper((err) => this.WriteError(err), (msg) => this.WriteVerbose(msg), (msg) => this.WriteWarning(msg),
                                         this.CommandRuntime.Host.UI,
                                         AzureSession.Instance.ClientFactory.CreateArmClient <StorageManagementClient>(DefaultProfile.DefaultContext, AzureEnvironment.Endpoint.ResourceManager),
                                         this.DefaultContext.Subscription,
                                         this.DefaultContext.Environment.GetEndpoint(AzureEnvironment.Endpoint.StorageEndpointSuffix));

            this._Helper.WriteVerbose("Starting TestAzureRmVMAEMExtension");

            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                AEMTestResult rootResult = new AEMTestResult();
                rootResult.TestName      = "Azure Enhanced Monitoring Test";

                //#################################################
                //# Check if VM exists
                //#################################################
                this._Helper.WriteHost("VM Existence check for {0} ...", false, this.VMName);
                var selectedVM       = this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);
                var selectedVMStatus = this.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView(this.ResourceGroupName, this.VMName).Body.InstanceView;


                if (selectedVM == null)
                {
                    rootResult.PartialResults.Add(new AEMTestResult("VM Existence check for {0}", false, this.VMName));
                    this._Helper.WriteHost("NOT OK ", ConsoleColor.Red);
                    return;
                }
                else
                {
                    rootResult.PartialResults.Add(new AEMTestResult("VM Existence check for {0}", true, this.VMName));
                    this._Helper.WriteHost("OK ", ConsoleColor.Green);
                }
                //#################################################
                //#################################################
                var osdisk = selectedVM.StorageProfile.OsDisk;
                if (String.IsNullOrEmpty(this.OSType))
                {
                    this.OSType = osdisk.OsType.ToString();
                }
                if (String.IsNullOrEmpty(this.OSType))
                {
                    this._Helper.WriteError("Could not determine Operating System of the VM. Please provide the Operating System type ({0} or {1}) via parameter OSType", AEMExtensionConstants.OSTypeWindows, AEMExtensionConstants.OSTypeLinux);
                    return;
                }

                //#################################################
                //# Check for Azure Enhanced Monitoring Extension for SAP
                //#################################################

                var monExtension = AEMHelper.GetAEMExtension(selectedVM, this.OSType);
                if (AEMHelper.IsNewExtension(monExtension, this.OSType))
                {
                    var newResults = this.TestNewExtension(selectedVM, selectedVMStatus, monExtension);
                    rootResult.PartialResults.AddRange(newResults);
                }
                else
                {
                    var oldResults = this.TestOldExtension(selectedVM, selectedVMStatus, monExtension);
                    rootResult.PartialResults.AddRange(oldResults);
                }

                if (!rootResult.Result)
                {
                    this._Helper.WriteHost("The script found some configuration issues. Please run the Set-AzVMAEMExtension commandlet to update the configuration of the virtual machine!");
                }

                this._Helper.WriteVerbose("TestAzureRmVMAEMExtension Done (" + rootResult.Result + ")");

                var result = ComputeAutoMapperProfile.Mapper.Map <AEMTestResult>(rootResult);
                WriteObject(result);
            });
        }