Author Posts

March 23, 2017 at 10:10 pm

Hello, for a while I've been banging my head against something that seems like it should be fairly simple. I have a list of server names, and I'm trying to write a script that will automatically increment through the server names, and on each server name assign 10 consecutive IPs. Each time the script goes to the next computer name, the consecutive IP start and end point needs to increment by 10. For example:
SERVER1
192.168.0.1
.2
.3
.4
.5
.6
.7
.8
.9
.10
SERVER2
192.168.0.11
.12
.13
.14
and so on

I thought I had what I wanted when I was just using "write-host (IP address generated)" to create the script. However once I added the invoke command part for each machine, it broke because the variable incrementing is not carried over to the next computer in the list, so it ends up assigning the same 10 IPs over and over again for each server. Here is the code I have that will just print out the computer number (will be replaced with servername-#) and the IPs that I'd like to assign to that computer name/number. I just can't seem to figure out how to get to actually invoke the command to assign those IPs on each machine without breaking the incrementation. If someone could show me how to make this actually assign the IPs for each Server name/number with invoke command without the incrementation scheme breaking I'd be very grateful. Possibly something to do with run as job?

$counterdiff=0
for ($counter=14; $counter -le 21; $counter++)
{

 write-host $counter

for ($counter2=1; $counter2 -le 10; $counter2++)
   {
        write-host "150.0.2.$($counter2+$counterdiff)"
   }
$counterdiff=$counterdiff+10
 }

March 23, 2017 at 10:44 pm

#region Input
$ServerList   = @('Server1','Server2','Server3')
$SubnetPrefix = '192.168.0'
$IPsPerServer = 10
$StartIP      = 1
#endregion

#region IP Loop
foreach ($ServerName in $ServerList) {
    0..$($IPsPerServer-1) | % {
        "Assigning IP address '$SubnetPrefix.$($StartIP + $_)' to Server '$ServerName'"
    }
    $StartIP += $IPsPerServer
}
#endregion

March 24, 2017 at 12:29 am

Thanks.. that almost works, except I run into the same issue when I try to invoke command with the variables you set up instead of just writing "assigning IP addresses". Once you invoke command on a remote computer, the variables only exist on that remote computer and not the computer running the script so the whole thing breaks down. Also I need to have the server list auto incremented, I can't have them be in here manually like "server1, server2, server3" I tried modifying your version but I end up with the same issue.

#region Input
$ServerList   = @('server-1','server-2','server-3')
$SubnetPrefix = '192.168.0'
$IPsPerServer = 10
$StartIP      = 1
#endregion

#region IP Loop
foreach ($ServerName in $ServerList) {
    0..$($IPsPerServer-1) | % {
        "Assigning IP address '$SubnetPrefix.$($StartIP + $_)' to Server '$ServerName'"
        Invoke-Command -ComputerName $servername -Credential $win_credentials -ScriptBlock {$testvar=(Get-NetAdapter -physical | where Linkspeed -eq '10 gbps').InterfaceAlias
        Set-NetAdapterAdvancedProperty -name $testvar -DisplayName "vlan id" -DisplayValue "1500";start-sleep -Seconds 5
        New-NetIPAddress -InterfaceAlias $testvar -ipaddress $SubnetPrefix.$($StartIP + $_) -AddressFamily ipv4 -PrefixLength 24} 
                              }
    $StartIP += $IPsPerServer
                                     }
#endregion

March 24, 2017 at 8:26 pm

Ok so after more research I figured this out! I just needed to know about scoping! By using "$using:variable" I was able to get the counters running inside the invoke command to use variables from outside the command! JOY!

March 25, 2017 at 2:07 am

#region Input
$ServerList   = @('server-1','server-2','server-3')
$SubnetPrefix = '192.168.0'
$IPsPerServer = 10
$StartIP      = 1
#endregion

#region IP Loop
foreach ($ServerName in $ServerList) {
    0..$($IPsPerServer-1) | % {
        $IPAddress = "$SubnetPrefix.$($StartIP + $_)"
        "Assigning IP address '$IPAddress' to Server '$ServerName'"
        Invoke-Command -ComputerName $servername -Credential $win_credentials -ScriptBlock {
            $Adapter = (Get-NetAdapter -physical | where Linkspeed -eq '10 gbps').InterfaceAlias
            Set-NetAdapterAdvancedProperty -name $Adapter -DisplayName "vlan id" -DisplayValue "1500"
            Start-Sleep -Seconds 5
            New-NetIPAddress -InterfaceAlias $Adapter -ipaddress $Using:IPAddress -AddressFamily ipv4 -PrefixLength 24
        } 
    }
    $StartIP += $IPsPerServer
}
#endregion