Пример #1
0
        protected void ExecuteVirtualMachineScaleSetVMGetMethod(object[] invokeMethodInputParameters)
        {
            string resourceGroupName = (string)ParseParameter(invokeMethodInputParameters[0]);
            string vmScaleSetName    = (string)ParseParameter(invokeMethodInputParameters[1]);
            string instanceId        = (string)ParseParameter(invokeMethodInputParameters[2]);

            if (!string.IsNullOrEmpty(resourceGroupName) && !string.IsNullOrEmpty(vmScaleSetName) && !string.IsNullOrEmpty(instanceId))
            {
                var result = VirtualMachineScaleSetVMsClient.Get(resourceGroupName, vmScaleSetName, instanceId);
                WriteObject(result);
            }
            else if (!string.IsNullOrEmpty(resourceGroupName) && !string.IsNullOrEmpty(vmScaleSetName))
            {
                var result       = VirtualMachineScaleSetVMsClient.List(resourceGroupName, vmScaleSetName);
                var resultList   = result.ToList();
                var nextPageLink = result.NextPageLink;
                while (!string.IsNullOrEmpty(nextPageLink))
                {
                    var pageResult = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink);
                    foreach (var pageItem in pageResult)
                    {
                        resultList.Add(pageItem);
                    }
                    nextPageLink = pageResult.NextPageLink;
                }
                WriteObject(resultList, true);
            }
        }
        protected void ExecuteVirtualMachineScaleSetVMListNextMethod(object[] invokeMethodInputParameters)
        {
            string nextPageLink = (string)ParseParameter(invokeMethodInputParameters[0]);

            var result = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink);

            WriteObject(result);
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                string resourceGroupName = this.ResourceGroupName;
                string vmScaleSetName    = this.VMScaleSetName;
                string instanceId        = this.InstanceId;

                if (!string.IsNullOrEmpty(resourceGroupName) && !string.IsNullOrEmpty(vmScaleSetName) && !string.IsNullOrEmpty(instanceId))
                {
                    if (this.ParameterSetName.Equals("FriendMethod"))
                    {
                        var result   = VirtualMachineScaleSetVMsClient.GetInstanceView(resourceGroupName, vmScaleSetName, instanceId);
                        var psObject = new PSVirtualMachineScaleSetVMInstanceView();
                        ComputeAutomationAutoMapperProfile.Mapper.Map <VirtualMachineScaleSetVMInstanceView, PSVirtualMachineScaleSetVMInstanceView>(result, psObject);
                        WriteObject(psObject);
                    }
                    else
                    {
                        var result   = VirtualMachineScaleSetVMsClient.Get(resourceGroupName, vmScaleSetName, instanceId);
                        var psObject = new PSVirtualMachineScaleSetVM();
                        ComputeAutomationAutoMapperProfile.Mapper.Map <VirtualMachineScaleSetVM, PSVirtualMachineScaleSetVM>(result, psObject);
                        WriteObject(psObject);
                    }
                }
                else if (!string.IsNullOrEmpty(resourceGroupName) && !string.IsNullOrEmpty(vmScaleSetName))
                {
                    var result       = VirtualMachineScaleSetVMsClient.List(resourceGroupName, vmScaleSetName);
                    var resultList   = result.ToList();
                    var nextPageLink = result.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }
                    var psObject = new List <PSVirtualMachineScaleSetVMList>();
                    foreach (var r in resultList)
                    {
                        psObject.Add(ComputeAutomationAutoMapperProfile.Mapper.Map <VirtualMachineScaleSetVM, PSVirtualMachineScaleSetVMList>(r));
                    }
                    WriteObject(psObject, true);
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            string resourceGroupName;
            string vmScaleSetName;

            resourceGroupName = this.ResourceGroupName;
            vmScaleSetName    = this.VMScaleSetName;

            var expand = new Microsoft.Rest.Azure.OData.ODataQuery <VirtualMachineScaleSetVM>();

            expand.Expand = "instanceView";
            var vmList       = VirtualMachineScaleSetVMsClient.List(resourceGroupName, vmScaleSetName, expand);
            var resultList   = vmList.ToList();
            var nextPageLink = vmList.NextPageLink;

            while (!string.IsNullOrEmpty(nextPageLink))
            {
                var pageResult = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink);
                foreach (var pageItem in pageResult)
                {
                    resultList.Add(pageItem);
                }
                nextPageLink = pageResult.NextPageLink;
            }
            List <VirtualMachineScaleSetVM> listOfVms = new List <VirtualMachineScaleSetVM>(resultList);

            Rest.Azure.AzureOperationResponse result = null;
            foreach (VirtualMachineScaleSetVM vm in listOfVms)
            {
                result = VirtualMachineScaleSetVMRunCommandsClient.BeginDeleteWithHttpMessagesAsync(resourceGroupName, vmScaleSetName, vm.InstanceId, this.RunCommandName).GetAwaiter().GetResult();
            }

            PSOperationStatusResponse output = new PSOperationStatusResponse
            {
                StartTime = this.StartTime,
                EndTime   = DateTime.Now
            };

            if (result != null && result.Request != null && result.Request.RequestUri != null)
            {
                output.Name = GetOperationIdFromUrlString(result.Request.RequestUri.ToString());
            }

            WriteObject(output);
        }
Пример #5
0
        protected void ExecuteVirtualMachineScaleSetVMListMethod(object[] invokeMethodInputParameters)
        {
            string resourceGroupName = (string)ParseParameter(invokeMethodInputParameters[0]);
            string virtualMachineScaleSetName = (string)ParseParameter(invokeMethodInputParameters[1]);
            Microsoft.Rest.Azure.OData.ODataQuery<VirtualMachineScaleSetVM> odataQuery = (Microsoft.Rest.Azure.OData.ODataQuery<VirtualMachineScaleSetVM>)ParseParameter(invokeMethodInputParameters[2]);
            string select = (string)ParseParameter(invokeMethodInputParameters[3]);

            var result = VirtualMachineScaleSetVMsClient.List(resourceGroupName, virtualMachineScaleSetName, odataQuery, select);
            var resultList = result.ToList();
            var nextPageLink = result.NextPageLink;
            while (!string.IsNullOrEmpty(nextPageLink))
            {
                var pageResult = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink);
                foreach (var pageItem in pageResult)
                {
                    resultList.Add(pageItem);
                }
                nextPageLink = pageResult.NextPageLink;
            }
            WriteObject(resultList, true);
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                if (string.IsNullOrWhiteSpace(this.InstanceId))
                {
                    var result = this.VirtualMachineScaleSetVMsClient.List(this.ResourceGroupName, this.VMScaleSetName);
                    List <VirtualMachineScaleSetVM> resultList = result.ToList();
                    var nextPageLink = result.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }

                    var psResultList = new List <PSVmssVMDiskEncryptionStatusContextList>();
                    foreach (var vm in resultList)
                    {
                        var diskStatus = GetDiskStatus(this.ResourceGroupName, this.VMScaleSetName, vm.InstanceId);
                        var psResult   = ComputeAutoMapperProfile.Mapper.Map <PSVmssVMDiskEncryptionStatusContextList>(diskStatus);
                        psResultList.Add(psResult);
                    }
                    WriteObject(psResultList);
                }
                else
                {
                    var diskStatus = GetDiskStatus(this.ResourceGroupName, this.VMScaleSetName, this.InstanceId);
                    var psResult   = ComputeAutoMapperProfile.Mapper.Map <PSVmssVMDiskEncryptionStatusContextList>(diskStatus);
                    WriteObject(psResult);
                }
            });
        }
