Skip to content

Commit 4101017

Browse files
authored
Get-AzVMDSCExtension and Get-AzVMDSCExtensionStatus new parameter set VMParameterSet for new parameter VM (#13657)
* practice * practice2 * success * testing attempts * testing * changelog * help doc for Status cmdlet * help text on status * 2nd cmdlet * 2nd cmdlet help doc
1 parent 4519e29 commit 4101017

File tree

9 files changed

+5276
-16
lines changed

9 files changed

+5276
-16
lines changed

src/Compute/Compute.Test/ScenarioTests/DscExtensionTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,12 @@ public void TestGetAzureRmVMDscExtension()
3030
{
3131
TestRunner.RunTestScript("Test-GetAzureRmVMDscExtension");
3232
}
33+
34+
[Fact]
35+
[Trait(Category.AcceptanceType, Category.CheckIn)]
36+
public void TestDSCExtensionVMPiping()
37+
{
38+
TestRunner.RunTestScript("Test-DSCExtensionVMPiping");
39+
}
3340
}
3441
}

src/Compute/Compute.Test/ScenarioTests/DscExtensionTests.ps1

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,112 @@ function Test-GetAzureRmVMDscExtension
9999
Remove-AzVMDscExtension -ResourceGroupName $rgname -VMName $vmname
100100
}
101101

102+
<#
103+
.SYNOPSIS
104+
Test Get-AzVM piping to Get-AzVMDSCExtensionStatus and Get-AzVMDscExtension cmdlets.
105+
#>
106+
function Test-DSCExtensionVMPiping
107+
{
108+
# Setup
109+
$loc = Get-ComputeVMLocation;
110+
$rgname = Get-ComputeTestResourceName;
111+
112+
try
113+
{
114+
115+
# VM Profile & Hardware
116+
$vmsize = 'Standard_E2s_v3';
117+
$vmname = 'v' + $rgname;
118+
$domainNameLabel1 = "domain1" + $rgname;
119+
120+
# Common
121+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
122+
123+
$username = "admin01";
124+
$password = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force;
125+
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password;
126+
127+
$p = New-AzVMConfig -VMName $vmname -VMSize $vmsize;
128+
129+
# NRP
130+
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
131+
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
132+
$vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
133+
$subnetId = $vnet.Subnets[0].Id;
134+
$pubip = New-AzPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic -DomainNameLabel ('pubip' + $rgname);
135+
$pubip = Get-AzPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname;
136+
$pubipId = $pubip.Id;
137+
$nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
138+
$nic = Get-AzNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname;
139+
$nicId = $nic.Id;
140+
141+
$p = Add-AzVMNetworkInterface -VM $p -Id $nicId;
142+
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1;
143+
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Id $nicId;
144+
145+
# Storage Account
146+
$stoname = 'sto' + $rgname;
147+
$stotype = 'Standard_GRS';
148+
New-AzStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;
149+
Retry-IfException { $global:stoaccount = Get-AzStorageAccount -ResourceGroupName $rgname -Name $stoname; }
150+
151+
$osDiskName = 'osDisk';
152+
$osDiskCaching = 'ReadWrite';
153+
$osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/os.vhd";
154+
$dataDiskVhdUri1 = "https://$stoname.blob.core.windows.net/test/data1.vhd";
155+
156+
$p = Set-AzVMOSDisk -VM $p -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption FromImage;
157+
$p = Add-AzVMDataDisk -VM $p -Name 'testDataDisk1' -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 1 -VhdUri $dataDiskVhdUri1 -CreateOption Empty;
158+
159+
# OS & Image
160+
$user = "localadmin";
161+
$password = $PLACEHOLDER;
162+
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
163+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
164+
$computerName = 'test';
165+
$vhdContainer = "https://$stoname.blob.core.windows.net/test";
166+
167+
$p = Set-AzVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent;
168+
$p = Set-AzVMSourceImage -VM $p -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"
169+
170+
# Virtual Machine
171+
New-AzVM -ResourceGroupName $rgname -Location $loc -VM $p;
172+
173+
# Test DSC Extension
174+
$version = '2.19';
175+
176+
#Install DSC Extension handler
177+
Set-AzVMDscExtension -ResourceGroupName $rgname -VMName $vmname -ArchiveBlobName $null -ArchiveStorageAccountName $stoname -Version $version -Force -Location $loc
178+
179+
$dscGet = Get-AzVM -ResourceGroupName $rgname -Name $vmname | Get-AzVmDscExtensionStatus;
180+
Assert-AreEqual $dscGet.Version $version;
181+
182+
# Test expected error message when missing ResourceGroup.
183+
$vmname2 = "errorvm";
184+
$vmConfig = New-AzVMConfig -Name $vmname2 -VMSize $vmsize;
185+
Assert-ThrowsContains {
186+
$vmError = $vmconfig | Get-AzVMDSCExtensionStatus; } `
187+
"The incoming virtual machine must have a 'resourceGroupName'.";
188+
189+
# Test Get-AzVMDscExtension piping scenario
190+
$dscGetExt = Get-AzVM -ResourceGroupName $rgname -Name $vmname | Get-AzVmDscExtension;
191+
Assert-AreEqual $dscGetExt.TypeHandlerVersion $version;
192+
193+
# Test expected error message when missing ResourceGroup.
194+
$vmname3 = "errorvm3";
195+
$vmConfigError = New-AzVMConfig -Name $vmname3 -VMSize $vmsize;
196+
Assert-ThrowsContains {
197+
$vmError = $vmconfigError | Get-AzVMDSCExtension; } `
198+
"The incoming virtual machine must have a 'resourceGroupName'.";
199+
}
200+
finally
201+
{
202+
# Cleanup
203+
Clean-ResourceGroup $rgname;
204+
}
205+
}
206+
207+
102208
#helper methods for ARM
103209
function Get-DefaultResourceGroupLocation
104210
{

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4523,4 +4523,4 @@ function Test-VirtualMachineGetVMNameAcrossResourceGroups
45234523
Clean-ResourceGroup $rgname;
45244524
Clean-ResourceGroup $rgname2;
45254525
}
4526-
}
4526+
}

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.DscExtensionTests/TestDSCExtensionVMPiping.json

