Automation

 View Only
  • 1.  Changing network adapters

    Posted Jul 03, 2019 07:31 AM

    Hi,

    A little help please, as part of my script i am trying to change the backing on a list of vms which all have different portgroups, if they = this then change to that. Below is what i have i get no erros the script runs but the guest does not change and i have copied and pasted the variables from vCenter so should be no spelling issues.

    foreach ($vm in $vmlist) {

        $adp = get-vm $vm | get-networkadapter

        if ($adp -eq $oldNet1){

            set-networkadapter -NetworkName $PG1 -confirm:$false

        }elseif ($adp -eq $oldNet2){

            set-networkadapter -NetworkName $PG2 -confirm:$false

        }elseif ($adp -eq $oldNet3){

            set-networkadapter -NetworkName $PG3 -confirm:$false

        }elseif ($adp -eq $oldNet4){

            set-networkadapter -NetworkName $PG4 -confirm:$false

        }

    }

    however if i run the one liner below it works fine.

    get-vm $vm | get-NetworkAdapter | where {$_.NetworkName -eq $oldNet1} | set-networkadapter -NetworkName $PG1 -confirm:$false



  • 2.  RE: Changing network adapters

    Posted Jul 03, 2019 07:37 AM

    If i actually put that last line in a foreach statement i can get it to work but not sure thats the best way to do it as it takes a while..

    foreach ($vm in $vmlist) {

    get-vm $vm | get-NetworkAdapter | where {$_.NetworkName -eq $oldNet1} | set-networkadapter -NetworkName $PG1 -confirm:$false

    get-vm $vm | get-NetworkAdapter | where {$_.NetworkName -eq $oldNet2} | set-networkadapter -NetworkName $PG2 -confirm:$false

    get-vm $vm | get-NetworkAdapter | where {$_.NetworkName -eq $oldNet3} | set-networkadapter -NetworkName $PG3 -confirm:$false

    get-vm $vm | get-NetworkAdapter | where {$_.NetworkName -eq $oldNet4} | set-networkadapter -NetworkName $PG4 -confirm:$false

    }



  • 3.  RE: Changing network adapters

    Posted Jul 03, 2019 08:08 AM

    You have to provide a NetworkAdapter object to the Set-NetworkAdapter cmdlet.

    It is a required parameter.

    foreach ($vm in $vmlist) {

       $adp = Get-VM $vm | Get-NetworkAdapter

       if ($adp -eq $oldNet1){

       Set-NetworkAdapter -NetworkAdapter $adp -NetworkName $PG1 -confirm:$false

       }elseif ($adp -eq $oldNet2){

       Set-NetworkAdapter -NetworkAdapter $adp -NetworkName $PG2 -confirm:$false

       }elseif ($adp -eq $oldNet3){

       Set-NetworkAdapter -NetworkAdapter $adp -NetworkName $PG3 -confirm:$false

       }elseif ($adp -eq $oldNet4){

       Set-NetworkAdapter -NetworkAdapter $adp -NetworkName $PG4 -confirm:$false

       }

    }



  • 4.  RE: Changing network adapters

    Posted Jul 03, 2019 08:59 AM

    Thanks Luc,

    I've run your code and it still does nothing....



  • 5.  RE: Changing network adapters
    Best Answer

    Posted Jul 03, 2019 09:04 AM

    If you have a string value in the $oldNet1, $oldNet2... variables,
    you will probably have to do like this

    foreach ($vm in $vmlist) {

       $adp = Get-VM $vm | Get-NetworkAdapter

       if ($adp.NetworkName -eq $oldNet1){

       Set-NetworkAdapter -NetworkAdapter $adp -NetworkName $PG1 -confirm:$false

       }elseif ($adp.NetworkName -eq $oldNet2){

       Set-NetworkAdapter -NetworkAdapter $adp -NetworkName $PG2 -confirm:$false

       }elseif ($adp.NetworkName -eq $oldNet3){

       Set-NetworkAdapter -NetworkAdapter $adp -NetworkName $PG3 -confirm:$false

       }elseif ($adp.NetworkName -eq $oldNet4){

       Set-NetworkAdapter -NetworkAdapter $adp -NetworkName $PG4 -confirm:$false

       }

    }

    ---------------------------------------------------------------------------------------------------------

    Was it helpful? Let us know by completing this short survey here.



  • 6.  RE: Changing network adapters

    Posted Jul 03, 2019 10:19 AM

    worked a treat..

    Thank you very much