Пример #7
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                if (ShouldProcess(this.VMScaleSetName, "Add"))
                {
                    string resourceGroupName;
                    string vmScaleSetName;
                    switch (this.ParameterSetName)
                    {
                    case "ObjectParameter":
                        resourceGroupName = GetResourceGroupName(this.VirtualMachineScaleSetVM.Id);
                        vmScaleSetName    = GetResourceName(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines");
                        break;

                    default:
                        resourceGroupName = this.ResourceGroupName;
                        vmScaleSetName    = this.VMScaleSetName;
                        break;
                    }
                    VirtualMachineRunCommand vmruncommand;

                    vmruncommand = new VirtualMachineRunCommand
                    {
                        OutputBlobUri    = this.IsParameterBound(c => c.OutputBlobUri) ? this.OutputBlobUri : null,
                        ErrorBlobUri     = this.IsParameterBound(c => c.ErrorBlobUri) ? this.ErrorBlobUri : null,
                        RunAsPassword    = this.IsParameterBound(c => c.RunAsPassword) ? this.RunAsPassword.ToString() : null,
                        RunAsUser        = this.IsParameterBound(c => c.RunAsUser) ? this.RunAsUser : null,
                        TimeoutInSeconds = this.IsParameterBound(c => c.TimeOutInSeconds) ? Convert.ToInt32(this.TimeOutInSeconds) : (int?)null,
                        Location         = this.IsParameterBound(c => c.Location) ? this.Location : null,
                        AsyncExecution   = this.IsParameterBound(c => c.AsyncExecution) ? this.AsyncExecution : null
                    };


                    if (this.IsParameterBound(c => c.ScriptUri))
                    {
                        vmruncommand.Source = new VirtualMachineRunCommandScriptSource
                        {
                            Script    = this.IsParameterBound(c => c.Script) ? this.Script : null,
                            ScriptUri = this.IsParameterBound(c => c.ScriptUri) ? this.ScriptUri : null,
                            CommandId = this.IsParameterBound(c => c.CommandId) ? this.CommandId : null
                        };
                    }
                    else if (this.ScriptPath != null && this.IsParameterBound(c => c.ScriptPath))
                    {
                        List <string> Script       = new List <string>();
                        PathIntrinsics currentPath = SessionState.Path;
                        var filePath       = new System.IO.FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(this.ScriptPath));
                        string fileContent = Commands.Common.Authentication.Abstractions.FileUtilities.DataStore.ReadFileAsText(filePath.FullName);
                        Script             = fileContent.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries).ToList();

                        vmruncommand.Source = new VirtualMachineRunCommandScriptSource(this.Script, this.ScriptPath, this.CommandId);
                    }

                    if (this.Parameter != null)
                    {
                        var vParameter = new List <RunCommandInputParameter>();
                        foreach (var key in this.Parameter.Keys)
                        {
                            RunCommandInputParameter p = new RunCommandInputParameter();

                            p.Name  = key.ToString();
                            p.Value = this.Parameter[key].ToString();
                            vParameter.Add(p);
                        }
                        vmruncommand.Parameters = vParameter;
                    }
                    else if (this.ProtectedParameter != null)
                    {
                        var vParameter = new List <RunCommandInputParameter>();
                        foreach (var key in this.ProtectedParameter.Keys)
                        {
                            RunCommandInputParameter p = new RunCommandInputParameter();

                            p.Name  = key.ToString();
                            p.Value = this.ProtectedParameter[key].ToString();
                            vParameter.Add(p);
                        }
                        vmruncommand.ProtectedParameters = vParameter;
                    }

                    var expand       = new Microsoft.Rest.Azure.OData.ODataQuery <VirtualMachineScaleSetVM>();
                    expand.Expand    = "instanceView";
                    var vmList       = VirtualMachineScaleSetVMsClient.List(resourceGroupName, vmScaleSetName, expand);
                    var resultList   = vmList.ToList();
                    var nextPageLink = vmList.NextPageLink;
                    while (!string.IsNullOrEmpty(nextPageLink))
                    {
                        var pageResult = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink);
                        foreach (var pageItem in pageResult)
                        {
                            resultList.Add(pageItem);
                        }
                        nextPageLink = pageResult.NextPageLink;
                    }
                    List <VirtualMachineScaleSetVM> listOfVms = new List <VirtualMachineScaleSetVM>(resultList);
                    List <PSVirtualMachineRunCommand> output  = new List <PSVirtualMachineRunCommand>();


                    foreach (VirtualMachineScaleSetVM vm in listOfVms)
                    {
                        var resultOfRunCmd = VirtualMachineScaleSetVMRunCommandsClient.BeginCreateOrUpdateAsync(resourceGroupName, vmScaleSetName, vm.InstanceId, this.Name, vmruncommand);
                        var Pstemp         = new PSVirtualMachineRunCommand();
                        ComputeAutomationAutoMapperProfile.Mapper.Map <VirtualMachineRunCommand, PSVirtualMachineRunCommand>(resultOfRunCmd.Result, Pstemp);
                        output.Add(Pstemp);
                    }

                    WriteObject(output);
                }
            });
        }
        private PSVmssDiskEncryptionStatusContext GetVmssDiskStatus(string rgName, string vmssName)
        {
            VirtualMachineScaleSetExtension           ext;
            VirtualMachineScaleSetVMExtensionsSummary extSummary;
            PSVmssDiskEncryptionStatusContext         psResult = new PSVmssDiskEncryptionStatusContext
            {
                ResourceGroupName            = rgName,
                VmScaleSetName               = vmssName,
                EncryptionEnabled            = false,
                EncryptionExtensionInstalled = false
            };

            var vmssResult = this.VirtualMachineScaleSetClient.Get(rgName, vmssName);

            if (vmssResult.VirtualMachineProfile == null ||
                vmssResult.VirtualMachineProfile.ExtensionProfile == null ||
                vmssResult.VirtualMachineProfile.ExtensionProfile.Extensions == null ||
                vmssResult.VirtualMachineProfile.ExtensionProfile.Extensions.Count == 0)
            {
                return(psResult);
            }

            // retrieve installation status of the extension
            SetOSType(vmssResult.VirtualMachineProfile);
            try
            {
                if (string.IsNullOrWhiteSpace(this.ExtensionName))
                {
                    if (this.CurrentOSType == OperatingSystemTypes.Windows)
                    {
                        this.ExtensionName = AzureVmssDiskEncryptionExtensionContext.ExtensionDefaultName;
                    }
                    else
                    {
                        this.ExtensionName = AzureVmssDiskEncryptionExtensionContext.LinuxExtensionDefaultName;
                    }
                }

                ext = vmssResult.VirtualMachineProfile.ExtensionProfile.Extensions.First(
                    e => e.Type.Equals(this.ExtensionName));
            }
            catch (InvalidOperationException)
            {
                return(psResult);
            }
            psResult.EncryptionExtensionInstalled = true;

            // retrieve public configuration settings for the extension
            psResult.EncryptionSettings = JsonConvert.DeserializeObject <AzureVmssDiskEncryptionExtensionPublicSettings>(
                ext.Settings.ToString());

            // retrieve any status summary for the extension
            var vmssInstanceView = this.VirtualMachineScaleSetClient.GetInstanceView(rgName, vmssName);

            if (vmssInstanceView.Extensions == null ||
                vmssInstanceView.Extensions.Count == 0)
            {
                return(psResult);
            }
            try
            {
                extSummary = vmssInstanceView.Extensions.First(e => e.Name.Equals(this.ExtensionName));
            }
            catch (InvalidOperationException)
            {
                return(psResult);
            }
            psResult.EncryptionSummary = extSummary.StatusesSummary;

            // check if encryption is enabled on any disk in the scale set
            // stop evaluation at the first occurrence of an encrypted disk
            var page = this.VirtualMachineScaleSetVMsClient.List(rgName, vmssName);

            while (!psResult.EncryptionEnabled && page != null)
            {
                foreach (var pageItem in page)
                {
                    if (psResult.EncryptionEnabled)
                    {
                        break;
                    }
                    VirtualMachineScaleSetVMInstanceView vmiv = this.VirtualMachineScaleSetVMsClient.GetInstanceView(rgName, vmssName, pageItem.InstanceId);
                    if (vmiv != null && vmiv.Disks != null)
                    {
                        foreach (DiskInstanceView div in vmiv.Disks)
                        {
                            List <InstanceViewStatus> perDiskEncryptionStatuses = new List <InstanceViewStatus>();
                            bool isEncrypted = false;
                            foreach (InstanceViewStatus ivs in div.Statuses)
                            {
                                if (ivs != null && ivs.Code != null && ivs.Code.StartsWith("EncryptionState"))
                                {
                                    if (!psResult.EncryptionEnabled)
                                    {
                                        isEncrypted = ivs.Code.Equals("EncryptionState/encrypted");
                                    }
                                }
                            }
                            if (isEncrypted)
                            {
                                psResult.EncryptionEnabled = true;
                                break;
                            }
                        }
                    }
                }
                // advance to the next page as needed
                page = (page.NextPageLink != null) ? VirtualMachineScaleSetVMsClient.ListNext(page.NextPageLink) : null;
            }

            return(psResult);
        }