Category: PowerCLI

Home / Category: PowerCLI

More PowerCLI scripts to make life easier!

December 11, 2019 | PowerCLI, VMware | No Comments

Hello!

Inspired by my last blog post, here are more PowerCLI scripts to make life easier.

Script one; get VMs on a specific host with a specific Port Group

$viserver = "vCenter IP/FQDN here"
$t_host = "ESXi FQDN/IP here as used in vCenter"
$portgroup = "port group name here"

Connect-VIServer $viserver
Get-Cluster -VMHost $t_host | Get-VMHost | Get-VM | Get-NetworkAdapter | Where-Object{$_.NetworkName -like $portgroup}

Script two; get VMs on a specific host with a specific Port Group and move them over to a new Port Group

$viserver = "vCenter IP/FQDN here"
$t_host = "ESXi FQDN/IP here as used in vCenter"
$srcportgroup = "Source port group name here"
$dstportgroup = "Destination port group name here"

Connect-VIServer $viserver
Get-Cluster -VMHost $t_host | Get-VMHost | Get-VM | Get-NetworkAdapter | Where-Object{$_.NetworkName -like $srcportgroup} | Set-NetworkAdapter -Portgroup $dstportgroup -Confirm:$false

Note that the above script can also be made in a while loop and ask for user input each time, just move the Read-Host around with hard-coded values:

Do {
    $viserver = "vCenter IP/FQDN here"
    $t_host = "ESXi FQDN/IP here as used in vCenter"
    $srcportgroup = Read-Host -Prompt "Source port group"
    $dstportgroup = Read-Host -Prompt "Destination port group name here"
    
    Connect-VIServer $viserver
    Get-Cluster -VMHost $t_host | Get-VMHost | Get-VM | Get-NetworkAdapter | Where-Object{$_.NetworkName -like $srcportgroup} | Set-NetworkAdapter -Portgroup $dstportgroup -Confirm:$false
    } while($true)

These are simple scripts are they are not optimized, for any comments or improvements, feel free to contact me through email or Twitter, found here.

Thank you for reading and have a good day! See you at the next post.

Hi everyone,

In this post I am sharing a PowerCLI script that I made in order to easily migrate VMs from one network to the other.

There are two versions of this script, one version has a hard-coded ESXi/vCenter instance, and the other one asks for the vCenter on each run.

Version 1 (hard coded ESXi/vCenter):

Do {
$vspherehost = "ADD-ME"
$src = Read-Host -Prompt "Input source network"
$dst = Read-Host -Prompt "Input destination network"

Connect-VIServer $vspherehost
Get-VM | Get-NetworkAdapter | where{$_.NetworkName -like $src} | Set-NetworkAdapter -NetworkName $dst
} while($true)

Version 2 (Asks for ESXi/vCenter on reach run):

Do {
$vspherehost = Read-Host -Prompt "Input vCenter appliances or ESXi host"
$src = Read-Host -Prompt "Input source network"
$dst = Read-Host -Prompt "Input destination network"

Connect-VIServer $vspherehost
Get-VM | Get-NetworkAdapter | where{$_.NetworkName -like $src} | Set-NetworkAdapter -NetworkName $dst
} while($true)

As you can see, I opted for a while loop so I can change multiple networks at once, because it simply runs the script again for me instead of me having to manually run it again.

The script is not optimized as you can see, for any improvements or ideas, please email me using my contact page.

Thank you for reading and I hope to see you soon in a new post which will be about the vExpert program.