Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-AzVMDSCExtension and Get-AzVMDSCExtensionStatus new parameter set VMParameterSet for new parameter VM #13657

Merged
merged 11 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Compute/Compute.Test/ScenarioTests/DscExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ public void TestGetAzureRmVMDscExtension()
{
TestRunner.RunTestScript("Test-GetAzureRmVMDscExtension");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestDSCExtensionVMPiping()
{
TestRunner.RunTestScript("Test-DSCExtensionVMPiping");
}
}
}
106 changes: 106 additions & 0 deletions src/Compute/Compute.Test/ScenarioTests/DscExtensionTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,112 @@ function Test-GetAzureRmVMDscExtension
Remove-AzVMDscExtension -ResourceGroupName $rgname -VMName $vmname
}

<#
.SYNOPSIS
Test Get-AzVM piping to Get-AzVMDSCExtensionStatus and Get-AzVMDscExtension cmdlets.
#>
function Test-DSCExtensionVMPiping
{
# Setup
$loc = Get-ComputeVMLocation;
$rgname = Get-ComputeTestResourceName;

try
{

# VM Profile & Hardware
$vmsize = 'Standard_E2s_v3';
$vmname = 'v' + $rgname;
$domainNameLabel1 = "domain1" + $rgname;

# Common
New-AzResourceGroup -Name $rgname -Location $loc -Force;

$username = "admin01";
$password = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force;
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password;

$p = New-AzVMConfig -VMName $vmname -VMSize $vmsize;

# NRP
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
$vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
$subnetId = $vnet.Subnets[0].Id;
$pubip = New-AzPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic -DomainNameLabel ('pubip' + $rgname);
$pubip = Get-AzPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname;
$pubipId = $pubip.Id;
$nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
$nic = Get-AzNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname;
$nicId = $nic.Id;

$p = Add-AzVMNetworkInterface -VM $p -Id $nicId;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1;
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Id $nicId;

# Storage Account
$stoname = 'sto' + $rgname;
$stotype = 'Standard_GRS';
New-AzStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;
Retry-IfException { $global:stoaccount = Get-AzStorageAccount -ResourceGroupName $rgname -Name $stoname; }

$osDiskName = 'osDisk';
$osDiskCaching = 'ReadWrite';
$osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/os.vhd";
$dataDiskVhdUri1 = "https://$stoname.blob.core.windows.net/test/data1.vhd";

$p = Set-AzVMOSDisk -VM $p -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption FromImage;
$p = Add-AzVMDataDisk -VM $p -Name 'testDataDisk1' -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 1 -VhdUri $dataDiskVhdUri1 -CreateOption Empty;

# OS & Image
$user = "localadmin";
$password = $PLACEHOLDER;
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
$computerName = 'test';
$vhdContainer = "https://$stoname.blob.core.windows.net/test";

$p = Set-AzVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent;
$p = Set-AzVMSourceImage -VM $p -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"

# Virtual Machine
New-AzVM -ResourceGroupName $rgname -Location $loc -VM $p;

# Test DSC Extension
$version = '2.19';

#Install DSC Extension handler
Set-AzVMDscExtension -ResourceGroupName $rgname -VMName $vmname -ArchiveBlobName $null -ArchiveStorageAccountName $stoname -Version $version -Force -Location $loc

$dscGet = Get-AzVM -ResourceGroupName $rgname -Name $vmname | Get-AzVmDscExtensionStatus;
Assert-AreEqual $dscGet.Version $version;

# Test expected error message when missing ResourceGroup.
$vmname2 = "errorvm";
$vmConfig = New-AzVMConfig -Name $vmname2 -VMSize $vmsize;
Assert-ThrowsContains {
$vmError = $vmconfig | Get-AzVMDSCExtensionStatus; } `
"The incoming virtual machine must have a 'resourceGroupName'.";

# Test Get-AzVMDscExtension piping scenario
$dscGetExt = Get-AzVM -ResourceGroupName $rgname -Name $vmname | Get-AzVmDscExtension;
Assert-AreEqual $dscGetExt.TypeHandlerVersion $version;

# Test expected error message when missing ResourceGroup.
$vmname3 = "errorvm3";
$vmConfigError = New-AzVMConfig -Name $vmname3 -VMSize $vmsize;
Assert-ThrowsContains {
$vmError = $vmconfigError | Get-AzVMDSCExtension; } `
"The incoming virtual machine must have a 'resourceGroupName'.";
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname;
}
}


#helper methods for ARM
function Get-DefaultResourceGroupLocation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4523,4 +4523,4 @@ function Test-VirtualMachineGetVMNameAcrossResourceGroups
Clean-ResourceGroup $rgname;
Clean-ResourceGroup $rgname2;
}
}
}

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Compute/Compute/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

-->
## Upcoming Release
* New parameter `VM` in new parameter set `VMParameterSet` added to `Get-AzVMDscExtensionStatus` and `Get-AzVMDscExtension` cmdlets.

