More PowerCLI scripts to make life easier!

Share on:

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.