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); } }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); ExecuteClientAction(() => { if (ShouldProcess(this.VMScaleSetName, VerbsLifecycle.Invoke)) { string resourceGroupName; string vmScaleSetName; string instanceId; switch (this.ParameterSetName) { case "ResourceIdParameter": resourceGroupName = GetResourceGroupName(this.ResourceId); vmScaleSetName = GetResourceName(this.ResourceId, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines"); instanceId = GetInstanceId(this.ResourceId, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines"); break; case "ObjectParameter": resourceGroupName = GetResourceGroupName(this.VirtualMachineScaleSetVM.Id); vmScaleSetName = GetResourceName(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines"); instanceId = GetInstanceId(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines"); break; default: resourceGroupName = this.ResourceGroupName; vmScaleSetName = this.VMScaleSetName; instanceId = this.InstanceId; break; } RunCommandInput parameters = new RunCommandInput(); parameters.CommandId = this.CommandId; if (this.ScriptPath != null) { parameters.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); parameters.Script = fileContent.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); } 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); } parameters.Parameters = vParameter; } var result = VirtualMachineScaleSetVMsClient.RunCommand(resourceGroupName, vmScaleSetName, instanceId, parameters); var psObject = new PSRunCommandResult(); ComputeAutomationAutoMapperProfile.Mapper.Map <RunCommandResult, PSRunCommandResult>(result, psObject); WriteObject(psObject); } }); }
public override void ExecuteCmdlet() { ExecuteClientAction(() => { if (ShouldProcess(this.VMScaleSetName, VerbsCommon.Set)) { string resourceGroupName = this.ResourceGroupName; string vmScaleSetName = this.VMScaleSetName; string instanceId = this.InstanceId; if (this.ParameterSetName.Equals("FriendMethod")) { var result = VirtualMachineScaleSetVMsClient.ReimageAll(resourceGroupName, vmScaleSetName, instanceId); var psObject = new PSOperationStatusResponse(); ComputeAutomationAutoMapperProfile.Mapper.Map <Azure.Management.Compute.Models.OperationStatusResponse, PSOperationStatusResponse>(result, psObject); WriteObject(psObject); } else { var result = VirtualMachineScaleSetVMsClient.Reimage(resourceGroupName, vmScaleSetName, instanceId); var psObject = new PSOperationStatusResponse(); ComputeAutomationAutoMapperProfile.Mapper.Map <Azure.Management.Compute.Models.OperationStatusResponse, PSOperationStatusResponse>(result, psObject); WriteObject(psObject); } } }); }
protected void ExecuteVirtualMachineScaleSetVMReimageMethod(object[] invokeMethodInputParameters) { string resourceGroupName = (string)ParseParameter(invokeMethodInputParameters[0]); string vmScaleSetName = (string)ParseParameter(invokeMethodInputParameters[1]); string instanceId = (string)ParseParameter(invokeMethodInputParameters[2]); VirtualMachineScaleSetVMsClient.Reimage(resourceGroupName, vmScaleSetName, instanceId); }
protected void ExecuteVirtualMachineScaleSetVMListNextMethod(object[] invokeMethodInputParameters) { string nextPageLink = (string)ParseParameter(invokeMethodInputParameters[0]); var result = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink); WriteObject(result); }
protected void ExecuteVirtualMachineScaleSetVMDeleteMethod(object[] invokeMethodInputParameters) { string resourceGroupName = (string)ParseParameter(invokeMethodInputParameters[0]); string vmScaleSetName = (string)ParseParameter(invokeMethodInputParameters[1]); string instanceId = (string)ParseParameter(invokeMethodInputParameters[2]); var result = VirtualMachineScaleSetVMsClient.Delete(resourceGroupName, vmScaleSetName, instanceId); 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() { base.ExecuteCmdlet(); ExecuteClientAction(() => { if (ShouldProcess(this.VMScaleSetName, VerbsCommon.Set)) { string resourceGroupName = this.ResourceGroupName; string vmScaleSetName = this.VMScaleSetName; string instanceId = this.InstanceId; Rest.Azure.AzureOperationResponse result = null; if (this.ParameterSetName.Equals("FriendMethod")) { result = VirtualMachineScaleSetVMsClient.ReimageAllWithHttpMessagesAsync(resourceGroupName, vmScaleSetName, instanceId).GetAwaiter().GetResult(); } else if (this.ParameterSetName.Equals("RedeployMethodParameter")) { result = VirtualMachineScaleSetVMsClient.RedeployWithHttpMessagesAsync(resourceGroupName, vmScaleSetName, instanceId).GetAwaiter().GetResult(); } else if (this.ParameterSetName.Equals("PerformMaintenanceMethodParameter")) { result = VirtualMachineScaleSetVMsClient.PerformMaintenanceWithHttpMessagesAsync(resourceGroupName, vmScaleSetName, instanceId).GetAwaiter().GetResult(); } else if (this.ParameterSetName.Equals("SimulateEvictionMethodParameter")) { result = VirtualMachineScaleSetVMsClient.SimulateEvictionWithHttpMessagesAsync(resourceGroupName, vmScaleSetName, instanceId).GetAwaiter().GetResult(); } else { result = VirtualMachineScaleSetVMsClient.ReimageWithHttpMessagesAsync(resourceGroupName, vmScaleSetName, instanceId).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); } }); }
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); WriteObject(result); } }
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); }
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); } }); }
protected override void ProcessRecord() { ExecuteClientAction(() => { if (ShouldProcess(this.ResourceGroupName, VerbsCommon.Set)) { string resourceGroupName = this.ResourceGroupName; string vmScaleSetName = this.VMScaleSetName; string instanceId = this.InstanceId; if (this.ParameterSetName.Equals("FriendMethod")) { var result = VirtualMachineScaleSetVMsClient.ReimageAll(resourceGroupName, vmScaleSetName, instanceId); WriteObject(result); } else { var result = VirtualMachineScaleSetVMsClient.Reimage(resourceGroupName, vmScaleSetName, instanceId); WriteObject(result); } } }); }
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); var psObject = new PSVirtualMachineScaleSetVM(); Mapper.Map <VirtualMachineScaleSetVM, PSVirtualMachineScaleSetVM>(result, psObject); WriteObject(psObject); } else if (!string.IsNullOrEmpty(resourceGroupName) && !string.IsNullOrEmpty(vmScaleSetName)) { var result = VirtualMachineScaleSetVMsClient.List(resourceGroupName, vmScaleSetName); var psObject = new List <PSVirtualMachineScaleSetVMList>(); foreach (var r in result) { psObject.Add(Mapper.Map <VirtualMachineScaleSetVM, PSVirtualMachineScaleSetVMList>(r)); } WriteObject(psObject); } }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); ExecuteClientAction(() => { if (ShouldProcess(this.VMScaleSetName, VerbsData.Update)) { string resourceGroupName; string vmScaleSetName; string instanceId; switch (this.ParameterSetName) { case "ResourceIdParameter": resourceGroupName = GetResourceGroupName(this.ResourceId); vmScaleSetName = GetResourceName(this.ResourceId, "Microsoft.Compute/VirtualMachineScaleSets", "virtualMachines"); instanceId = GetInstanceId(this.ResourceId, "Microsoft.Compute/VirtualMachineScaleSets", "virtualMachines"); break; case "ObjectParameter": resourceGroupName = GetResourceGroupName(this.VirtualMachineScaleSetVM.Id); vmScaleSetName = GetResourceName(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/VirtualMachineScaleSets", "virtualMachines"); instanceId = GetInstanceId(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/VirtualMachineScaleSets", "virtualMachines"); break; default: resourceGroupName = this.ResourceGroupName; vmScaleSetName = this.VMScaleSetName; instanceId = this.InstanceId; break; } VirtualMachineScaleSetVM parameters = new VirtualMachineScaleSetVM(); ComputeAutomationAutoMapperProfile.Mapper.Map <PSVirtualMachineScaleSetVM, VirtualMachineScaleSetVM>(this.VirtualMachineScaleSetVM, parameters); if (this.ParameterSetName != "ObjectParameter") { parameters = VirtualMachineScaleSetVMsClient.Get(resourceGroupName, vmScaleSetName, instanceId); } if (this.DataDisk != null) { if (parameters.StorageProfile == null) { parameters.StorageProfile = new StorageProfile(); } if (parameters.StorageProfile.DataDisks == null) { parameters.StorageProfile.DataDisks = new List <DataDisk>(); } foreach (var d in this.DataDisk) { parameters.StorageProfile.DataDisks.Add(d); } } var result = VirtualMachineScaleSetVMsClient.Update(resourceGroupName, vmScaleSetName, instanceId, parameters); var psObject = new PSVirtualMachineScaleSetVM(); ComputeAutomationAutoMapperProfile.Mapper.Map <VirtualMachineScaleSetVM, PSVirtualMachineScaleSetVM>(result, psObject); WriteObject(psObject); } }); }
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); }
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); } }); }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); ExecuteClientAction(() => { if (ShouldProcess(this.VMScaleSetName, VerbsData.Update)) { string resourceGroupName; string vmScaleSetName; string instanceId; switch (this.ParameterSetName) { case ResourceIdParameterSet: resourceGroupName = GetResourceGroupName(this.ResourceId); vmScaleSetName = GetResourceName(this.ResourceId, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines"); instanceId = GetInstanceId(this.ResourceId, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines"); break; case ObjectParameterSet: resourceGroupName = GetResourceGroupName(this.VirtualMachineScaleSetVM.Id); vmScaleSetName = GetResourceName(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines"); instanceId = GetInstanceId(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines"); break; default: resourceGroupName = this.ResourceGroupName; vmScaleSetName = this.VMScaleSetName; instanceId = this.InstanceId; break; } VirtualMachineScaleSetVM parameters = new VirtualMachineScaleSetVM(); ComputeAutomationAutoMapperProfile.Mapper.Map <PSVirtualMachineScaleSetVM, VirtualMachineScaleSetVM>(this.VirtualMachineScaleSetVM, parameters); if (this.IsParameterBound(c => c.UserData)) { parameters = VirtualMachineScaleSetVMsClient.Get(resourceGroupName, vmScaleSetName, instanceId, UserDataExpand); } else if (this.ParameterSetName != ObjectParameterSet) { parameters = VirtualMachineScaleSetVMsClient.Get(resourceGroupName, vmScaleSetName, instanceId); } if (this.DataDisk != null) { if (parameters.StorageProfile == null) { parameters.StorageProfile = new StorageProfile(); } if (parameters.StorageProfile.DataDisks == null) { parameters.StorageProfile.DataDisks = new List <DataDisk>(); } foreach (var d in this.DataDisk) { parameters.StorageProfile.DataDisks.Add(d); } } if (this.IsParameterBound(c => c.ProtectFromScaleIn)) { if (parameters.ProtectionPolicy == null) { parameters.ProtectionPolicy = new VirtualMachineScaleSetVMProtectionPolicy(); } parameters.ProtectionPolicy.ProtectFromScaleIn = this.ProtectFromScaleIn; } if (this.IsParameterBound(c => c.ProtectFromScaleSetAction)) { if (parameters.ProtectionPolicy == null) { parameters.ProtectionPolicy = new VirtualMachineScaleSetVMProtectionPolicy(); } parameters.ProtectionPolicy.ProtectFromScaleSetActions = this.ProtectFromScaleSetAction; } if (this.IsParameterBound(c => c.UserData)) { if (!ValidateBase64EncodedString.ValidateStringIsBase64Encoded(this.UserData)) { this.UserData = ValidateBase64EncodedString.EncodeStringToBase64(this.UserData); this.WriteInformation(ValidateBase64EncodedString.UserDataEncodeNotification, new string[] { "PSHOST" }); } parameters.UserData = this.UserData; } var result = VirtualMachineScaleSetVMsClient.Update(resourceGroupName, vmScaleSetName, instanceId, parameters); var psObject = new PSVirtualMachineScaleSetVM(); ComputeAutomationAutoMapperProfile.Mapper.Map <VirtualMachineScaleSetVM, PSVirtualMachineScaleSetVM>(result, psObject); WriteObject(psObject); } }); }