## Version 4.7.0
* Edited Get-AzVm to filter by `-Name` prior to checking for throttling due to too many resources.
Expand Down
43 changes: 38 additions & 5 deletions src/Compute/Compute/Extension/DSC/GetAzureVMDscExtensionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ namespace Microsoft.Azure.Commands.Compute.Extension.DSC
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMDscExtension",DefaultParameterSetName = GetDscExtensionParamSetName),OutputType(typeof(VirtualMachineDscExtensionContext))]
public class GetAzureVMDscExtensionCommand : VirtualMachineExtensionBaseCmdlet
{
private const string GetDscExtensionParamSetName = "GetDscExtension";
private const string GetDscExtensionParamSetName = "GetDscExtension",
VMParameterSetName = "VMParameterSet";

[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
ParameterSetName = GetDscExtensionParamSetName,
HelpMessage = "The resource group name.")]
[ResourceGroupCompleter]
[ValidateNotNullOrEmpty]
Expand All @@ -30,6 +32,7 @@ public class GetAzureVMDscExtensionCommand : VirtualMachineExtensionBaseCmdlet
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
ParameterSetName = GetDscExtensionParamSetName,
HelpMessage = "The virtual machine name.")]
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
[ValidateNotNullOrEmpty]
Expand All @@ -38,6 +41,7 @@ public class GetAzureVMDscExtensionCommand : VirtualMachineExtensionBaseCmdlet
[Parameter(
Position = 2,
ValueFromPipelineByPropertyName = true,
ParameterSetName = GetDscExtensionParamSetName,
HelpMessage = "Name of the ARM resource that represents the extension. The Set-AzVMDscExtension cmdlet sets this name to " +
"'Microsoft.Powershell.DSC', which is the same value used by Get-AzVMDscExtension. Specify this parameter only if you changed " +
"the default name in the Set cmdlet or used a different resource name in an ARM template.")]
Expand All @@ -51,19 +55,43 @@ public class GetAzureVMDscExtensionCommand : VirtualMachineExtensionBaseCmdlet
[ValidateNotNullOrEmpty]
public SwitchParameter Status { get; set; }

[Parameter(
ParameterSetName = VMParameterSetName,
ValueFromPipeline = true,
HelpMessage = "Specifies the virtual machine object the extension is on.")]
[ValidateNotNullOrEmpty]
public PSVirtualMachine VM { get; set; }

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

string virtualMachineName = "";
string resourceGroup = "";
if (this.ParameterSetName.Equals(VMParameterSetName))
{
virtualMachineName = this.VM.Name;
if (this.VM.ResourceGroupName == null)
{
WriteError("The incoming virtual machine must have a 'resourceGroupName'.", this.VM);
}
resourceGroup = this.VM.ResourceGroupName;
}
else
{
virtualMachineName = VMName;
resourceGroup = ResourceGroupName;
}

if (String.IsNullOrEmpty(Name))
{
Name = DscExtensionCmdletConstants.ExtensionPublishedNamespace + "." + DscExtensionCmdletConstants.ExtensionPublishedName;
}

