示例#1
0
        /// <inheritdoc/>
        protected override void ProcessRecord()
        {
            if (!(CidrNotation is null))
            {
                if (IPAddress.TryParse(CidrNotation.Split('/', '\\')[0].Trim(), out var parsedNetworkAddress))
                {
                    NetworkAddress = parsedNetworkAddress;
                }
                else
                {
                    WriteError(new ErrorRecord(
                                   new ArgumentException("An IP address could not be identified within the input object. Specify an address and CIDR separated by a forward slash and try again."),
                                   "ParseIPAddressFailed",
                                   ErrorCategory.InvalidArgument,
                                   CidrNotation.Split('/', '\\')[0].Trim()
                                   )
                    {
                        ErrorDetails = new ErrorDetails($"Failed to identify an IP address in the input value '{CidrNotation}'." +
                                                        $" The input value should be an IP address and CIDR separated with a forward slash: for example, '192.168.10.0/24'.")
                    }
                               );
                    return;
                }
                if (int.TryParse(CidrNotation.Split('\\', '/')[1].Trim(), out var cidr))
                {
                    if (cidr > MaxCidrValue)
                    {
                        WriteError(new ErrorRecord(
                                       new ArgumentOutOfRangeException(nameof(CIDR), cidr, $"The identified CIDR value is greater than the maximum allowed value."),
                                       "CidrOutOfRange",
                                       ErrorCategory.InvalidArgument,
                                       cidr
                                       )
                        {
                            ErrorDetails = new ErrorDetails($"The CIDR value '{cidr}' identified within the input object is invalid: the value should not be greater than {MaxCidrValue}.")
                        }
                                   );
                        return;
                    }
                    else if (cidr < 0)
                    {
                        WriteError(new ErrorRecord(
                                       new ArgumentOutOfRangeException(nameof(CIDR), cidr, $"The identified CIDR value is lower than the minimum allowed value."),
                                       "CidrOutOfRange",
                                       ErrorCategory.InvalidArgument,
                                       cidr
                                       )
                        {
                            ErrorDetails = new ErrorDetails($"The CIDR value '{cidr}' identified within the input object is invalid: the value should not be less than 0.")
                        }
                                   );
                        return;
                    }
                    SubnetMask = IPv4TypeConverter.ConvertCidrToSubnetMask(cidr);
                }
                else
                {
                    WriteError(new ErrorRecord(
                                   new ArgumentException("A CIDR value could not be identified within the input object. Specify an address and CIDR separated by a forward slash and try again."),
                                   "ParseCIDRFailed",
                                   ErrorCategory.InvalidArgument,
                                   CidrNotation.Split('/', '\\')[1].Trim()
                                   )
                    {
                        ErrorDetails = new ErrorDetails($"Failed to identify a CIDR value in the input value '{CidrNotation}'." +
                                                        $" The input value should be an IP address and CIDR separated with a forward slash: for example, '192.168.10.0/24'.")
                    }
                               );
                    return;
                }
            }
            if (SubnetMask is null)
            {
                SubnetMask = IPv4TypeConverter.ConvertCidrToSubnetMask(CIDR);
            }

            WriteObject(new IPv4SubnetRange(NetworkAddress, SubnetMask));
        }