how to generate continuous IP address with powershell

As we know , 1…5 will generate 1,2,3,4,5

But I want to get a range of continuous IP address from 10.1.1.1 to 10.1.1.10 , but bellow result is not I want from PS

[002]: PS C:\> 10.1.1.1 .. 10.1.1.10
0
[002]: PS C:\>

I also want to generate a continuous strings begin with P-P01 and end with P-P05

But I don’t know how to do that, any one can show me your fantastic code ?

 

BR

 

Hey tlsalex27,

The following code is an example of how you can do it for the IP addresses.

[pre] 1…10 | foreach { “10.1.1.$($_)” } [/pre]

For the example with PP, it would be the following.

[pre] 1…5 | foreach { “P-P0$($_)” } [/pre]

Regards,
Tom

Little bit shorter

[pre]

1…10 | foreach { “10.1.1.$_” }

[/pre]

 

Oh yeah, double quotes… :slight_smile:

Thank you so much to All

[quote quote=148521]Oh yeah, double quotes…

[/quote]
Your version was also correct,

but a bit to long :stuck_out_tongue:

Another method for IP addresses, that will handle any starting IP and correctly rollover 255s:

[IPAddress]$StartingAddress = "10.1.1.250"
1..1000 | ForEach-Object {
    ([IPAddress]($StartingAddress.SortableAddress + $_)).ToString()
}

It is a bit too long, though… :wink:

Just in case foreach makes you die a little inside like Don Jones…

1..10 -replace '^','10.1.1.'

To set an IP address on a network adapter in Windows, we have the New-NetIPAddress command. This command is part of the NetTcpIp module and is included with PowerShell v5 and later. But, before we change something, we should always check the current configuration. To get the current IP address, we’ll use Get-NetIPAddress.

I like this version,thank you

Sorry , I’m a little confused on bellow two implements as both of them work well.

the first one, $_ it means the current PSobject , and $($) , it means it will calculate the value of current PSobject , or it means it will reference current PSobject ? when we can ignore the first $ in the $($) , and when we can not ?

1..10 | foreach { "10.1.1.$($_)" }
1..10 | foreach { "10.1.1.$_" }

They both mean the same in this case.

If you would have a variable Name (for example) and you would put it like “$Name”, then PowerShell will take the value of $Name. But if you would have a hashtable variable with multiple objects in it, PowerShell can’t just translate that into a string. Since PowerShell can’t recognize the dot in a string as a way to mention a property of an object, you need to do this by using the following technique $($HashTable.Property1).

So, $_ and $($_) do the same thing and it’ll both reference the current object in the pipeline. You would need to use the second technique to get a property of an object into a string (double quotes, single is literal).

[quote quote=148902]They both mean the same in this case.
But if you would have a hashtable variable with multiple objects in it, PowerShell can’t just translate that into a string. Since PowerShell can’t recognize the dot in a string as a way to mention a property of an object, you need to do this by using the following technique $($HashTable.Property1).
[/quote]
Sorry , I still not quite understand. Let’s do bellow operation. (BTW , I may understand the difference between double quotes and single quotes in PS )
As bellow , $lsass represent a object , and this object has many properties , how ever the first result of 1…3 return a number which its type is int32 , and it does not have any properties.no need to any evaluate operation when reference to it (no dot to it )

So what I want to say is , we need to use $($lsass.property) to take it into a string , because powershell can not understand the dot in a string in $lsass.property ,right ? the $() will cause $lsass.property evaluate its value , so we need two $ , the first $ and () will force the expression which is $lsass.property to evaluate its value , is it Comme ça?

PS D:\> 1..3 | foreach {"the $_ time of $lsass.ProcessName"}
the 1 time of System.Diagnostics.Process (lsass).ProcessName
the 2 time of System.Diagnostics.Process (lsass).ProcessName
the 3 time of System.Diagnostics.Process (lsass).ProcessName

PS D:\> 1..3 | foreach {"the $_ time of $($lsass.ProcessName)"}
the 1 time of lsass
the 2 time of lsass
the 3 time of lsass

PS D:\> 1..3 | select -First 1 | Get-Member

   TypeName: System.Int32

PS D:\> $lsass | Get-Member


   TypeName: System.Diagnostics.Process

Yes, it is like that. The $() will force the subexpression and everything in it will be treated as PowerShell commands.

$( ) has a use outside of strings as well. Like if you want to pipe multiple commands to something else. You couldn’t do it with just ( ).

$(get-date; get-childitem foo) | Measure-object

Count             : 6
Average           : 
Sum               : 
Maximum           : 
Minimum           : 
StandardDeviation : 
Property          : 

You can even use $( ) in an ad filter, if you quote it just so…

import-csv users.txt -header surname,givenname |
foreach-object { 
  get-aduser -filter "surname -eq '$($_.surname)' -and givenname -eq '$($_.givenname)'" 
}