if (Status)
{
var result = VirtualMachineExtensionClient.GetWithInstanceView(ResourceGroupName, VMName, Name);
var extension = result.ToPSVirtualMachineExtension(this.ResourceGroupName, this.VMName);
var result = VirtualMachineExtensionClient.GetWithInstanceView(resourceGroup, virtualMachineName, Name);
var extension = result.ToPSVirtualMachineExtension(resourceGroup, virtualMachineName);

if (
extension.Publisher.Equals(DscExtensionCmdletConstants.ExtensionPublishedNamespace,
Expand All @@ -80,8 +108,8 @@ public override void ExecuteCmdlet()
}
else
{
var result = VirtualMachineExtensionClient.Get(ResourceGroupName, VMName, Name);
var extension = result.ToPSVirtualMachineExtension(this.ResourceGroupName, this.VMName);
var result = VirtualMachineExtensionClient.Get(resourceGroup, virtualMachineName, Name);
var extension = result.ToPSVirtualMachineExtension(resourceGroup, virtualMachineName);

if (
extension.Publisher.Equals(
Expand Down Expand Up @@ -157,5 +185,10 @@ private VirtualMachineDscExtensionContext GetDscExtensionContext(PSVirtualMachin

return context;
}

private void WriteError(string message, params object[] args)
{
base.WriteError(new ErrorRecord(new Exception(String.Format(message, args)), "Error", ErrorCategory.NotSpecified, null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ namespace Microsoft.Azure.Commands.Compute.Extension.DSC
/// Get-AzVMDscExtensionStatus -ResourceGroupName resgrp1 -VMName vm1
/// /// Get-AzVMDscExtensionStatus -ResourceGroupName resgrp1 -VMName vm1 -Name DSC
/// </summary>
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMDscExtensionStatus"),OutputType(typeof(PSVirtualMachineInstanceView))]
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMDscExtensionStatus", DefaultParameterSetName = GetDscExtensionParamSetName), OutputType(typeof(PSVirtualMachineInstanceView))]
public class GetAzureVMDscExtensionStatusCommand : VirtualMachineExtensionBaseCmdlet
{
private const string GetDscExtensionParamSetName = "GetDscExtension",
VMParameterSetName = "VMParameterSet";

[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
ParameterSetName = GetDscExtensionParamSetName,
HelpMessage = "The resource group name.")]
[ResourceGroupCompleter]
[ValidateNotNullOrEmpty]
Expand All @@ -36,6 +40,7 @@ public class GetAzureVMDscExtensionStatusCommand : VirtualMachineExtensionBaseCm
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
ParameterSetName = GetDscExtensionParamSetName,
HelpMessage = "The virtual machine name.")]
[ResourceNameCompleter("Microsoft.Compute/virtualMachines", "ResourceGroupName")]
[ValidateNotNullOrEmpty]
Expand All @@ -44,26 +49,51 @@ public class GetAzureVMDscExtensionStatusCommand : VirtualMachineExtensionBaseCm
[Parameter(
Position = 2,
ValueFromPipelineByPropertyName = true,
ParameterSetName = GetDscExtensionParamSetName,
HelpMessage = "Name of the ARM resource that represents the extension. The Set-AzVMDscExtension cmdlet sets this name to " +
"'Microsoft.Powershell.DSC', which is the same value used by Get-AzVMDscExtension. Specify this parameter only if you changed " +
"the default name in the Set cmdlet or used a different resource name in an ARM template.")]
[ResourceNameCompleter("Microsoft.Compute/virtualMachines/extensions", "ResourceGroupName", "VMName")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Parameter(
ValueFromPipeline = true,
ParameterSetName = VMParameterSetName,
HelpMessage = "Specifies the virtual machine object the extension is on.")]
[ValidateNotNullOrEmpty]
public PSVirtualMachine VM { get; set; }

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

string virtualMachineName = "";
string resourceGroup = "";
if (this.ParameterSetName.Equals(VMParameterSetName))
{
virtualMachineName = this.VM.Name;
if (this.VM.ResourceGroupName == null)
{
WriteError("The incoming virtual machine must have a 'resourceGroupName'.", this.VM);
}
resourceGroup = this.VM.ResourceGroupName;
}
else
{
virtualMachineName = VMName;
resourceGroup = ResourceGroupName;
}

if (String.IsNullOrEmpty(Name))
{
Name = DscExtensionCmdletConstants.ExtensionPublishedNamespace + "." + DscExtensionCmdletConstants.ExtensionPublishedName;
}

var result = VirtualMachineExtensionClient.GetWithInstanceView(ResourceGroupName, VMName, Name);
var result = VirtualMachineExtensionClient.GetWithInstanceView(resourceGroup, virtualMachineName, Name);
if (result != null && result.Body != null)
{
WriteObject(GetDscExtensionStatusContext(result.Body, ResourceGroupName, VMName));
WriteObject(GetDscExtensionStatusContext(result.Body, resourceGroup, virtualMachineName));
}
else
{
Expand Down Expand Up @@ -104,5 +134,10 @@ private VirtualMachineDscExtensionStatusContext GetDscExtensionStatusContext(

return context;
}

private void WriteError(string message, params object[] args)
{
base.WriteError(new ErrorRecord(new Exception(String.Format(message, args)), "Error", ErrorCategory.NotSpecified, null));
}
}
}
28 changes: 25 additions & 3 deletions src/Compute/Compute/help/Get-AzVMDscExtension.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ Gets the settings of the DSC extension on a particular virtual machine.

## SYNTAX

### GetDscExtension (Default)
```
Get-AzVMDscExtension [-ResourceGroupName] <String> [-VMName] <String> [[-Name] <String>] [-Status]
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### VMParameterSet
```
Get-AzVMDscExtension [-Status] [-VM <PSVirtualMachine>] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
```

## DESCRIPTION
The **Get-AzVMDscExtension** cmdlet gets the settings of the Desired State Configuration (DSC) extension on a particular virtual machine.

Expand Down Expand Up @@ -54,7 +61,7 @@ Specify this parameter only if you changed the default name in the **Set-AzVMDsc

```yaml
Type: System.String
Parameter Sets: (All)
Parameter Sets: GetDscExtension
Aliases:

Required: False
Expand All @@ -69,7 +76,7 @@ Specifies the name of the resource group of the virtual machine.

```yaml
Type: System.String
Parameter Sets: (All)
Parameter Sets: GetDscExtension
Aliases:

Required: True
Expand All @@ -94,12 +101,27 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -VM
Specifies the virtual machine object the extension is on.

```yaml
Type: Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine
Parameter Sets: VMParameterSet
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```

### -VMName
Specifies the name of a virtual machine for which this cmdlet gets the DSC extension.

```yaml
Type: System.String
Parameter Sets: (All)
Parameter Sets: GetDscExtension
Aliases:

Required: True
Expand Down
Loading