VMware Cloud Community
Mizzou_RobMan
Contributor
Contributor

modify output or exceptions for list of VMs

I have the following script that looks at all VMs in vCenter and emails once a week to list VMs that reside on the standard vSwitch.  Per our security group we can't have any production VMs residing on a standard vSwitch.  For other reasons we don't set our templates to DvSwitch.

I have VMs that are templates so when I run the following weekly script I'd like to tag or append the displayed output with "-template" or perhaps even not display the templates.

Any thoughts?

########################################################
# Purpose: Determine all VMs that reside on standard vSwitch
#                and emails when complete
########################################################
# Notes:   does NOT use Send-MailMessage @messageParameters -BodyAsHtml
########################################################
#setup Environment
add-pssnapin vmware.vimautomation.core
initialize-vitoolkitenvironment.ps1
. c:\scripts\vmwarevars.ps1
#need this to disable proxy settings
set-powercliconfiguration -proxypolicy noproxy -confirm:$false
#connect to selected Virtualcenter or host server
Connect-VIServer -Server $SERVER -Protocol $PROTOCOL -User $USERNAME -Password $PASSWORD
#name of portgroup whose VMs to get info on
$strPortgroupName = "VM Network"
#$GetVMList= All VMs that are attached to $strPortgroupName
$GetVMList = Get-View -ViewType Network -Property Name,VM -Filter @{"Name" = $strPortgroupName}
#use $variable (in this case $GetVMList) to print to screen or use write-host $variable
#$GetVMList
#$Id= Id of VM (ex: VirtualMachine-vm-xxxxx)
#$VMView= retrieves Name of VM
#`r = Carriage return `n = New line
foreach ($Id in $GetVMList.VM) {
$VMView = Get-View -Id $Id
$VMView.Name
$msgreport = $msgreport + $VMView.Name + "`n"
}
#disconnect viserver session to clean things up
disconnect-viserver -confirm:$false
   
#Email HTML Results
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpServer = "emailserver.mydomain.com"
$SmtpClient.host = $SmtpServer
$MailMessage.From = "sender@mydomain.com"
$MailMessage.To.add("receiver@mydomain.com")
$MailMessage.Subject = "$SERVER VMs on '$strPortgroupName' standard vSwitch"
$MailMessage.IsBodyHtml = 0
$MailMessage.Body = $msgreport
$SmtpClient.Send($MailMessage)
The output is similiar to the following:
Win2k8r2
Win2k8
Win2k3Std32bit
SuseBase
UbuntuBase
Tags (3)
0 Kudos
4 Replies
LucD
Leadership
Leadership

Just to make sure, you want to also list the templates that are connected to a portgroup on a dvSwitch ?

If yes, will you provide the name of the portgroup ?

Or should the script do a generic search for dvSwitch portgroups ?


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

CRad14
Hot Shot
Hot Shot

If I am reading your request correctly...it should be as easy as setting up a switch parameter then finding all the templates...you may want to look at the template format to be something you are looking for...hope this helps

param([switch]$template)

if($template)

$vmtemplates=get-template

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
Mizzou_RobMan
Contributor
Contributor

LucD:

Thanks for presenting at VMworld.

I am only concerned with generic dvSwitch portgroups.

Thanks,

Rob

0 Kudos
LucD
Leadership
Leadership

Thanks, much appreciated.

To get all the templates that are attached to portgroups on dvSwitches, you use something like this

Get-VirtualSwitch -Distributed | Get-VirtualPortGroup | %{
  if($_.ExtensionData.Vm){
    Get-View -Id $_.ExtensionData.Vm |
   
where {$_.Config.Template} |
   
Select Name
  } }

It gets each dvSwitch portgroup and looks at the connected VirtualMachines. On those it only selects the ones that are Templates.


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

0 Kudos