Lines changed: 5033 additions & 0 deletions
Large diffs are not rendered by default.

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
-->
2222
## Upcoming Release
23+
* New parameter `VM` in new parameter set `VMParameterSet` added to `Get-AzVMDscExtensionStatus` and `Get-AzVMDscExtension` cmdlets.
2324

2425
## Version 4.7.0
2526
* Edited Get-AzVm to filter by `-Name` prior to checking for throttling due to too many resources.

src/Compute/Compute/Extension/DSC/GetAzureVMDscExtensionCommand.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ namespace Microsoft.Azure.Commands.Compute.Extension.DSC
1515
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMDscExtension",DefaultParameterSetName = GetDscExtensionParamSetName),OutputType(typeof(VirtualMachineDscExtensionContext))]
1616
public class GetAzureVMDscExtensionCommand : VirtualMachineExtensionBaseCmdlet
1717
{
18-
private const string GetDscExtensionParamSetName = "GetDscExtension";
18+
private const string GetDscExtensionParamSetName = "GetDscExtension",
19+
VMParameterSetName = "VMParameterSet";
1920

2021
[Parameter(
2122
Mandatory = true,
2223
Position = 0,
2324
ValueFromPipelineByPropertyName = true,
25+
ParameterSetName = GetDscExtensionParamSetName,
2426
HelpMessage = "The resource group name.")]
2527
[ResourceGroupCompleter]
2628
[ValidateNotNullOrEmpty]
@@ -30,6 +32,7 @@ public class GetAzureVMDscExtensionCommand : VirtualMachineExtensionBaseCmdlet
3032
Mandatory = true,
3133
Position = 1,
3234
ValueFromPipelineByPropertyName = true,
35+
ParameterSetName = GetDscExtensionParamSetName,
3336
HelpMessage = "The virtual machine name.")]
3437
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
3538
[ValidateNotNullOrEmpty]
@@ -38,6 +41,7 @@ public class GetAzureVMDscExtensionCommand : VirtualMachineExtensionBaseCmdlet
3841
[Parameter(
3942
Position = 2,
4043
ValueFromPipelineByPropertyName = true,
44+
ParameterSetName = GetDscExtensionParamSetName,
4145
HelpMessage = "Name of the ARM resource that represents the extension. The Set-AzVMDscExtension cmdlet sets this name to " +
4246
"'Microsoft.Powershell.DSC', which is the same value used by Get-AzVMDscExtension. Specify this parameter only if you changed " +
4347
"the default name in the Set cmdlet or used a different resource name in an ARM template.")]
@@ -51,19 +55,43 @@ public class GetAzureVMDscExtensionCommand : VirtualMachineExtensionBaseCmdlet
5155
[ValidateNotNullOrEmpty]
5256
public SwitchParameter Status { get; set; }
5357

