namespace PS.IPAM.Cmdlets; using System; using System.Collections.Generic; using System.Management.Automation; using PS.IPAM; using PS.IPAM.Helpers; [Cmdlet(VerbsCommon.Get, "Vlan", DefaultParameterSetName = "NoParams")] [OutputType(typeof(Vlan))] public class GetVlanCmdlet : PSCmdlet { [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ByID")] [ValidateNotNullOrEmpty] public int? Id { get; set; } [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ByNumber")] [ValidateNotNullOrEmpty] public int? Number { get; set; } [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ByL2Domain")] [ValidateNotNullOrEmpty] public int? L2DomainId { get; set; } [Parameter( Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "BySubnetObject")] [ValidateNotNullOrEmpty] public Subnetwork? SubnetObject { get; set; } [Parameter( Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ByDomainObject")] [ValidateNotNullOrEmpty] public Domain? DomainObject { get; set; } protected override void ProcessRecord() { try { var controller = controllers.vlan; var identifiers = new List(); switch (ParameterSetName) { case "ByID": identifiers.Add(Id!.Value.ToString()); break; case "ByNumber": identifiers.Add("search"); identifiers.Add(Number!.Value.ToString()); break; case "ByL2Domain": controller = controllers.l2domains; identifiers.Add(L2DomainId!.Value.ToString()); identifiers.Add(subcontrollers.vlans.ToString()); break; case "BySubnetObject": if (SubnetObject != null && SubnetObject.VlanId > 0) { identifiers.Add(SubnetObject.VlanId.ToString()); } else { return; } break; case "ByDomainObject": if (DomainObject != null) { var result = RequestHelper.InvokeRequest("GET", controllers.l2domains, types.Vlan, subcontrollers.vlans, null, new[] { DomainObject.Id.ToString() }) .GetAwaiter().GetResult(); if (result != null) { if (result is System.Collections.IEnumerable enumerable && !(result is string)) { foreach (var item in enumerable) { WriteObject(item); } } else { WriteObject(result); } } } return; } var vlanResult = RequestHelper.InvokeRequest("GET", controller, types.Vlan, null, null, identifiers.Count > 0 ? identifiers.ToArray() : null) .GetAwaiter().GetResult(); if (vlanResult != null) { if (vlanResult is System.Collections.IEnumerable enumerable && !(vlanResult is string)) { foreach (var item in enumerable) { WriteObject(item); } } else { WriteObject(vlanResult); } } } catch (Exception ex) { WriteError(new ErrorRecord(ex, "GetVlanError", ErrorCategory.InvalidOperation, null)); } } }