2

I have some values that I want formatted in a specific way. To be very clear, its an IP address octet and I want it in a "xxx" format. For example if I have a octet value of "20" I want to display it as "020" not "20".

I'm ultimately going to have an array filled with IP's and I want to properly format all of them.

Here is one example that I know works, and I've figured out vie a get-member that this is an integer.

$Test = 1

$Test.ToString("000")

001

This does NOT work. I've figured out via a Get-Member that the value is a string already.

$Test = "1"

$test.ToString("000")

Cannot find an overload for "tostring" and the argument count: "1".
At line:1 char:2
+ $test.tostring("000")
+  ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Any idea on how I can get the value that's already a string formatted like i can with the value that's an integer? Long term i'm hoping the example $test will actually contain a full IP address.

For example 10.10.10.10 I'd like to be 010.010.010.010

Answer For those interested. By utilizing @GregL example I was able to put together a working example for a full IP format with leading zeros which is shown below for reference.

$Test = "10.10.10.10"
$test2 = ($test.Split(".") | ForEach-Object {$_.tostring().padleft(3, '0')}) -join "."
3
  • .TosString() isn't a method. Is that a typo?
    – jscott
    Mar 18, 2015 at 15:55
  • yes, but the result is the same Mar 18, 2015 at 15:57
  • You should use the IPAddress, IPHostEntry, etc. .NET classes for playing with IP addresses. They are specifically designed to help prevent this sort of confusion, and they contain helpful parse to toString methods.
    – Ryan Ries
    Mar 23, 2015 at 14:40

2 Answers 2

4

Presumably you're trying to format them this way so that they all show up nicely aligned.

The only problem is that 020 isn't the same as 20. Not according to this anyway.

Windows for Workgroups with TCP/IP-32 and Windows NT utilities can accept Internet Protocol (IP) addresses comprised of decimal, octal, or hexadecimal numbers. This can cause confusion if you unintentionally use a leading zero in a decimal octet. With a leading zero, the number is resolved by these utilities as an octal number, thus specifying the wrong IP address.

The article says that it only applies to WIndows 2000 and NT, but I just tried it on my Windows 7 PC and it's the same story.

To answer your initial question anyway, use the PadLeft method:

$Test = 1
$Test.ToString().PadLeft(3, '0')
001
3
  • +1 Beat me to it whilst editing.
    – jscott
    Mar 18, 2015 at 16:01
  • @jscott: That'll teach you to edit anything! :)
    – GregL
    Mar 18, 2015 at 16:02
  • Thank you this is exactly what I needed to put everything together. Mar 18, 2015 at 16:18
0

I use these great Subnet Math functions written by Chris Dent. Store your list of addresses in the pure decimal (not dotted decimal) or pure binary values. Sort the list, then retrieve the list, and finally display the list utilizing the ConvertTo-DottedDecimalIP function. Lots of other great subnet functions in his blog.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .