VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

script_powercli_renaming distributed port groups

Hi Luc,

can you check folowingscript  orange part .

1:i need to exclude one vm in $vms and store in $vms_supported can we use exclude switch

2:i need to rename distributed portgroups by replacing abc by xyz is this correct what i mentioned in second orange part.

$source_side=read-host "specify cluster"

$cluster_source=get-cluster $source_side

$vms=get-vm -Location $cluster_source

$vms_supported=.....

foreach($vm in $vms_supported)

{

$vm.name

}

$des_cluster=get-cluster -name cluster2

$grb_distributedswitch=Get-VirtualSwitch -Distributed -Datacenter dc2

$port_groups_grbs=Get-VirtualPortGroup -Distributed -VirtualSwitch $dc2_distributedswitch

$pgdc2=$port_groups_dc2|select name

foreach($pg in $pgdc2)

{

$new_name=$pg.replace("abc","xyz")

Set-VirtualPortGroup -VirtualPortGroup $pg -name $new_name -WhatIf

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I assume these 2 questions are not related?

1) If I understand the question correctly, you want to exclude 1 specific VM froma list of VMs?

Then you could do

$source_side = Read-Host "specify cluster"

$cluster_source = Get-Cluster -Name $source_side

$vms = Get-VM -Location $cluster_source


$vms_supported = $vms | where {$_.Name -ne 'VM-to-be-excluded'}

foreach ($vm in $vms_supported) {

   $vm.name

}

2) I would use the VDS cmdlets

Like this

$des_cluster = Get-Cluster -Name cluster2

$grb_distributedswitch = Get-Datacenter -Name dc2 | Get-VDSwitch


$port_groups_grbs = Get-VDPortgroup -VDSwitch $grb_distributedswitch

foreach ($pg in $port_groups_grbs)

{

   $new_name = $pg.Name.replace("abc", "xyz")


   Set-VDPortgroup -VDPortgroup $pg -Name $new_name -WhatIf

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

I assume these 2 questions are not related?

1) If I understand the question correctly, you want to exclude 1 specific VM froma list of VMs?

Then you could do

$source_side = Read-Host "specify cluster"

$cluster_source = Get-Cluster -Name $source_side

$vms = Get-VM -Location $cluster_source


$vms_supported = $vms | where {$_.Name -ne 'VM-to-be-excluded'}

foreach ($vm in $vms_supported) {

   $vm.name

}

2) I would use the VDS cmdlets

Like this

$des_cluster = Get-Cluster -Name cluster2

$grb_distributedswitch = Get-Datacenter -Name dc2 | Get-VDSwitch


$port_groups_grbs = Get-VDPortgroup -VDSwitch $grb_distributedswitch

foreach ($pg in $port_groups_grbs)

{

   $new_name = $pg.Name.replace("abc", "xyz")


   Set-VDPortgroup -VDPortgroup $pg -Name $new_name -WhatIf

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

tx iam checking this

dont we need paramter -VirtualPortGroup  to specify the old port groupname.

Set-VDPortgroup -Name $new_name -WhatIf

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Indeed, corrected the code above.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

tx Luc.

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

if you can also suggest some simple powercli way to add .vmx file to inventory .

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The New-VM cmdlet with the VMFilePath parameter.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos