|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.Commands.Compute.Common; |
| 16 | +using Microsoft.Azure.Commands.Compute.Models; |
| 17 | +using Microsoft.Azure.Management.Compute; |
| 18 | +using Newtonsoft.Json; |
| 19 | +using System; |
| 20 | +using System.Management.Automation; |
| 21 | + |
| 22 | +namespace Microsoft.Azure.Commands.Compute |
| 23 | +{ |
| 24 | + [Cmdlet( |
| 25 | + VerbsCommon.Get, |
| 26 | + ProfileNouns.VirtualMachineCustomScriptExtension, |
| 27 | + DefaultParameterSetName = GetCustomScriptExtensionParamSetName), |
| 28 | + OutputType( |
| 29 | + typeof(VirtualMachineCustomScriptExtensionContext))] |
| 30 | + public class GetAzureVMCustomScriptExtensionCommand : VirtualMachineExtensionBaseCmdlet |
| 31 | + { |
| 32 | + protected const string GetCustomScriptExtensionParamSetName = "GetCustomScriptExtension"; |
| 33 | + |
| 34 | + [Parameter( |
| 35 | + Mandatory = true, |
| 36 | + Position = 0, |
| 37 | + ValueFromPipelineByPropertyName = true, |
| 38 | + HelpMessage = "The resource group name.")] |
| 39 | + [ValidateNotNullOrEmpty] |
| 40 | + public string ResourceGroupName { get; set; } |
| 41 | + |
| 42 | + [Alias("ResourceName")] |
| 43 | + [Parameter( |
| 44 | + Mandatory = true, |
| 45 | + Position = 1, |
| 46 | + ValueFromPipelineByPropertyName = true, |
| 47 | + HelpMessage = "The virtual machine name.")] |
| 48 | + [ValidateNotNullOrEmpty] |
| 49 | + public string VMName { get; set; } |
| 50 | + |
| 51 | + [Alias("ExtensionName")] |
| 52 | + [Parameter( |
| 53 | + Mandatory = true, |
| 54 | + Position = 2, |
| 55 | + ValueFromPipelineByPropertyName = true, |
| 56 | + HelpMessage = "The extension name.")] |
| 57 | + [ValidateNotNullOrEmpty] |
| 58 | + public string Name { get; set; } |
| 59 | + |
| 60 | + [Parameter( |
| 61 | + Position = 3, |
| 62 | + ValueFromPipelineByPropertyName = true, |
| 63 | + HelpMessage = "To show the status.")] |
| 64 | + [ValidateNotNullOrEmpty] |
| 65 | + public SwitchParameter Status { get; set; } |
| 66 | + |
| 67 | + protected override void ProcessRecord() |
| 68 | + { |
| 69 | + base.ProcessRecord(); |
| 70 | + |
| 71 | + ExecuteClientAction(() => |
| 72 | + { |
| 73 | + if (Status) |
| 74 | + { |
| 75 | + var result = this.VirtualMachineExtensionClient.Get(this.ResourceGroupName, this.VMName, this.Name, "instanceView"); |
| 76 | + var returnedExtension = result.ToPSVirtualMachineExtension(this.ResourceGroupName); |
| 77 | + |
| 78 | + if (returnedExtension.Publisher.Equals(VirtualMachineCustomScriptExtensionContext.ExtensionDefaultPublisher, StringComparison.OrdinalIgnoreCase) && |
| 79 | + returnedExtension.ExtensionType.Equals(VirtualMachineCustomScriptExtensionContext.ExtensionDefaultName, StringComparison.OrdinalIgnoreCase)) |
| 80 | + { |
| 81 | + WriteObject(new VirtualMachineCustomScriptExtensionContext(returnedExtension)); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + WriteObject(null); |
| 86 | + } |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + var result = this.VirtualMachineExtensionClient.Get(this.ResourceGroupName, this.VMName, this.Name); |
| 91 | + var returnedExtension = result.ToPSVirtualMachineExtension(this.ResourceGroupName); |
| 92 | + |
| 93 | + if (returnedExtension.Publisher.Equals(VirtualMachineCustomScriptExtensionContext.ExtensionDefaultPublisher, StringComparison.OrdinalIgnoreCase) && |
| 94 | + returnedExtension.ExtensionType.Equals(VirtualMachineCustomScriptExtensionContext.ExtensionDefaultName, StringComparison.OrdinalIgnoreCase)) |
| 95 | + { |
| 96 | + WriteObject(new VirtualMachineCustomScriptExtensionContext(returnedExtension)); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + WriteObject(null); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments