powershell - Rename multiple nics using a CSV file -


i cannot seem figure array/loop combination out. looking run script obtain active nics , rename them based on csv file. afterwards, create nic team , utilize csv file again setup static networking information.

i obtain active nics using:

$adapter = get-netadapter | {$_.status -eq "up"} 

the csv file headings in following form:

host    newname   ipaddress   subnet    gateway    dns 

the newname column has value of admin1a;admin1b.

here example works produces errors:

foreach ($entry in $dc1_nics) {     $newname = $entry.newname.split(";")     foreach ($item in $newname) {         foreach ($nic in $adapter) {             rename-netadapter -name $nic.name -newname $item         }     } } 

so, have 2 names can split, admin1a , admin1b cannot seem nics named appropriately, either 1 nic gets renamed or both , there additional errors.

the problem nested foreach loops, because takes first name, each nic tries name nic name, , moves second name, , each nic tries name that, have server 2 nics, , csv gives names admin1 , admin2. loops through names, , run internal loop each time:

rename nic1 admin1
rename nic2 admin1

then same thing second name, admin2:

rename nic1 admin2
rename nic2 admin2

so that's crux of problem... need iterate through both nics, , new names. in case need for loop. base off of number of nics, , let's hope have enough names specified in csv.

foreach ($entry in $dc1_nics) {     $adapter = get-netadapter | {$_.status -eq "up"}     $newname = $entry.newname.split(";")     for($i = 0;$i -lt $adapter.count;$i++){         rename-netadapter -name $adapter[$i].name -newname $newname[$i]     } } 

so first nic gets first name, , second nic gets second name, , if there's third nic third name. doesn't solve fact looks designed multiple servers, , performing function on host server, solves issue of problem asked about.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -