VMware Cloud Community
thebart
Enthusiast
Enthusiast
Jump to solution

Associating standard port groups to distributed port groups when migrating VMs

$vmlist = foreach ($vHst in $vHost) {$vHst | Get-VM}
$sPGs = foreach($vm in $vmlist){
$NetAdp = Get-NetworkAdapter $vm | Select-Object NetworkName
$NetAdp -replace "[^0-9]"
}

foreach ($PG in $sPGs){
Set-NetworkAdapter -Portgroup (Get-VDPortgroup | Where-Object {($_.Name -match "$PG")} | select Name)
}

I'm attempting to migrate VMs on port groups from my standard switches to new distributed port groups I created.

Get-NetworkAdapter $vm | Select-Object NetworkName

NetworkName
-----------
Vlan2222

Next i'm removing "Vlan" from each network adapter

$sPGs = foreach($vm in $vmlist){
$NetAdp = Get-NetworkAdapter $vm | Select-Object NetworkName
$NetAdp -replace "[^0-9]"
}
$sPGs

2222
2223
2224

Finally i'm attempting to match the output to the new distributed port groups:

Get-VDPortgroup | Where-Object {($_.Name -match "$PG")} | select Name
Name   
----   
DPG-2222

but when i attempt to do this for each VMs network adapter it errors out with:

foreach ($PG in $sPGs){
Set-NetworkAdapter -Portgroup (Get-VDPortgroup | Where-Object {($_.Name -match "$PG")} | select Name)
}
Set-NetworkAdapter : Cannot bind parameter 'Portgroup'. Cannot convert the "" value of type "System.Management.Automation.PSCustomObject" to type "VMware.VimAutomation.ViCore.Types.V1.Host.Networking.VirtualPortGroupBase".
At line:2 char:31
+ ...  -Portgroup (Get-VDPortgroup | Where-Object {($_.Name -match "$PG")}  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-NetworkAdapter], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter

 Essentially all i want to do is migrate all my VMs from the current port groups (Vlanxxxx) to the new distributed port groups (DPG-xxxx)

Reply
0 Kudos
1 Solution

Accepted Solutions
thebart
Enthusiast
Enthusiast
Jump to solution

I replaced "VLAN" with "Vlan" and it is working perfectly!!!

You sir are a legend! thank you!

Get-VM | Get-NetworkAdapter -PipelineVariable vnic |
Foreach-Object -Process {
Set-NetworkAdapter -NetworkAdapter $vnic -Portgroup (Get-VDPortgroup -Name "$($vnic.NetworkName.Replace('Vlan','DPG-'))")
}

View solution in original post

Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

Leave out the Select Name on that last snippet.


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

thebart
Enthusiast
Enthusiast
Jump to solution

First off thank you for taking a look at this you have helped me countless times with your posts.

but i'm now getting this:

foreach ($PG in $sPGs){
Set-NetworkAdapter -Portgroup (Get-VDPortgroup | Where-Object {($_.Name -match "$PG")})
}
cmdlet Set-NetworkAdapter at command pipeline position 1
Supply values for the following parameters:
NetworkAdapter[0]:

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to provide a NetworkAdapter object for the Set-NetworkAdapter cmdlet.
You did a Get-NetworkAdapter in the previous snippet, feed that object to the Set-NetworkAdapter cmdlet.


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

Reply
0 Kudos
thebart
Enthusiast
Enthusiast
Jump to solution

That's what i thought i was doing with $PG

