VMware Cloud Community
Mizzou_RobMan
Contributor
Contributor
Jump to solution

PS script to display VMs residing on a specified vSwitch

In the process of migrating VMs to vSphere 5 running Cisco Nexus 1000v.  However, I need to generate a list of VMs that still reside on the old vSwitch across a datacenter.

I know the VMware powerpack will display the VMCount, but it would be helpful to be able to automatically run the script every morning and email the list of VMs that reside on the old vSwitch.

Running vCenter 5.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You probably have more than 1 portgroup with that name

Try it like this

#
# name of portgroup whose VMs to get info on

$strPortgroupName = "MyPortgroupName" #
# Get the View object of the Network of the given name, then get the View objects of the VMs on that network, and return the VMs' names
Get-View -Id (Get-View -ViewType Network -Property Name,VM -Filter @{
"Name" = $strPortgroupName} | %{$_.VM}) -Property Name | select Name


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

View solution in original post

0 Kudos
7 Replies
AureusStone
Expert
Expert
Jump to solution

This should work.  It is not that quick, but it should be quick enough to a temp daily script. For some reason HTML is not working, so sorry about the formatting.

$whatSwitch = "<switchname>"

$vCenterServer = "<vCenterServer>"

Connect-VIServer $vCenterServer
$allVMs = Get-VM
Write-Host "All VM data collected.  About to query VMs to find any VMs that reside on $whatSwitch"
$report = @()

ForEach ($virtualMachine in $allVMs)
    {
    If (((Get-VirtualSwitch -VM $virtualMachine.name).Name) -match $whatSwitch)
        {
        Write-Host "The VM $virtualMachine.Name is on $whatswitch"
        $row = "" | select VMName
        $row.VMName = $virtualMachine.Name
        $report += $row
    }
    else
        {
    Write-Host "."
    }
}
Disconnect-VIServer * -Confirm:$false
$output = "c:\temp\switchAudit.csv"
$report | export-csv $output -NoTypeInformation

mattboren
Expert
Expert
Jump to solution

Hello, Mizzou_RobMan-

The VMware PowerPack for PowerGUI, the "Filter" where it shows Networks with the corresponding "VMCount" values -- that is showing info for portgroups.  To get the corresponding VM names, you can use the .VM property of the given portgroup objects (as returned by Get-View), as such:

## name of portgroup whose VMs to get info on
$strPortgroupName = "MyPortgroupName"

## Get the View object of the Network of the given name, then get the View objects of the VMs on that network, and return the VMs' names
Get-View -Id (Get-View -ViewType Network -Property Name,VM -Filter @{"Name" = $strPortgroupName}).VM -Property Name | select Name

This gets the View object for each VM MoRef in the .VM property of the portgroup View object, and returns the VMs' names.  You can do this for each portgroup you desire.  How does that do for you?

Mizzou_RobMan
Contributor
Contributor
Jump to solution

Hmm, got an error..

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or
empty and then try the command again.
At C:\Users\MyUserAccount\Desktop\forums-2.ps1:9 char:13
+ Get-View -Id <<<<  (Get-View -ViewType Network -Property Name,VM -Filter @{"Name" = $strPortgroupName}).VM -Property Name |
select Name
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.Ge
   tVIView

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You probably have more than 1 portgroup with that name

Try it like this

#
# name of portgroup whose VMs to get info on

$strPortgroupName = "MyPortgroupName" #
# Get the View object of the Network of the given name, then get the View objects of the VMs on that network, and return the VMs' names
Get-View -Id (Get-View -ViewType Network -Property Name,VM -Filter @{
"Name" = $strPortgroupName} | %{$_.VM}) -Property Name | select Name


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

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can get a list of all the VM's that use specific vswitch with a PowerCLI oneliner. E.g. if your vswitch is named vDswitch3, you can use the following command to list all the virtual machines that use vDswitch3:

Get-VirtualSwitch -Name vDswitch3 | Get-VM


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
Mizzou_RobMan
Contributor
Contributor
Jump to solution

LucD- how do I get that one liner to output to an email? :smileyblush:

0 Kudos
AureusStone
Expert
Expert
Jump to solution

I didn't realise that one liner would work.  Awesome. Smiley Happy

To send the file as an email use "Send-MailMessage"

Send-MailMessage has been available since PS V2.  Before that you have to use .Net

It is a really easy cmd so just look at the man pages and you should have no problems.

0 Kudos