VMware Cloud Community
esxi1979
Expert
Expert
Jump to solution

Tags missing vms - Enhancement to script

Hi

Here is the code , thanks to @LucD in my last post he helped to get this one, as that was marked solved, starting new one

Get-VM | Where { $_.PowerState -eq 'PoweredOn' }| where{(Get-Annotation -Entity $_ -CustomAttribute OS).Value -eq ''} | Select Name | Export-Csv -NoTypeInformation -UseCulture c:\temp\test.csv

Send-MailMessage -To "xxx" -From "xxx" -Subject "Here is list of VMs where one of the costume attributes are missing" -smtpserver xx -Attachments c:\temp\test.csv
#

 

Any suggestions for how can i ensure send email only if the get-vm command shows 1 or more vms ?

 

Also if i have custom attributes simply a "or" will help ? ie say where{(Get-Annotation -Entity $_ -CustomAttribute OS).Value -eq ''} or where{(Get-Annotation -Entity $_ -CustomAttribute LOB).Value -eq ''}

thanks

 

 

0 Kudos
1 Solution

Accepted Solutions
pdirmann01
Enthusiast
Enthusiast
Jump to solution

How about if you just assign a variable to the line with you Get-VM statement, then just you a simple If/Then for the Send-MailMessage cmdlet, e.g.:

$vmList = Get-VM | Where { $_.PowerState -eq 'PoweredOn' }| where{(Get-Annotation -Entity $_ -CustomAttribute OS).Value -eq ''} | Select Name

If ($vmList) {

$vmList | Export-Csv -NoTypeInformation -UseCulture c:\temp\test.csv

Send-MailMessage -To "xxx" -From "xxx" -Subject "Here is list of VMs where one of the costume attributes are missing" -smtpserver xx -Attachments c:\temp\test.csv

}

Edit: I moved the Export-CSV into the If/Then statement too so it doesn't make an empty file.

View solution in original post

2 Replies
pdirmann01
Enthusiast
Enthusiast
Jump to solution

How about if you just assign a variable to the line with you Get-VM statement, then just you a simple If/Then for the Send-MailMessage cmdlet, e.g.:

$vmList = Get-VM | Where { $_.PowerState -eq 'PoweredOn' }| where{(Get-Annotation -Entity $_ -CustomAttribute OS).Value -eq ''} | Select Name

If ($vmList) {

$vmList | Export-Csv -NoTypeInformation -UseCulture c:\temp\test.csv

Send-MailMessage -To "xxx" -From "xxx" -Subject "Here is list of VMs where one of the costume attributes are missing" -smtpserver xx -Attachments c:\temp\test.csv

}

Edit: I moved the Export-CSV into the If/Then statement too so it doesn't make an empty file.

LucD
Leadership
Leadership
Jump to solution

I think I already replied to the "or" part of your question in Solved: Find VMs only where Specific Custom attribute is m... - VMware Technology Network VMTN


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