So i also tried to nest the foreach loop with no success:


 

 $sPGs = foreach($vm in $vmlist){
$NetAdp = Get-NetworkAdapter $vm | Select-Object NetworkName
$NetAdp -replace "[^0-9]"


foreach ($PG in $sPGs){
$vm | Set-NetworkAdapter -Portgroup (Get-VDPortgroup | Where-Object {($_.Name -match "$PG")})
}
}
Set-NetworkAdapter : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:7 char:7
+ $vm | Set-NetworkAdapter -Portgroup (Get-VDPortgroup | Where-Object { ...
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (New Virtual Machine:PSObject) [Set-NetworkAdapter], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter​

 


 

Reply
0 Kudos
thebart
Enthusiast
Enthusiast
Jump to solution

Step closer adding Get-networkadapter $VM to the second foreach but now its only applying to a single VM.

foreach ($PG in $sPGs){
    Get-NetworkAdapter $vm | Set-NetworkAdapter -Portgroup (Get-VDPortgroup | Where-Object {($_.Name -match "$PG")})
    }
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm not sure what you are doing with the -replace.
Do you have the VLANId in the VSS Portgroup name?
Are the VDS Portgroup names the same as the VSS Portgroup names?


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

Reply
0 Kudos
thebart
Enthusiast
Enthusiast
Jump to solution

For example:

VSS Portgroup names are Vlan2222 and i've created VDS Portgroup names that correspond to them using DPG-2222.

The -replace drops "Vlan" and leaves me with 2222

$NetAdp

NetworkName
-----------
Vlan2222    



$NetAdp = Get-NetworkAdapter $vm | Select-Object NetworkName
$NetAdp -replace "[^0-9]"
2222

No the VDS and VSS portgroup names are different.

VDS portgroup names start with DPG-xxxx

VSS portgroup names start with Vlanxxxx

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Wouldn't then this also do the trick?

Get-VM | Get-NetworkAdapter -PipelineVariable vnic |
Set-NetworkAdapter -Portgroup (Get-VDPortgroup -Name "$($vnic.Name.Replace('VLAN','DPG-'))") -Confirm:$false


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

Reply
0 Kudos
thebart
Enthusiast
Enthusiast
Jump to solution

I think i understand what you are doing here but can't figure out why i'm getting this error:

Get-VM | Get-NetworkAdapter -PipelineVariable vnic |
Set-NetworkAdapter -Portgroup (Get-VDPortgroup -Name "$($vnic.Name.Replace('VLAN','DPG-'))")
You cannot call a method on a null-valued expression.
At line:3 char:57
+ ... tgroup (Get-VDPortgroup -Name "$($vnic.Name.Replace('VLAN','DPG-'))")
+                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Get-VDPortgroup : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:3 char:54
+ ... tgroup (Get-VDPortgroup -Name "$($vnic.Name.Replace('VLAN','DPG-'))")
+                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-VDPortgroup], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Vds.Commands.Cmdlets.GetVDPortgroup
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM | Get-NetworkAdapter -PipelineVariable vnic |
Foreach-Object -Process {
  Set-NetworkAdapter -NetworkAdapter $vnic -Portgroup (Get-VDPortgroup -Name "$($vnic.NetworkName.Replace('VLAN','DPG-'))") -Confirm:$false
}


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

Reply
0 Kudos
thebart
Enthusiast
Enthusiast
Jump to solution

Get-VM | Get-NetworkAdapter -PipelineVariable vnic |

Foreach-Object -Process {

  Set-NetworkAdapter -NetworkAdapter $vnic -Portgroup (Get-VDPortgroup -Name "$($vnic.Name.Replace('VLAN','DPG-'))") -Confirm:$false

}

Get-VDPortgroup : 11/16/2021 4:52:53 PMGet-VDPortgroupVDPortgroup with name 'Network adapter 1' was not found using the specified filter(s).

At line:3 char:56

+ ... -Portgroup (Get-VDPortgroup -Name "$($vnic.Name.Replace('VLAN','DPG-' ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Get-VDPortgroup], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.Vds.Commands.Cmdlets.GetVDPortgroup

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Oops, that should have been NetworkName


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

thebart
Enthusiast
Enthusiast
Jump to solution

 Get-VM | Get-NetworkAdapter -PipelineVariable vnic |
Foreach-Object -Process {
  Set-NetworkAdapter -NetworkAdapter $vnic -Portgroup (Get-VDPortgroup -Name "$($vnic.NetworkName.Replace('VLAN','DPG-'))")
}
Get-VDPortgroup : 11/16/2021 5:04:29 PM	Get-VDPortgroup		VDPortgroup with name 'Vlan138' was not found using the specified filter(s).	
At line:3 char:56
+ ... -Portgroup (Get-VDPortgroup -Name "$($vnic.NetworkName.Replace('VLAN' ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-VDPortgroup], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.Vds.Commands.Cmdlets.GetVDPortgroup

It looks like it is not able to make that correlation somehow... as if it is still looking for a VDS portgroup starting with "Vlan" instead of "DPG-"

Reply
0 Kudos
thebart
Enthusiast
Enthusiast
Jump to solution

I replaced "VLAN" with "Vlan" and it is working perfectly!!!

You sir are a legend! thank you!

Get-VM | Get-NetworkAdapter -PipelineVariable vnic |
Foreach-Object -Process {
Set-NetworkAdapter -NetworkAdapter $vnic -Portgroup (Get-VDPortgroup -Name "$($vnic.NetworkName.Replace('Vlan','DPG-'))")
}

Reply
0 Kudos