示例#1
0
 /// <inheritdoc/>
 protected override void ProcessRecord()
 {
     foreach (var input in InputObject)
     {
         WriteObject(IPv4TypeConverter.ConvertNumberToIPv4(input));
     }
 }
 /// <inheritdoc/>
 protected override void ProcessRecord()
 {
     foreach (var input in InputObject)
     {
         if (input.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
         {
             WriteError(new ErrorRecord(
                            new PSArgumentException("The address provided is not an IPv4 address (address family is not InterNetwork)."),
                            "InvalidAddressFamily",
                            ErrorCategory.InvalidArgument,
                            input)
             {
                 ErrorDetails = new ErrorDetails($"The IP address '{input}' is not an IPv4 address. Only IP addresses " +
                                                 $"in the {System.Net.Sockets.AddressFamily.InterNetwork} address family can be converted by this cmdlet.")
                 {
                     RecommendedAction = "Only provide IPv4 addresses."
                 }
             }
                        );
         }
         else
         {
             WriteObject(IPv4TypeConverter.ConvertIPv4ToNumber(input));
         }
     }
 }
 /// <summary>
 /// Converts an IP address the associated <see cref="long"/> value.
 /// </summary>
 /// <param name="ipAddress">The address to be converted.</param>
 /// <returns>The <see cref="long"/> value representation of <paramref name="ipAddress"/>.</returns>
 public static long ToLong(this IPAddress ipAddress) => IPv4TypeConverter.ConvertIPv4ToNumber(ipAddress);
示例#4
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));
        }