58+
[Parameter(
59+
ParameterSetName = VMParameterSetName,
60+
ValueFromPipeline = true,
61+
HelpMessage = "Specifies the virtual machine object the extension is on.")]
62+
[ValidateNotNullOrEmpty]
63+
public PSVirtualMachine VM { get; set; }
64+
5465
public override void ExecuteCmdlet()
5566
{
5667
base.ExecuteCmdlet();
5768

69+
string virtualMachineName = "";
70+
string resourceGroup = "";
71+
if (this.ParameterSetName.Equals(VMParameterSetName))
72+
{
73+
virtualMachineName = this.VM.Name;
74+
if (this.VM.ResourceGroupName == null)
75+
{
76+
WriteError("The incoming virtual machine must have a 'resourceGroupName'.", this.VM);
77+
}
78+
resourceGroup = this.VM.ResourceGroupName;
79+
}
80+
else
81+
{
82+
virtualMachineName = VMName;
83+
resourceGroup = ResourceGroupName;
84+
}
85+
5886
if (String.IsNullOrEmpty(Name))
5987
{
6088
Name = DscExtensionCmdletConstants.ExtensionPublishedNamespace + "." + DscExtensionCmdletConstants.ExtensionPublishedName;
6189
}
6290

6391
if (Status)
6492
{
65-
var result = VirtualMachineExtensionClient.GetWithInstanceView(ResourceGroupName, VMName, Name);
66-
var extension = result.ToPSVirtualMachineExtension(this.ResourceGroupName, this.VMName);
93+
var result = VirtualMachineExtensionClient.GetWithInstanceView(resourceGroup, virtualMachineName, Name);
94+
var extension = result.ToPSVirtualMachineExtension(resourceGroup, virtualMachineName);
6795

6896
if (
6997
extension.Publisher.Equals(DscExtensionCmdletConstants.ExtensionPublishedNamespace,
@@ -80,8 +108,8 @@ public override void ExecuteCmdlet()
80108
}
81109
else
82110
{
83-
var result = VirtualMachineExtensionClient.Get(ResourceGroupName, VMName, Name);
84-
var extension = result.ToPSVirtualMachineExtension(this.ResourceGroupName, this.VMName);
111+
var result = VirtualMachineExtensionClient.Get(resourceGroup, virtualMachineName, Name);
112+
var extension = result.ToPSVirtualMachineExtension(resourceGroup, virtualMachineName);
85113

86114
if (
87115
extension.Publisher.Equals(
@@ -157,5 +185,10 @@ private VirtualMachineDscExtensionContext GetDscExtensionContext(PSVirtualMachin
157185

158186
return context;
159187
}
188+
189+
private void WriteError(string message, params object[] args)
190+
{
191+
base.WriteError(new ErrorRecord(new Exception(String.Format(message, args)), "Error", ErrorCategory.NotSpecified, null));
192+
}
160193
}
161194
}

src/Compute/Compute/Extension/DSC/GetAzureVMDscExtensionStatusCommand.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ namespace Microsoft.Azure.Commands.Compute.Extension.DSC
2020
/// Get-AzVMDscExtensionStatus -ResourceGroupName resgrp1 -VMName vm1
2121
/// /// Get-AzVMDscExtensionStatus -ResourceGroupName resgrp1 -VMName vm1 -Name DSC
2222
/// </summary>
23-
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMDscExtensionStatus"),OutputType(typeof(PSVirtualMachineInstanceView))]
23+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMDscExtensionStatus", DefaultParameterSetName = GetDscExtensionParamSetName), OutputType(typeof(PSVirtualMachineInstanceView))]
2424
public class GetAzureVMDscExtensionStatusCommand : VirtualMachineExtensionBaseCmdlet
2525
{
26+
private const string GetDscExtensionParamSetName = "GetDscExtension",
27+
VMParameterSetName = "VMParameterSet";
28+
2629
[Parameter(
2730
Mandatory = true,
2831
Position = 0,
2932
ValueFromPipelineByPropertyName = true,
33+
ParameterSetName = GetDscExtensionParamSetName,
3034
HelpMessage = "The resource group name.")]
3135
[ResourceGroupCompleter]
3236
[ValidateNotNullOrEmpty]
@@ -36,6 +40,7 @@ public class GetAzureVMDscExtensionStatusCommand : VirtualMachineExtensionBaseCm
3640
Mandatory = true,
3741
Position = 1,
3842
ValueFromPipelineByPropertyName = true,
43+
ParameterSetName = GetDscExtensionParamSetName,
3944
HelpMessage = "The virtual machine name.")]
4045
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
4146
[ValidateNotNullOrEmpty]
@@ -44,26 +49,51 @@ public class GetAzureVMDscExtensionStatusCommand : VirtualMachineExtensionBaseCm
4449
[Parameter(
4550
Position = 2,
4651
ValueFromPipelineByPropertyName = true,
52+
ParameterSetName = GetDscExtensionParamSetName,
4753
HelpMessage = "Name of the ARM resource that represents the extension. The Set-AzVMDscExtension cmdlet sets this name to " +
4854
"'Microsoft.Powershell.DSC', which is the same value used by Get-AzVMDscExtension. Specify this parameter only if you changed " +
4955
"the default name in the Set cmdlet or used a different resource name in an ARM template.")]
5056
[ResourceNameCompleter("Microsoft.Compute/virtualMachines/extensions", "ResourceGroupName", "VMName")]
5157
[ValidateNotNullOrEmpty]
5258
public string Name { get; set; }
5359

60+
[Parameter(
61+
ValueFromPipeline = true,
62+
ParameterSetName = VMParameterSetName,
63+
HelpMessage = "Specifies the virtual machine object the extension is on.")]
64+
[ValidateNotNullOrEmpty]
65+
public PSVirtualMachine VM { get; set; }
66+
5467
public override void ExecuteCmdlet()
5568
{
5669
base.ExecuteCmdlet();
5770

71+
string virtualMachineName = "";
72+
string resourceGroup = "";
73+
if (this.ParameterSetName.Equals(VMParameterSetName))
74+
{
75+
virtualMachineName = this.VM.Name;
76+
if (this.VM.ResourceGroupName == null)
77+
{
78+
WriteError("The incoming virtual machine must have a 'resourceGroupName'.", this.VM);
79+
}
80+
resourceGroup = this.VM.ResourceGroupName;
81+
}
82+
else
83+
{
84+
virtualMachineName = VMName;
85+
resourceGroup = ResourceGroupName;
86+
}
87+
5888
if (String.IsNullOrEmpty(Name))
5989
{
6090
Name = DscExtensionCmdletConstants.ExtensionPublishedNamespace + "." + DscExtensionCmdletConstants.ExtensionPublishedName;
6191
}
6292

63-
var result = VirtualMachineExtensionClient.GetWithInstanceView(ResourceGroupName, VMName, Name);
93+
var result = VirtualMachineExtensionClient.GetWithInstanceView(resourceGroup, virtualMachineName, Name);
6494
if (result != null && result.Body != null)
6595
{
66-
WriteObject(GetDscExtensionStatusContext(result.Body, ResourceGroupName, VMName));
96+
WriteObject(GetDscExtensionStatusContext(result.Body, resourceGroup, virtualMachineName));
6797
}
6898
else
6999
{
@@ -104,5 +134,10 @@ private VirtualMachineDscExtensionStatusContext GetDscExtensionStatusContext(
104134

105135
return context;
106136
}
137+
138+
private void WriteError(string message, params object[] args)
139+
{
140+
base.WriteError(new ErrorRecord(new Exception(String.Format(message, args)), "Error", ErrorCategory.NotSpecified, null));
141+
}
107142
}
108143
}

src/Compute/Compute/help/Get-AzVMDscExtension.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ Gets the settings of the DSC extension on a particular virtual machine.
1313

1414
## SYNTAX
1515

16+
### GetDscExtension (Default)
1617
```
1718
Get-AzVMDscExtension [-ResourceGroupName] <String> [-VMName] <String> [[-Name] <String>] [-Status]
1819
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
1920
```
2021

22+
### VMParameterSet
23+
```
24+
Get-AzVMDscExtension [-Status] [-VM <PSVirtualMachine>] [-DefaultProfile <IAzureContextContainer>]
25+
[<CommonParameters>]
26+
```
27+
2128
## DESCRIPTION
2229
The **Get-AzVMDscExtension** cmdlet gets the settings of the Desired State Configuration (DSC) extension on a particular virtual machine.
2330

@@ -54,7 +61,7 @@ Specify this parameter only if you changed the default name in the **Set-AzVMDsc
5461
5562
```yaml
5663
Type: System.String
57-
Parameter Sets: (All)
64+
Parameter Sets: GetDscExtension
5865
Aliases:
5966

6067
Required: False
@@ -69,7 +76,7 @@ Specifies the name of the resource group of the virtual machine.
6976
7077
```yaml
7178
Type: System.String
72-
Parameter Sets: (All)
79+
Parameter Sets: GetDscExtension
7380
Aliases:
7481

7582
Required: True
@@ -94,12 +101,27 @@ Accept pipeline input: True (ByPropertyName)
94101
Accept wildcard characters: False
95102
```
96103
104+
### -VM
105+
Specifies the virtual machine object the extension is on.
106+
107+
```yaml
108+
Type: Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine
109+
Parameter Sets: VMParameterSet
110+
Aliases:
111+
112+
Required: False
113+
Position: Named
114+
Default value: None
115+
Accept pipeline input: True (ByValue)
116+
Accept wildcard characters: False
117+
```
118+
97119
### -VMName
98120
Specifies the name of a virtual machine for which this cmdlet gets the DSC extension.
99121
100122
```yaml
101123
Type: System.String
102-
Parameter Sets: (All)
124+
Parameter Sets: GetDscExtension
103125
Aliases:
104126

105127
Required: True

0 commit comments

Comments
 (0)