VMware Cloud Community
JasonHess
Contributor
Contributor

list vm networks for a list of vm's

I'm trying to take a list of vm's and get the network they are conencted to.

I'll then want to that list of vm's and change the port profile off the 1000v and onto a standard vswitch with a similar (but different) name.

So far I'm jsut trying to list out vm names and the network name they are connected to.

$path = "c:\temp\vms.csv"

Import-csv -path $path |

foreach-object `

{

Get-VM $_.vmname | Get-NetworkAdapter | Out-File c:\temp\test.txt -Append

}

unfortunatly, this only gives me the details of the nics and not the name of the VM

in addition, the network names are so long that the output is truncated like "Interalname_netw..."

0 Kudos
5 Replies
mattboren
Expert
Expert

Hello, Jason-

You were pretty close -- instead of using the Out-File cmdlet, though, try it with the Export-Csv cmdlet, which writes the desired info out to a CSV file.

Below, I typed up a couple of ways to get this VM network info.  The first way makes a separate "row" for each network to which each VM is connected. If a VM is connected to one network, then one row.  Two networks, two rows, etc:

$path = "c:\temp\vms.csv"
Import-Csv -Path $path | Foreach-Object {
   
## create a new entry for every network to which each VM is connected
    Get-VM $_.VMName | %{$strVMName = $_.Name; Get-NetworkAdapter -VM $_ | Select @{n="VMName"; e={$strVMName}}, NetworkName}
} |
Export-Csv c:\temp\vmNetwkInfo.csv -NoTypeInformation -UseCulture

The second way just makes one row per VM with a field that is a string of the names of all of the networks to which the given VM is connected.

$path = "c:\temp\vms.csv"
Import-Csv -Path $path | Foreach-Object {
   
## put the names of all the VM's networks into one field (so, one row per VM)
    Get-VM  $_.VMName | Select Name, @{n="NetworkNames"; e={(Get-NetworkAdapter -VM $_ | %{$_.NetworkName}) -join ","}}
} |
Export-Csv c:\temp\vmNetwkInfo.csv -NoTypeInformation -UseCulture

Give them a try and see which you like.

JasonHess
Contributor
Contributor

awesome reply and quick too.

I opted for the 1st option, but not married to it.

Could you help with the changing of the nic too?

My ultimate goal is to get a csv like you provided, then feed that back in (after making some manual change in excel) to "flip" the VM from the 1000v to a standard vswitch.  I don't see a field to target vswitch0 vs. 1000v's name?

or is that going to be too complicated.. or shoudl I start another thread for "points"? (I'm new to posting up and asking for help.. usually I just muddle through or do things manually)

Thanks!

0 Kudos
mattboren
Expert
Expert

Hello, again-

Ah-ha.  Nope, not too complicated (and this thread will be fine for this one).  If the goal is really to change VMs' NICs from using one network (on a vDS) to using another network (on a standard switch), you can go about it without exporting the VM network.  Rather, you can just have a series of calls to set the network adapters' network name.

Example:

## network name to change adapters off of
$strOriginalNetworkName = "myVDSNetwork"
## network name to change adpaters to use
$strNewNetworkNameToUse = "myStdNetwork"

Import-Csv -Path "c:\temp\vms.csv" | Foreach-Object {
   
Get-VM $_.VMName | Get-NetworkAdapter | Where-Object {$_.NetworkName -eq $strOriginalNetworkName} | Set-NetworkAdapter -NetworkName $strNewNetworkNameToUse -Confirm:$false -WhatIf
}
## end foreach-object

This uses the Set-NetworkAdapter cmdlet to change the network adapters on any of the given VMs from using the network on the vDS (by name) to using the specified name of the network (presumably on the desired switch).  No mess/fuss with exporting the VMs' network adapter info, only to turn right around and import it again -- just uses the info in the pipeline.

And, I left the "-WhatIf" parameter in the Set-NetworkAdapter part, so you can run this as is first and just see what is going to happen (without actually changing the network adapters' network name).  If it seems like it is going to do what you want, just take out that -WhatIf.

How does that do?

0 Kudos
JasonHess
Contributor
Contributor

hmmm.. So are you ready for the whole story now? haha

this mogration goes like this:

1000v->vswitch0->vmotion(to another host with a matching vswitch0)->1000v(this different 1000v has slightly different portprofile name)

1000v (a) might have "internal_vlan"

1000v (b) might have "internal_vlan_165" (they found not having the vlan number in the portprofile name ot be problematic for administration.. so they are taking this migration as an oppurtunity to add the vlan number in the new environmet).

you're script would get us from 1000v(a) to vswitch0.. I was wanting to dump out the network info to csv so I could modify the new network name for the last step.

This help is really appreciated and impressive.  Can't thank you enough.

0 Kudos
mattboren
Expert
Expert

Alright, so the story changes a little.

I would still probably keep with the info in the pipeline, rather than writing data out to CSVs, importing it back into PowerShell, etc.  So, the following builds on the last portion of code I posted here.  After setting the given VMs' network adapters from the "internal_vlan" network of vDS 1000v(a) to the network on the std vSwitch, there is a Move-VM to vMotion the VMs to a host with the new vDS.  Then (after you wait for the vMotions to finish) there is a piece that sets the given VMs' NICs from the std vSwitch network to using the "internal_vlan_165" on vDS 1000v(b).  Like:

## network name to change adapters off of
$strOriginalNetworkName = "internal_vlan"
## std network name to change adpaters to use temporarily
$strNewStdNetworkNameToUse = "myStdNetwork"
## network name on vDS on destination host(s) to use
$strNewVDSNetworkName = "internal_vlan_165"
## name of VMHost to which to move VMs
$strNewVMHostName = "myHost2"

$colVMsToUpdateAndMove = Import-Csv -Path "c:\temp\vms.csv"
$colVMsToUpdateAndMove | Foreach-Object {
   
$vmThisVM = Get-VM $_.VMName
   
## set the VM's NICs that use the 1000v(a) network to use a network on the std vSwitch
    Get-NetworkAdapter -VM $vmThisVM | Where-Object {$_.NetworkName -eq $strOriginalNetworkName} | Set-NetworkAdapter -NetworkName $strNewStdNetworkNameToUse -Confirm:$false
   
## initiate vMotion of the VM to the given host
    Move-VM -VM $vmThisVM -RunAsync -Destination $strNewVMHostName -Confirm:$false
}
## end foreach-object

## wait for vMotions to finish, if not done yet

## set the moved VMs' network adapters that are on the temporary standard vSwitch to use the network on the vDS on the new host
$colVMsToUpdateAndMove | Foreach-Object {
   
## set the VM's NICs that use the std vSwitch network to use the given 1000v(b) network
    Get-VM $_.VMName | Get-NetworkAdapter | Where-Object {$_.NetworkName -eq $strNewStdNetworkNameToUse} | Set-NetworkAdapter -NetworkName $strNewVDSNetworkName -Confirm:$false
}
## end foreach-object

Does that get you to where you want to be?

If this is for a large number of VMs, you might update it a bit to set the Destination parameter of the Move-VM calls to be a random host in the destination cluster.

0 Kudos