public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                VirtualMachine virtualMachineResponse = (this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName));

                if ((virtualMachineResponse == null) ||
                    (virtualMachineResponse.StorageProfile == null) ||
                    (virtualMachineResponse.StorageProfile.OsDisk == null))
                {
                    // VM should have been created and have valid storageProfile and OSDisk by now
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new ApplicationException(
                                string.Format(
                                    CultureInfo.CurrentUICulture,
                                    "Disable-AzureDiskEncryption can disable encryption only on a VM that was already created and has appropriate storageProfile and OS disk")),
                            "InvalidResult",
                            ErrorCategory.InvalidResult,
                            null));
                }

                currentOSType = virtualMachineResponse.StorageProfile.OsDisk.OsType;

                if (this.ShouldProcess(VMName, Properties.Resources.DisableDiskEncryptionAction) &&
                    (this.Force.IsPresent ||
                     this.ShouldContinue(Properties.Resources.DisableAzureDiskEncryptionConfirmation, Properties.Resources.DisableAzureDiskEncryptionCaption)))
                {
                    VirtualMachineExtension parameters = GetVmExtensionParameters(virtualMachineResponse);

                    var opExt = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
                        this.ResourceGroupName,
                        this.VMName,
                        this.Name,
                        parameters).GetAwaiter().GetResult();

                    if (string.IsNullOrWhiteSpace(VolumeType) ||
                        VolumeType.Equals(AzureDiskEncryptionExtensionContext.VolumeTypeAll, StringComparison.InvariantCultureIgnoreCase) ||
                        VolumeType.Equals(AzureDiskEncryptionExtensionContext.VolumeTypeOS, StringComparison.InvariantCultureIgnoreCase))
                    {
                        var opVm   = UpdateVmEncryptionSettings();
                        var result = Mapper.Map <PSAzureOperationResponse>(opVm);
                        WriteObject(result);
                    }
                    else
                    {
                        var result = Mapper.Map <PSAzureOperationResponse>(opExt);
                        WriteObject(result);
                    }
                }
            });
        }
Exemplo n.º 2
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            ExecuteClientAction(() =>
            {
                VirtualMachine virtualMachineResponse = (this.ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName));

                if ((virtualMachineResponse == null) ||
                    (virtualMachineResponse.StorageProfile == null) ||
                    (virtualMachineResponse.StorageProfile.OsDisk == null))
                {
                    // VM should have been created and have valid storageProfile and OSDisk by now
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new ApplicationException(
                                string.Format(
                                    CultureInfo.CurrentUICulture,
                                    "Disable-AzureDiskEncryption can disable encryption only on a VM that was already created and has appropriate storageProfile and OS disk")),
                            "InvalidResult",
                            ErrorCategory.InvalidResult,
                            null));
                }

                currentOSType = virtualMachineResponse.StorageProfile.OsDisk.OsType;

                if (OperatingSystemTypes.Linux.Equals(currentOSType) &&
                    !AzureDiskEncryptionExtensionContext.VolumeTypeData.Equals(VolumeType, StringComparison.InvariantCultureIgnoreCase))
                {
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new ArgumentException(
                                string.Format(
                                    CultureInfo.CurrentUICulture,
                                    "Disabling encryption is only allowed on Data volumes for Linux VMs.")),
                            "InvalidType",
                            ErrorCategory.NotImplemented,
                            null));
                }

                if (this.ShouldProcess(VMName, Properties.Resources.DisableDiskEncryptionAction) &&
                    (this.Force.IsPresent ||
                     this.ShouldContinue(Properties.Resources.DisableAzureDiskEncryptionConfirmation, Properties.Resources.DisableAzureDiskEncryptionCaption)))
                {
                    VirtualMachineExtension parameters = GetVmExtensionParameters(virtualMachineResponse);

                    var opExt = this.VirtualMachineExtensionClient.CreateOrUpdateWithHttpMessagesAsync(
                        this.ResourceGroupName,
                        this.VMName,
                        this.Name,
                        parameters).GetAwaiter().GetResult();

                    // +---------+---------------+----------------------------+
                    // | OSType  |  VolumeType   | UpdateVmEncryptionSettings |
                    // +---------+---------------+----------------------------+
                    // | Windows | OS            | Yes                        |
                    // | Windows | Data          | No                         |
                    // | Windows | Not Specified | Yes                        |
                    // | Linux   | OS            | N/A                        |
                    // | Linux   | Data          | Yes                        |
                    // | Linux   | Not Specified | N/A                        |
                    // +---------+---------------+----------------------------+

                    if (OperatingSystemTypes.Windows.Equals(currentOSType) &&
                        !string.IsNullOrEmpty(VolumeType) &&
                        VolumeType.Equals(AzureDiskEncryptionExtensionContext.VolumeTypeData, StringComparison.InvariantCultureIgnoreCase))
                    {
                        var result = Mapper.Map <PSAzureOperationResponse>(opExt);
                        WriteObject(result);
                    }
                    else
                    {
                        var opVm   = UpdateVmEncryptionSettings();
                        var result = Mapper.Map <PSAzureOperationResponse>(opVm);
                        WriteObject(result);
                    }
                }
            });
        }