52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
namespace PS.IPAM.Cmdlets;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Management.Automation;
|
|
using PS.IPAM;
|
|
using PS.IPAM.Helpers;
|
|
|
|
[Cmdlet(VerbsCommon.Remove, "Address", DefaultParameterSetName = "ByID")]
|
|
public class RemoveAddressCmdlet : PSCmdlet
|
|
{
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
Position = 0,
|
|
ParameterSetName = "ByID")]
|
|
[ValidateNotNullOrEmpty]
|
|
public int? Id { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 0,
|
|
ParameterSetName = "ByAddressObject")]
|
|
[ValidateNotNullOrEmpty]
|
|
public Address? AddressObject { get; set; }
|
|
|
|
protected override void ProcessRecord()
|
|
{
|
|
try
|
|
{
|
|
int addressId;
|
|
if (ParameterSetName == "ByID")
|
|
{
|
|
addressId = Id!.Value;
|
|
}
|
|
else
|
|
{
|
|
addressId = AddressObject!.Id;
|
|
}
|
|
|
|
var identifiers = new List<string> { addressId.ToString() };
|
|
RequestHelper.InvokeRequest("DELETE", controllers.addresses, null, null, null, identifiers.ToArray())
|
|
.GetAwaiter().GetResult();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteError(new ErrorRecord(ex, "RemoveAddressError", ErrorCategory.InvalidOperation, null));
|
|
}
|
|
}
|
|
}
|